-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Extensions.cs
70 lines (58 loc) · 3.32 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Diagnostics.CodeAnalysis;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Modules.Controllers.Provider;
using GenHTTP.Modules.Conversion.Formatters;
using GenHTTP.Modules.Conversion.Providers;
using GenHTTP.Modules.Layouting.Provider;
using GenHTTP.Modules.Reflection.Injectors;
namespace GenHTTP.Modules.Controllers
{
public static class Extensions
{
/// <summary>
/// Causes all requests to the specified path to be handled by the
/// given controller class.
/// </summary>
/// <typeparam name="T">The type of the controller used to handle requests</typeparam>
/// <param name="builder">The layout the controller should be added to</param>
/// <param name="path">The path that should be handled by the controller</param>
/// <param name="injectors">Optionally the injectors to be used by this controller</param>
/// <param name="serializers">Optionally the serializers to be used by this controller</param>
/// <param name="formatters">Optionally the formatters to be used by this controller</param>
public static LayoutBuilder AddController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, string path, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
{
builder.Add(path, Controller.From<T>().Configured(injectors, serializers, formatters));
return builder;
}
/// <summary>
/// Causes the specified controller class to be used to handle the index of
/// this layout.
/// </summary>
/// <typeparam name="T">The type of the controller used to handle requests</typeparam>
/// <param name="builder">The layout the controller should be added to</param>
/// <param name="injectors">Optionally the injectors to be used by this controller</param>
/// <param name="serializers">Optionally the serializers to be used by this controller</param>
/// <param name="formatters">Optionally the formatters to be used by this controller</param>
public static LayoutBuilder IndexController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
{
builder.Add(Controller.From<T>().Configured(injectors, serializers, formatters));
return builder;
}
private static ControllerBuilder<T> Configured<T>(this ControllerBuilder<T> builder, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
{
if (injectors != null)
{
builder.Injectors(injectors);
}
if (serializers != null)
{
builder.Serializers(serializers);
}
if (formatters != null)
{
builder.Formatters(formatters);
}
return builder;
}
}
}