Skip to content

Commit

Permalink
Allow to pass serialization and injection factories for web services
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Feb 21, 2024
1 parent f336b9c commit 59315c3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Modules/Webservices/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Diagnostics.CodeAnalysis;

using GenHTTP.Api.Infrastructure;

using GenHTTP.Modules.Conversion.Providers;
using GenHTTP.Modules.Layouting.Provider;
using GenHTTP.Modules.Reflection.Injectors;
using GenHTTP.Modules.Webservices.Provider;

namespace GenHTTP.Modules.Webservices
{
Expand All @@ -17,9 +22,11 @@ public static class Extensions
/// </summary>
/// <typeparam name="T">The type of the resource to be added</typeparam>
/// <param name="path">The path the resource should be available at</param>
public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path) where T : new()
/// <param name="injectors">Optionally the injectors to be used by this service</param>
/// <param name="formats">Optionally the formats to be used by this service</param>
public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? formats = null) where T : new()
{
return layout.Add(path, ServiceResource.From<T>());
return layout.Add(path, ServiceResource.From<T>().Configured(injectors, formats));
}

/// <summary>
Expand All @@ -28,9 +35,26 @@ public static class Extensions
/// </summary>
/// <param name="path">The path the resource should be available at</param>
/// <param name="instance">The webservice resource instance</param>
public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance)
/// <param name="injectors">Optionally the injectors to be used by this service</param>
/// <param name="formats">Optionally the formats to be used by this service</param>
public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? formats = null)
{
return layout.Add(path, ServiceResource.From(instance).Configured(injectors, formats));
}

private static ServiceResourceBuilder Configured(this ServiceResourceBuilder builder, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? formats = null)
{
return layout.Add(path, ServiceResource.From(instance));
if (injectors != null)
{
builder.Injectors(injectors);
}

if (formats != null)
{
builder.Formats(formats);
}

return builder;
}

}
Expand Down
58 changes: 58 additions & 0 deletions Testing/Acceptance/Modules/Webservices/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Net;
using System.Threading.Tasks;

using GenHTTP.Modules.Conversion;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Reflection;
using GenHTTP.Modules.Webservices;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.Webservices
{

[TestClass]
public sealed class ExtensionTests
{

#region Supporting data structures

public class TestService
{

[ResourceMethod]
public int DoWork() => 42;

}

#endregion

#region Tests

[TestMethod]
public async Task TestConfiguration()
{
var injectors = Injection.Default();

var formats = Serialization.Default();

var app = Layout.Create()
.AddService<TestService>("by-type", injectors, formats)
.AddService("by-instance", new TestService(), injectors, formats);

using var host = TestHost.Run(app);

using var r1 = await host.GetResponseAsync("/by-type");

await r1.AssertStatusAsync(HttpStatusCode.OK);

using var r2 = await host.GetResponseAsync("/by-instance");

await r2.AssertStatusAsync(HttpStatusCode.OK);
}

#endregion

}

}

0 comments on commit 59315c3

Please sign in to comment.