Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Decorator : Collection<IDecorator>
- {
- private readonly IServiceCollection _serviceProvider;
- public Decorator(IServiceCollection serviceProvider)
- {
- _serviceProvider = serviceProvider;
- }
- public new void Add<T>(IDecorator decorator)
- {
- _serviceProvider.Decorate<T>((inner, provider) =>
- {
- return (T)decorator;
- });
- Items.Add(decorator);
- }
- }
- public static class ServicesCollection
- {
- public static void AddCqrs(this IServiceCollection services,Assembly assemblies, Action<CqrsConfigurationOptions> options)
- {
- options.Invoke(CqrsConfigurationOptions.Create(services, assemblies));
- }
- }
- public class CqrsConfigurationOptions
- {
- public readonly IServiceProvider serviceProvider;
- public readonly Decorator Decorators;
- private CqrsConfigurationOptions(IServiceCollection serviceCollection)
- {
- serviceProvider = serviceCollection.BuildServiceProvider();
- Decorators = new Decorator(serviceCollection);
- }
- public static CqrsConfigurationOptions Create(IServiceCollection serviceCollection, params Assembly[] assemblies)
- {
- RegisterCommand(serviceCollection, assemblies);
- return new CqrsConfigurationOptions(serviceCollection);
- }
- private static void RegisterCommand(IServiceCollection services, params Assembly[] assemblies)
- {
- if(!assemblies.Any())
- {
- throw new ArgumentNullException();
- }
- services.Scan(scan =>
- scan.FromAssemblies(assemblies)
- .AddClasses(classess => classess.AssignableTo(typeof(ICommandHandler<,>)))
- .AddClasses(classess => classess.AssignableTo(typeof(ICommandHandler<>)))
- .AsSelf()
- .AsImplementedInterfaces().WithScopedLifetime());
- services.AddScoped<ICommandDispatcher, CommandDispatcher>();
- services.AddScoped<ICommandFactory, CommandFactory>();
- services.AddScoped<IGate, CommandGate>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement