From 690ccbbdf8a4804896c214600d68505de90e4ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=A4geli?= Date: Tue, 15 Oct 2024 16:43:55 +0200 Subject: [PATCH] Rename method extensions to registry --- .../Controllers/Provider/ControllerBuilder.cs | 2 +- .../Controllers/Provider/ControllerHandler.cs | 18 +++++------ Modules/Functional/Provider/InlineBuilder.cs | 2 +- Modules/Functional/Provider/InlineHandler.cs | 12 +++---- Modules/Reflection/MethodHandler.cs | 16 +++++----- ...{MethodExtensions.cs => MethodRegistry.cs} | 32 +++++++++---------- .../Reflection/Operations/ArgumentProvider.cs | 22 ++++++------- .../Reflection/Operations/OperationBuilder.cs | 4 +-- .../Operations/SignatureAnalyzer.cs | 10 +++--- Modules/Reflection/ResponseProvider.cs | 12 +++---- .../Provider/ServiceResourceBuilder.cs | 2 +- .../Provider/ServiceResourceRouter.cs | 12 +++---- 12 files changed, 72 insertions(+), 72 deletions(-) rename Modules/Reflection/{MethodExtensions.cs => MethodRegistry.cs} (86%) diff --git a/Modules/Controllers/Provider/ControllerBuilder.cs b/Modules/Controllers/Provider/ControllerBuilder.cs index a6441de3..3891eb2c 100644 --- a/Modules/Controllers/Provider/ControllerBuilder.cs +++ b/Modules/Controllers/Provider/ControllerBuilder.cs @@ -69,7 +69,7 @@ public IHandler Build(IHandler parent) var instance = _Instance ?? throw new BuilderMissingPropertyException("Instance or Type"); - var extensions = new MethodExtensions(serializers, injectors, formatters); + var extensions = new MethodRegistry(serializers, injectors, formatters); return Concerns.Chain(parent, _Concerns, p => new ControllerHandler(p, instance, extensions)); } diff --git a/Modules/Controllers/Provider/ControllerHandler.cs b/Modules/Controllers/Provider/ControllerHandler.cs index 565f79aa..cef88190 100644 --- a/Modules/Controllers/Provider/ControllerHandler.cs +++ b/Modules/Controllers/Provider/ControllerHandler.cs @@ -21,7 +21,7 @@ public sealed partial class ControllerHandler : IHandler private ResponseProvider ResponseProvider { get; } - private MethodExtensions Extensions { get; } + private MethodRegistry Registry { get; } private object Instance { get; } @@ -29,19 +29,19 @@ public sealed partial class ControllerHandler : IHandler #region Initialization - public ControllerHandler(IHandler parent, object instance, MethodExtensions extensions) + public ControllerHandler(IHandler parent, object instance, MethodRegistry registry) { Parent = parent; - Extensions = extensions; + Registry = registry; Instance = instance; - ResponseProvider = new ResponseProvider(extensions); + ResponseProvider = new ResponseProvider(registry); - Provider = new MethodCollection(this, AnalyzeMethods(instance.GetType(), extensions)); + Provider = new MethodCollection(this, AnalyzeMethods(instance.GetType(), registry)); } - private IEnumerable> AnalyzeMethods(Type type, MethodExtensions extensions) + private IEnumerable> AnalyzeMethods(Type type, MethodRegistry registry) { foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { @@ -51,7 +51,7 @@ private IEnumerable> AnalyzeMethods(Type type, Met var operation = CreateOperation(method, arguments); - yield return parent => new MethodHandler(parent, operation, () => Instance, annotation, ResponseProvider.GetResponseAsync, extensions); + yield return parent => new MethodHandler(parent, operation, () => Instance, annotation, ResponseProvider.GetResponseAsync, registry); } } @@ -61,14 +61,14 @@ private Operation CreateOperation(MethodInfo method, List arguments) if (method.Name == "Index") { - return OperationBuilder.Create(pathArguments.Length > 0 ? $"/{pathArguments}/" : null, method, Extensions,true); + return OperationBuilder.Create(pathArguments.Length > 0 ? $"/{pathArguments}/" : null, method, Registry,true); } var name = HypenCase(method.Name); var path = $"/{name}"; - return OperationBuilder.Create(pathArguments.Length > 0 ? $"{path}/{pathArguments}/" : $"{path}/", method, Extensions, true); + return OperationBuilder.Create(pathArguments.Length > 0 ? $"{path}/{pathArguments}/" : $"{path}/", method, Registry, true); } private List FindPathArguments(MethodInfo method) diff --git a/Modules/Functional/Provider/InlineBuilder.cs b/Modules/Functional/Provider/InlineBuilder.cs index cba0aa88..90939a7a 100644 --- a/Modules/Functional/Provider/InlineBuilder.cs +++ b/Modules/Functional/Provider/InlineBuilder.cs @@ -181,7 +181,7 @@ public IHandler Build(IHandler parent) var formatters = (_Formatters ?? Formatting.Default()).Build(); - var extensions = new MethodExtensions(serializers, injectors, formatters); + var extensions = new MethodRegistry(serializers, injectors, formatters); return Concerns.Chain(parent, _Concerns, p => new InlineHandler(p, _Functions, extensions)); } diff --git a/Modules/Functional/Provider/InlineHandler.cs b/Modules/Functional/Provider/InlineHandler.cs index a2104ff6..3df99aa0 100644 --- a/Modules/Functional/Provider/InlineHandler.cs +++ b/Modules/Functional/Provider/InlineHandler.cs @@ -21,26 +21,26 @@ public class InlineHandler : IHandler #region Initialization - public InlineHandler(IHandler parent, List functions, MethodExtensions extensions) + public InlineHandler(IHandler parent, List functions, MethodRegistry registry) { Parent = parent; - ResponseProvider = new ResponseProvider(extensions); + ResponseProvider = new ResponseProvider(registry); - Methods = new MethodCollection(this, AnalyzeMethods(functions, extensions)); + Methods = new MethodCollection(this, AnalyzeMethods(functions, registry)); } - private IEnumerable> AnalyzeMethods(List functions, MethodExtensions extensions) + private IEnumerable> AnalyzeMethods(List functions, MethodRegistry registry) { foreach (var function in functions) { var method = function.Delegate.Method; - var operation = OperationBuilder.Create(function.Path, method, extensions); + var operation = OperationBuilder.Create(function.Path, method, registry); var target = function.Delegate.Target ?? throw new InvalidOperationException("Delegate target must not be null"); - yield return parent => new MethodHandler(parent, operation, () => target, function.Configuration, ResponseProvider.GetResponseAsync, extensions); + yield return parent => new MethodHandler(parent, operation, () => target, function.Configuration, ResponseProvider.GetResponseAsync, registry); } } diff --git a/Modules/Reflection/MethodHandler.cs b/Modules/Reflection/MethodHandler.cs index 8455df83..55de8ee9 100644 --- a/Modules/Reflection/MethodHandler.cs +++ b/Modules/Reflection/MethodHandler.cs @@ -37,7 +37,7 @@ public sealed class MethodHandler : IHandler private Func?, ValueTask> ResponseProvider { get; } - public MethodExtensions Extensions { get; } + public MethodRegistry Registry { get; } #endregion @@ -45,7 +45,7 @@ public sealed class MethodHandler : IHandler public MethodHandler(IHandler parent, Operation operation, Func instanceProvider, IMethodConfiguration metaData, Func?, ValueTask> responseProvider, - MethodExtensions extensions) + MethodRegistry registry) { Parent = parent; @@ -55,7 +55,7 @@ public MethodHandler(IHandler parent, Operation operation, Func instance ResponseProvider = responseProvider; Operation = operation; - Extensions = extensions; + Registry = registry; } #endregion @@ -102,11 +102,11 @@ public MethodHandler(IHandler parent, Operation operation, Func instance { targetArguments[i] = arg.Source switch { - OperationArgumentSource.Injected => ArgumentProvider.GetInjectedArgument(request, this, arg, Extensions), - OperationArgumentSource.Path => ArgumentProvider.GetPathArgument(arg, sourceParameters, Extensions), - OperationArgumentSource.Body => await ArgumentProvider.GetBodyArgumentAsync(request, arg, Extensions), - OperationArgumentSource.Query => ArgumentProvider.GetQueryArgument(request, bodyArguments, arg, Extensions), - OperationArgumentSource.Content => await ArgumentProvider.GetContentAsync(request, arg, Extensions), + OperationArgumentSource.Injected => ArgumentProvider.GetInjectedArgument(request, this, arg, Registry), + OperationArgumentSource.Path => ArgumentProvider.GetPathArgument(arg, sourceParameters, Registry), + OperationArgumentSource.Body => await ArgumentProvider.GetBodyArgumentAsync(request, arg, Registry), + OperationArgumentSource.Query => ArgumentProvider.GetQueryArgument(request, bodyArguments, arg, Registry), + OperationArgumentSource.Content => await ArgumentProvider.GetContentAsync(request, arg, Registry), _ => throw new ProviderException(ResponseStatus.InternalServerError, $"Unable to map argument '{arg.Name}' of type '{arg.Type}' because source '{arg.Source}' is not supported") }; } diff --git a/Modules/Reflection/MethodExtensions.cs b/Modules/Reflection/MethodRegistry.cs similarity index 86% rename from Modules/Reflection/MethodExtensions.cs rename to Modules/Reflection/MethodRegistry.cs index 1ed94fb0..dea30a54 100644 --- a/Modules/Reflection/MethodExtensions.cs +++ b/Modules/Reflection/MethodRegistry.cs @@ -1,16 +1,16 @@ -using GenHTTP.Modules.Conversion.Formatters; -using GenHTTP.Modules.Conversion.Serializers; -using GenHTTP.Modules.Reflection.Injectors; - -namespace GenHTTP.Modules.Reflection; - -public record MethodExtensions -( - - SerializationRegistry Serialization, - - InjectionRegistry Injection, - - FormatterRegistry Formatting - -); +using GenHTTP.Modules.Conversion.Formatters; +using GenHTTP.Modules.Conversion.Serializers; +using GenHTTP.Modules.Reflection.Injectors; + +namespace GenHTTP.Modules.Reflection; + +public record MethodRegistry +( + + SerializationRegistry Serialization, + + InjectionRegistry Injection, + + FormatterRegistry Formatting + +); diff --git a/Modules/Reflection/Operations/ArgumentProvider.cs b/Modules/Reflection/Operations/ArgumentProvider.cs index f9d58322..078e58f1 100644 --- a/Modules/Reflection/Operations/ArgumentProvider.cs +++ b/Modules/Reflection/Operations/ArgumentProvider.cs @@ -10,9 +10,9 @@ namespace GenHTTP.Modules.Reflection.Operations; public static class ArgumentProvider { - public static object? GetInjectedArgument(IRequest request, IHandler handler, OperationArgument argument, MethodExtensions extensions) + public static object? GetInjectedArgument(IRequest request, IHandler handler, OperationArgument argument, MethodRegistry registry) { - foreach (var injector in extensions.Injection) + foreach (var injector in registry.Injection) { if (injector.Supports(argument.Type)) { @@ -23,7 +23,7 @@ public static class ArgumentProvider return null; } - public static object? GetPathArgument(OperationArgument argument, Match? matchedPath, MethodExtensions extensions) + public static object? GetPathArgument(OperationArgument argument, Match? matchedPath, MethodRegistry registry) { if (matchedPath != null) { @@ -31,14 +31,14 @@ public static class ArgumentProvider if (sourceArgument.Success) { - return sourceArgument.Value.ConvertTo(argument.Type, extensions.Formatting); + return sourceArgument.Value.ConvertTo(argument.Type, registry.Formatting); } } return null; } - public static async ValueTask GetBodyArgumentAsync(IRequest request, OperationArgument argument, MethodExtensions extensions) + public static async ValueTask GetBodyArgumentAsync(IRequest request, OperationArgument argument, MethodRegistry registry) { if (request.Content == null) { @@ -53,7 +53,7 @@ public static class ArgumentProvider if (!string.IsNullOrWhiteSpace(body)) { - result = body.ConvertTo(argument.Type, extensions.Formatting); + result = body.ConvertTo(argument.Type, registry.Formatting); } request.Content.Seek(0, SeekOrigin.Begin); @@ -61,27 +61,27 @@ public static class ArgumentProvider return result; } - public static object? GetQueryArgument(IRequest request, Dictionary? formArguments, OperationArgument argument, MethodExtensions extensions) + public static object? GetQueryArgument(IRequest request, Dictionary? formArguments, OperationArgument argument, MethodRegistry registry) { if (request.Query.TryGetValue(argument.Name, out var queryValue)) { - return queryValue.ConvertTo(argument.Type, extensions.Formatting); + return queryValue.ConvertTo(argument.Type, registry.Formatting); } if (formArguments is not null) { if (formArguments.TryGetValue(argument.Name, out var bodyValue)) { - return bodyValue.ConvertTo(argument.Type, extensions.Formatting); + return bodyValue.ConvertTo(argument.Type, registry.Formatting); } } return null; } - public static async ValueTask GetContentAsync(IRequest request, OperationArgument argument, MethodExtensions extensions) + public static async ValueTask GetContentAsync(IRequest request, OperationArgument argument, MethodRegistry registry) { - var deserializer = extensions.Serialization.GetDeserialization(request); + var deserializer = registry.Serialization.GetDeserialization(request); if (deserializer is null) { diff --git a/Modules/Reflection/Operations/OperationBuilder.cs b/Modules/Reflection/Operations/OperationBuilder.cs index 84b5e30b..ccc79a2d 100644 --- a/Modules/Reflection/Operations/OperationBuilder.cs +++ b/Modules/Reflection/Operations/OperationBuilder.cs @@ -19,7 +19,7 @@ public static partial class OperationBuilder #region Functionality - public static Operation Create(string? definition, MethodInfo method, MethodExtensions extensions, bool forceTrailingSlash = false) + public static Operation Create(string? definition, MethodInfo method, MethodRegistry registry, bool forceTrailingSlash = false) { var isWildcard = CheckWildcardRoute(method.ReturnType); @@ -76,7 +76,7 @@ public static Operation Create(string? definition, MethodInfo method, MethodExte path = new OperationPath(nameBuilder.ToString(), matcher, false, isWildcard); } - var arguments = SignatureAnalyzer.GetArguments(method, pathArguments, extensions); + var arguments = SignatureAnalyzer.GetArguments(method, pathArguments, registry); return new Operation(method, path, arguments); } diff --git a/Modules/Reflection/Operations/SignatureAnalyzer.cs b/Modules/Reflection/Operations/SignatureAnalyzer.cs index ed0a1c76..fb7adae0 100644 --- a/Modules/Reflection/Operations/SignatureAnalyzer.cs +++ b/Modules/Reflection/Operations/SignatureAnalyzer.cs @@ -6,7 +6,7 @@ namespace GenHTTP.Modules.Reflection.Operations; public static class SignatureAnalyzer { - public static Dictionary GetArguments(MethodInfo method, HashSet pathArguments, MethodExtensions extensions) + public static Dictionary GetArguments(MethodInfo method, HashSet pathArguments, MethodRegistry registry) { var result = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -20,13 +20,13 @@ public static Dictionary GetArguments(MethodInfo meth continue; } - if (TryInject(param, extensions, out var injectedArg)) + if (TryInject(param, registry, out var injectedArg)) { result.Add(param.Name, injectedArg); continue; } - if (param.CanFormat(extensions.Formatting)) + if (param.CanFormat(registry.Formatting)) { if (TryFromBody(param, out var bodyArg)) { @@ -46,9 +46,9 @@ public static Dictionary GetArguments(MethodInfo meth return result; } - private static bool TryInject(ParameterInfo param, MethodExtensions extensions, [NotNullWhen(true)] out OperationArgument? argument) + private static bool TryInject(ParameterInfo param, MethodRegistry registry, [NotNullWhen(true)] out OperationArgument? argument) { - foreach (var injector in extensions.Injection) + foreach (var injector in registry.Injection) { if (injector.Supports(param.ParameterType)) { diff --git a/Modules/Reflection/ResponseProvider.cs b/Modules/Reflection/ResponseProvider.cs index 8fca341a..764d5efd 100644 --- a/Modules/Reflection/ResponseProvider.cs +++ b/Modules/Reflection/ResponseProvider.cs @@ -17,15 +17,15 @@ public class ResponseProvider #region Get-/Setters - private MethodExtensions Extensions { get; } + private MethodRegistry Registry { get; } #endregion #region Initialization - public ResponseProvider(MethodExtensions extensions) + public ResponseProvider(MethodRegistry registry) { - Extensions = extensions; + Registry = registry; } #endregion @@ -91,17 +91,17 @@ public ResponseProvider(MethodExtensions extensions) } // format the value if possible - if (Extensions.Formatting.CanHandle(type)) + if (Registry.Formatting.CanHandle(type)) { return request.Respond() - .Content(Extensions.Formatting.Write(result, type) ?? string.Empty) + .Content(Registry.Formatting.Write(result, type) ?? string.Empty) .Type(ContentType.TextPlain) .Adjust(adjustments) .Build(); } // serialize the result - var serializer = Extensions.Serialization.GetSerialization(request); + var serializer = Registry.Serialization.GetSerialization(request); if (serializer is null) { diff --git a/Modules/Webservices/Provider/ServiceResourceBuilder.cs b/Modules/Webservices/Provider/ServiceResourceBuilder.cs index 55e9f38a..5b2862b8 100644 --- a/Modules/Webservices/Provider/ServiceResourceBuilder.cs +++ b/Modules/Webservices/Provider/ServiceResourceBuilder.cs @@ -65,7 +65,7 @@ public IHandler Build(IHandler parent) var instance = _Instance ?? throw new BuilderMissingPropertyException("instance"); - var extensions = new MethodExtensions(serializers, injectors, formatters); + var extensions = new MethodRegistry(serializers, injectors, formatters); return Concerns.Chain(parent, _Concerns, p => new ServiceResourceRouter(p, instance, extensions)); } diff --git a/Modules/Webservices/Provider/ServiceResourceRouter.cs b/Modules/Webservices/Provider/ServiceResourceRouter.cs index c898575c..290d36a2 100644 --- a/Modules/Webservices/Provider/ServiceResourceRouter.cs +++ b/Modules/Webservices/Provider/ServiceResourceRouter.cs @@ -24,18 +24,18 @@ public sealed class ServiceResourceRouter : IHandler #region Initialization - public ServiceResourceRouter(IHandler parent, object instance, MethodExtensions extensions) + public ServiceResourceRouter(IHandler parent, object instance, MethodRegistry registry) { Parent = parent; Instance = instance; - ResponseProvider = new ResponseProvider(extensions); + ResponseProvider = new ResponseProvider(registry); - Methods = new MethodCollection(this, AnalyzeMethods(instance.GetType(), extensions)); + Methods = new MethodCollection(this, AnalyzeMethods(instance.GetType(), registry)); } - private IEnumerable> AnalyzeMethods(Type type, MethodExtensions extensions) + private IEnumerable> AnalyzeMethods(Type type, MethodRegistry registry) { foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)) { @@ -43,9 +43,9 @@ private IEnumerable> AnalyzeMethods(Type type, Met if (attribute is not null) { - var operation = OperationBuilder.Create(attribute.Path, method, extensions); + var operation = OperationBuilder.Create(attribute.Path, method, registry); - yield return parent => new MethodHandler(parent, operation, () => Instance, attribute, ResponseProvider.GetResponseAsync, extensions); + yield return parent => new MethodHandler(parent, operation, () => Instance, attribute, ResponseProvider.GetResponseAsync, registry); } } }