Skip to content

Commit 59315c3

Browse files
Allow to pass serialization and injection factories for web services
1 parent f336b9c commit 59315c3

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

Modules/Webservices/Extensions.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using System.Diagnostics.CodeAnalysis;
22

3+
using GenHTTP.Api.Infrastructure;
4+
5+
using GenHTTP.Modules.Conversion.Providers;
36
using GenHTTP.Modules.Layouting.Provider;
7+
using GenHTTP.Modules.Reflection.Injectors;
8+
using GenHTTP.Modules.Webservices.Provider;
49

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

2532
/// <summary>
@@ -28,9 +35,26 @@ public static class Extensions
2835
/// </summary>
2936
/// <param name="path">The path the resource should be available at</param>
3037
/// <param name="instance">The webservice resource instance</param>
31-
public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance)
38+
/// <param name="injectors">Optionally the injectors to be used by this service</param>
39+
/// <param name="formats">Optionally the formats to be used by this service</param>
40+
public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? formats = null)
41+
{
42+
return layout.Add(path, ServiceResource.From(instance).Configured(injectors, formats));
43+
}
44+
45+
private static ServiceResourceBuilder Configured(this ServiceResourceBuilder builder, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? formats = null)
3246
{
33-
return layout.Add(path, ServiceResource.From(instance));
47+
if (injectors != null)
48+
{
49+
builder.Injectors(injectors);
50+
}
51+
52+
if (formats != null)
53+
{
54+
builder.Formats(formats);
55+
}
56+
57+
return builder;
3458
}
3559

3660
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
4+
using GenHTTP.Modules.Conversion;
5+
using GenHTTP.Modules.Layouting;
6+
using GenHTTP.Modules.Reflection;
7+
using GenHTTP.Modules.Webservices;
8+
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
namespace GenHTTP.Testing.Acceptance.Modules.Webservices
12+
{
13+
14+
[TestClass]
15+
public sealed class ExtensionTests
16+
{
17+
18+
#region Supporting data structures
19+
20+
public class TestService
21+
{
22+
23+
[ResourceMethod]
24+
public int DoWork() => 42;
25+
26+
}
27+
28+
#endregion
29+
30+
#region Tests
31+
32+
[TestMethod]
33+
public async Task TestConfiguration()
34+
{
35+
var injectors = Injection.Default();
36+
37+
var formats = Serialization.Default();
38+
39+
var app = Layout.Create()
40+
.AddService<TestService>("by-type", injectors, formats)
41+
.AddService("by-instance", new TestService(), injectors, formats);
42+
43+
using var host = TestHost.Run(app);
44+
45+
using var r1 = await host.GetResponseAsync("/by-type");
46+
47+
await r1.AssertStatusAsync(HttpStatusCode.OK);
48+
49+
using var r2 = await host.GetResponseAsync("/by-instance");
50+
51+
await r2.AssertStatusAsync(HttpStatusCode.OK);
52+
}
53+
54+
#endregion
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)