Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public class Decorator : Collection<IDecorator>
  2. {
  3. private readonly IServiceCollection _serviceProvider;
  4.  
  5. public Decorator(IServiceCollection serviceProvider)
  6. {
  7. _serviceProvider = serviceProvider;
  8. }
  9.  
  10. public new void Add<T>(IDecorator decorator)
  11. {
  12. _serviceProvider.Decorate<T>((inner, provider) =>
  13. {
  14. return (T)decorator;
  15. });
  16.  
  17. Items.Add(decorator);
  18. }
  19. }
  20.  
  21. public static class ServicesCollection
  22. {
  23. public static void AddCqrs(this IServiceCollection services,Assembly assemblies, Action<CqrsConfigurationOptions> options)
  24. {
  25. options.Invoke(CqrsConfigurationOptions.Create(services, assemblies));
  26. }
  27. }
  28.  
  29. public class CqrsConfigurationOptions
  30. {
  31. public readonly IServiceProvider serviceProvider;
  32. public readonly Decorator Decorators;
  33.  
  34. private CqrsConfigurationOptions(IServiceCollection serviceCollection)
  35. {
  36. serviceProvider = serviceCollection.BuildServiceProvider();
  37. Decorators = new Decorator(serviceCollection);
  38. }
  39.  
  40. public static CqrsConfigurationOptions Create(IServiceCollection serviceCollection, params Assembly[] assemblies)
  41. {
  42. RegisterCommand(serviceCollection, assemblies);
  43.  
  44. return new CqrsConfigurationOptions(serviceCollection);
  45. }
  46.  
  47. private static void RegisterCommand(IServiceCollection services, params Assembly[] assemblies)
  48. {
  49. if(!assemblies.Any())
  50. {
  51. throw new ArgumentNullException();
  52. }
  53.  
  54. services.Scan(scan =>
  55. scan.FromAssemblies(assemblies)
  56. .AddClasses(classess => classess.AssignableTo(typeof(ICommandHandler<,>)))
  57. .AddClasses(classess => classess.AssignableTo(typeof(ICommandHandler<>)))
  58. .AsSelf()
  59. .AsImplementedInterfaces().WithScopedLifetime());
  60.  
  61. services.AddScoped<ICommandDispatcher, CommandDispatcher>();
  62. services.AddScoped<ICommandFactory, CommandFactory>();
  63. services.AddScoped<IGate, CommandGate>();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement