public static IServiceCollection AddShriek(this IServiceCollection services, Action <ShriekOption> optionAction = null) { services.Scan(scan => scan.FromAssemblies(Reflection.GetAssemblies()) .AddClasses() .AsImplementedInterfaces() .WithScopedLifetime()); services.AddScoped <IEventStorage, InMemoryEventStorage>(); services.AddTransient <IMessagePublisher, InProcessMessagePublisher>(); if (optionAction != null) { var options = new ShriekOption(); optionAction(options); } return(services); }
public static IShriekBuilder AddShriek(this IServiceCollection services, Action <ShriekOption> optionAction = null) { var builder = new ShriekBuilder(services); builder.Services.Scan(scan => scan.FromAssemblies(Reflection.CurrentAssembiles) .AddClasses() .AsImplementedInterfaces() .WithScopedLifetime()); builder.Services.AddScoped <IEventStorage, InMemoryEventStorage>(); builder.Services.AddTransient <IMessagePublisher, InProcessMessagePublisher>(); builder.Services.AddSingleton(typeof(IMessageSubscriber <DomainNotification>), typeof(EventMessageSubscriber <DomainNotification>)); var messages = Reflection.CurrentAssembiles.SelectMany(x => x.GetTypes()).Where(x => x.Assembly != Assembly.GetExecutingAssembly() && typeof(Message).IsAssignableFrom(x)); foreach (var msg in messages) { var type = typeof(IMessageSubscriber <>).MakeGenericType(msg); if (typeof(Command).IsAssignableFrom(msg)) { var impl = typeof(CommandMessageSubscriber <>).MakeGenericType(msg); builder.Services.AddSingleton(type, impl); } if (typeof(Event).IsAssignableFrom(msg)) { var impl = typeof(EventMessageSubscriber <>).MakeGenericType(msg); builder.Services.AddSingleton(type, impl); } } if (optionAction != null) { var options = new ShriekOption(); optionAction(options); } return(builder); }