|
| 1 | +namespace Carter |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using FluentValidation; |
| 6 | + using Microsoft.Extensions.Logging; |
| 7 | + |
| 8 | + /// <summary> |
| 9 | + /// Configures registrations of certain types within Carter |
| 10 | + /// </summary> |
| 11 | + public class CarterConfigurator |
| 12 | + { |
| 13 | + internal CarterConfigurator() |
| 14 | + { |
| 15 | + this.ModuleTypes = new List<Type>(); |
| 16 | + this.ValidatorTypes = new List<Type>(); |
| 17 | + this.StatusCodeHandlerTypes = new List<Type>(); |
| 18 | + this.ResponseNegotiatorTypes = new List<Type>(); |
| 19 | + } |
| 20 | + |
| 21 | + internal List<Type> ModuleTypes { get; } |
| 22 | + |
| 23 | + internal List<Type> ValidatorTypes { get; } |
| 24 | + |
| 25 | + internal List<Type> StatusCodeHandlerTypes { get; } |
| 26 | + |
| 27 | + internal List<Type> ResponseNegotiatorTypes { get; } |
| 28 | + |
| 29 | + internal void LogDiscoveredCarterTypes(ILogger logger) |
| 30 | + { |
| 31 | + foreach (var validator in this.ValidatorTypes) |
| 32 | + { |
| 33 | + logger.LogDebug("Found validator {ValidatorName}", validator.Name); |
| 34 | + } |
| 35 | + |
| 36 | + foreach (var module in this.ModuleTypes) |
| 37 | + { |
| 38 | + logger.LogDebug("Found module {ModuleName}", module.FullName); |
| 39 | + } |
| 40 | + |
| 41 | + foreach (var sch in this.StatusCodeHandlerTypes) |
| 42 | + { |
| 43 | + logger.LogDebug("Found status code handler {StatusCodeHandlerName}", sch.FullName); |
| 44 | + } |
| 45 | + |
| 46 | + foreach (var negotiator in this.ResponseNegotiatorTypes) |
| 47 | + { |
| 48 | + logger.LogDebug("Found response negotiator {ResponseNegotiatorName}", negotiator.FullName); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Register a specific <see cref="CarterModule"/> |
| 54 | + /// </summary> |
| 55 | + /// <typeparam name="TModule">The <see cref="CarterModule"/> to register</typeparam> |
| 56 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 57 | + public CarterConfigurator WithModule<TModule>() where TModule : CarterModule |
| 58 | + { |
| 59 | + this.ModuleTypes.Add(typeof(TModule)); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Register specific <see cref="CarterModule"/>s |
| 65 | + /// </summary> |
| 66 | + /// <param name="modules">An array of <see cref="CarterModule"/>s</param> |
| 67 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 68 | + public CarterConfigurator WithModules(params Type[] modules) |
| 69 | + { |
| 70 | + modules.MustDeriveFrom<CarterModule>(); |
| 71 | + this.ModuleTypes.AddRange(modules); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Register a specific <see cref="IValidator"/> |
| 77 | + /// </summary> |
| 78 | + /// <typeparam name="T">The <see cref="IValidator"/> to register</typeparam> |
| 79 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 80 | + public CarterConfigurator WithValidator<T>() where T : IValidator |
| 81 | + { |
| 82 | + this.ValidatorTypes.Add(typeof(T)); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Register specific <see cref="IValidator"/>s |
| 88 | + /// </summary> |
| 89 | + /// <param name="validators">An array of <see cref="IValidator"/>s</param> |
| 90 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 91 | + public CarterConfigurator WithValidators(params Type[] validators) |
| 92 | + { |
| 93 | + validators.MustDeriveFrom<IValidator>(); |
| 94 | + this.ValidatorTypes.AddRange(validators); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Register a specific <see cref="IStatusCodeHandler"/> |
| 100 | + /// </summary> |
| 101 | + /// <typeparam name="T">The <see cref="IStatusCodeHandler"/> to register</typeparam> |
| 102 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 103 | + public CarterConfigurator WithStatusCodeHandler<T>() where T : IStatusCodeHandler |
| 104 | + { |
| 105 | + this.StatusCodeHandlerTypes.Add(typeof(T)); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Register specific <see cref="IStatusCodeHandler"/>s |
| 111 | + /// </summary> |
| 112 | + /// <param name="statusCodeHandlers">An array of <see cref="IStatusCodeHandler"/>s</param> |
| 113 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 114 | + public CarterConfigurator WithStatusCodeHandlers(params Type[] statusCodeHandlers) |
| 115 | + { |
| 116 | + statusCodeHandlers.MustDeriveFrom<IStatusCodeHandler>(); |
| 117 | + this.StatusCodeHandlerTypes.AddRange(statusCodeHandlers); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Register a specific <see cref="IResponseNegotiator"/> |
| 123 | + /// </summary> |
| 124 | + /// <typeparam name="T">The <see cref="IResponseNegotiator"/> to register</typeparam> |
| 125 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 126 | + public CarterConfigurator WithResponseNegotiator<T>() where T : IResponseNegotiator |
| 127 | + { |
| 128 | + this.ResponseNegotiatorTypes.Add(typeof(T)); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Register specific <see cref="IStatusCodeHandler"/>s |
| 134 | + /// </summary> |
| 135 | + /// <param name="responseNegotiators">An array of <see cref="IResponseNegotiator"/>s</param> |
| 136 | + /// <returns><see cref="CarterConfigurator"/></returns> |
| 137 | + public CarterConfigurator WithResponseNegotiators(params Type[] responseNegotiators) |
| 138 | + { |
| 139 | + responseNegotiators.MustDeriveFrom<IResponseNegotiator>(); |
| 140 | + this.ResponseNegotiatorTypes.AddRange(responseNegotiators); |
| 141 | + return this; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments