protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) { base.ConfigureRequestContainer(container, context); // create a singleton instance which will be reused for all calls in current request var ipb = new Router(); container.Register <ICommandSender, Router>(ipb); container.Register <IHandlerRegistrar, Router>(ipb); container.Register <IEventSerializer, EventSerializer>(new EventSerializer()); container.Register <IEventSourceFactory, EventSourceFactory>(); container.Register <ISettingsService, SettingsService>(); container.Register <NeuronCommandHandlers>(); var ticl = new TinyIoCServiceLocator(container); container.Register <IServiceProvider, TinyIoCServiceLocator>(ticl); var registrar = new RouteRegistrar(ticl); registrar.Register(typeof(NeuronCommandHandlers)); // Here we register our user mapper as a per-request singleton. // As this is now per-request we could inject a request scoped // database "context" or other request scoped services. ((TinyIoCServiceLocator)container.Resolve <IServiceProvider>()).SetRequestContainer(container); }
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) { base.ConfigureRequestContainer(container, context); container.Register <ISettingsService, SettingsService>(); container.Register <INeuronRepository, NeuronRepository>(); container.Register <ITerminalRepository, TerminalRepository>(); container.Register <IRepository <Domain.Model.Settings>, SettingsRepository>(); container.Register <INotificationLogClient, StandardNotificationLogClient>(); var ipb = new Router(); container.Register <ICommandSender, Router>(ipb); container.Register <IHandlerRegistrar, Router>(ipb); container.Register <GraphCommandHandlers>(); var ticl = new TinyIoCServiceLocator(container); container.Register <IServiceProvider, TinyIoCServiceLocator>(ticl); var registrar = new RouteRegistrar(ticl); registrar.Register(typeof(GraphCommandHandlers)); // As this is now per-request we could inject a request scoped // database "context" or other request scoped services. ((TinyIoCServiceLocator)container.Resolve <IServiceProvider>()).SetRequestContainer(container); }
public When_registering_handlers() { _locator = new TestServiceLocator(); var register = new RouteRegistrar(_locator); register.Register(GetType()); }
public When_registering_all_handlers_in_assembly() { _testHandleRegistrar = new TestHandleRegistrar(); _locator = new TestServiceLocator(_testHandleRegistrar); var register = new RouteRegistrar(_locator); register.Register(GetType()); // Changed From RegisterINAssemblyOf to Register to match apparent changes in source file }
public static IApplicationBuilder UseCQRSLiteBus(this IApplicationBuilder builder, params Type[] typesFromAssemblyContainingMessages) { var provider = new HttpProvider(builder.ApplicationServices.GetService <IServiceProvider>()); var registrar = new RouteRegistrar(provider); registrar.Register(typesFromAssemblyContainingMessages); return(builder); }
public When_registering_handlers() { _locator = new TestServiceLocator(); var register = new RouteRegistrar(_locator); if (TestHandleRegistrar.HandlerList.Count == 0) { register.Register(GetType()); } }
public When_registering_specific_handlers() { _testHandleRegistrar = new TestHandleRegistrar(); _locator = new TestServiceLocator(_testHandleRegistrar); var register = new RouteRegistrar(_locator); register.Register( // Changed to Register from RegistrarHandler - Method appears to have been changed in source file typeof(TestAggregateDoSomethingHandler), typeof(TestAggregateDoSomethingElseHandler), typeof(AbstractTestAggregateDoSomethingElseHandler), typeof(TestAggregateDoSomethingHandlerExplicit), typeof(TestAggregateDidSomethingHandler), typeof(AllHandler)); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); //Add Cqrs services services.AddSingleton <Router>(new Router()); services.AddSingleton <ICommandSender>(y => y.GetService <Router>()); services.AddSingleton <IEventPublisher>(y => y.GetService <Router>()); services.AddSingleton <IHandlerRegistrar>(y => y.GetService <Router>()); services.AddSingleton <IEventStore, InMemoryEventStore>(); services.AddSingleton <ICache, MemoryCache>(); services.AddScoped <IRepository>(y => new CacheRepository(new Repository(y.GetService <IEventStore>()), y.GetService <IEventStore>(), y.GetService <ICache>())); services.AddScoped <ISession, Session>(); services.AddSingleton <IOrderRepository, OrderRepository>(); //Scan for commandhandlers and eventhandlers services.Scan(scan => scan .FromAssemblies(typeof(OrderCommandHandler).GetTypeInfo().Assembly) .AddClasses(classes => classes.Where(x => { var allInterfaces = x.GetInterfaces(); return (allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IHandler <>)) || allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICancellableHandler <>))); })) .AsSelf() .WithTransientLifetime() ); // Add framework services. services.AddMvc(); //Register routes var serviceProvider = services.BuildServiceProvider(); var registrar = new RouteRegistrar(new Provider(serviceProvider)); registrar.Register(typeof(OrderCommandHandler)); return(serviceProvider); }
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) { base.ConfigureRequestContainer(container, context); // create a singleton instance which will be reused for all calls in current request var ipb = new Router(); container.Register <ICommandSender, Router>(ipb); container.Register <IHandlerRegistrar, Router>(ipb); container.Register <IEventSerializer, EventSerializer>(new EventSerializer()); container.Register <ISettingsService, SettingsService>(); container.Register <IEventStoreUrlService>( (tic, npo) => { var ss = container.Resolve <ISettingsService>(); return(new EventStoreUrlService( ss.EventSourcingInBaseUrl + "/", ss.EventSourcingOutBaseUrl + "/" )); }); container.Register <IEventStore, HttpEventStoreClient>(); container.Register <IRepository, Repository>(); container.Register <ISession, Session>(); container.Register <IItemAdapter, ItemAdapter>(); container.Register <ItemCommandHandlers>(); var ticl = new TinyIoCServiceLocator(container); container.Register <IServiceProvider, TinyIoCServiceLocator>(ticl); var registrar = new RouteRegistrar(ticl); registrar.Register(typeof(ItemCommandHandlers)); // Here we register our user mapper as a per-request singleton. // As this is now per-request we could inject a request scoped // database "context" or other request scoped services. ((TinyIoCServiceLocator)container.Resolve <IServiceProvider>()).SetRequestContainer(container); }
public async Task DoStuff() { var watch = new Stopwatch(); watch.Start(); var router = new Router(); var eventstore = new EventStore(router); IRepository rep = new Repository(eventstore); var cache = new MemoryCache(); //rep = new CacheRepository(rep, eventstore, cache); var session = new Session(rep); for (var index = 0; index < 15_000; index++) { await session.Add(new Employee(Guid.NewGuid())); } await session.Commit(); for (var i = 0; i < 15_000; i++) { await session.Add(new Employee(Guid.NewGuid())); await session.Commit(); } for (var i = 0; i < 15_000; i++) { var rep2 = new Repository(eventstore); var session2 = new Session(rep2); await session2.Add(new Employee(Guid.NewGuid())); await session2.Commit(); } Parallel.For(0, 300, async i => { var id2 = Guid.NewGuid(); var employee = new Employee(id2); var session2 = new Session(rep); await session2.Add(employee); await session2.Commit(); for (int j = 0; j < 100; j++) { var e = await session2.Get <Employee>(id2); for (int k = 0; k < 10; k++) { e.GiveRaise(10); } await session2.Commit(); } }); var id = Guid.NewGuid(); await session.Add(new Employee(id)); await session.Commit(); for (var i = 0; i < 5_000; i++) { var employee = await session.Get <Employee>(id); employee.GiveRaise(10); await session.Commit(); } var registrar = new RouteRegistrar(new ServiceLocator(rep, router)); registrar.Register(typeof(EmployeeGiveRaise)); var num = 3000; var tasks = new Task[num]; for (int i = 0; i < num; i++) { tasks[i] = router.Send(new EmployeeGiveRaise(id, 10)); } await Task.WhenAll(tasks); Parallel.For(0, 3000, async i => { var id2 = Guid.NewGuid(); var employee = new Employee(id2); var session2 = new Session(rep); await session2.Add(employee); await session2.Commit(); for (int j = 0; j < 100; j++) { await router.Send(new EmployeeGiveRaise(id2, 10)); } }); watch.Stop(); Console.WriteLine($"{eventstore.Count:##,###} events saved"); Console.WriteLine($"{eventstore.Read:##,###} events read from eventstore"); Console.WriteLine($"{Employee.AppliedEvents:##,###} events applied"); Console.WriteLine($"{watch.ElapsedMilliseconds:##,###} ms"); Console.WriteLine($"{Employee.AppliedEvents/watch.ElapsedMilliseconds:##,###} events handled per ms"); }
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) { base.ConfigureRequestContainer(container, context); // create a singleton instance which will be reused for all calls in current request var ipb = new Router(); container.Register <ICommandSender, Router>(ipb); container.Register <IHandlerRegistrar, Router>(ipb); container.Register <IRequestProvider>( (tic, npo) => { var rp = new RequestProvider(); rp.SetHttpClientHandler(new HttpClientHandler()); return(rp); }); container.Register <ISettingsService, SettingsService>(); container.Register <IValidationClient, HttpValidationClient>(); container.Register <INeuronGraphQueryClient, HttpNeuronGraphQueryClient>(); // data container.Register <IEventStoreUrlService>( (tic, npo) => { var ss = container.Resolve <ISettingsService>(); return(new EventStoreUrlService( ss.EventSourcingInBaseUrl + "/", ss.EventSourcingOutBaseUrl + "/" )); }); container.Register <IEventSerializer, EventSerializer>(); container.Register <IAuthoredEventStore, HttpEventStoreClient>(); container.Register <IInMemoryAuthoredEventStore, InMemoryEventStore>(); container.Register <IRepository>((tic, npo) => new Repository(container.Resolve <IInMemoryAuthoredEventStore>())); container.Register <ISession, Session>(); // neuron container.Register <INeuronAdapter, NeuronAdapter>(); container.Register((tic, npo) => new neurUL.Cortex.Application.Neurons.NeuronCommandHandlers(container.Resolve <IInMemoryAuthoredEventStore>(), container.Resolve <ISession>())); container.Register <ITerminalAdapter, TerminalAdapter>(); container.Register((tic, npo) => new neurUL.Cortex.Application.Neurons.TerminalCommandHandlers(container.Resolve <IInMemoryAuthoredEventStore>(), container.Resolve <ISession>())); // tag container.Register <ei8.Data.Tag.Port.Adapter.In.InProcess.IItemAdapter, ei8.Data.Tag.Port.Adapter.In.InProcess.ItemAdapter>(); container.Register((tic, npo) => new Data.Tag.Application.ItemCommandHandlers(container.Resolve <IInMemoryAuthoredEventStore>(), container.Resolve <ISession>())); // aggregate container.Register <ei8.Data.Aggregate.Port.Adapter.In.InProcess.IItemAdapter, ei8.Data.Aggregate.Port.Adapter.In.InProcess.ItemAdapter>(); container.Register((tic, npo) => new Data.Aggregate.Application.ItemCommandHandlers(container.Resolve <IInMemoryAuthoredEventStore>(), container.Resolve <ISession>())); // external reference container.Register <ei8.Data.ExternalReference.Port.Adapter.In.InProcess.IItemAdapter, ei8.Data.ExternalReference.Port.Adapter.In.InProcess.ItemAdapter>(); container.Register((tic, npo) => new Data.ExternalReference.Application.ItemCommandHandlers(container.Resolve <IInMemoryAuthoredEventStore>(), container.Resolve <ISession>())); container.Register <NeuronCommandHandlers>(); container.Register <TerminalCommandHandlers>(); var ticl = new TinyIoCServiceLocator(container); container.Register <IServiceProvider, TinyIoCServiceLocator>(ticl); var registrar = new RouteRegistrar(ticl); registrar.Register(typeof(NeuronCommandHandlers)); // neuron registrar.Register(typeof(neurUL.Cortex.Application.Neurons.NeuronCommandHandlers)); // tag registrar.Register(typeof(ei8.Data.Tag.Application.ItemCommandHandlers)); // aggregate registrar.Register(typeof(ei8.Data.Aggregate.Application.ItemCommandHandlers)); // external reference registrar.Register(typeof(ei8.Data.ExternalReference.Application.ItemCommandHandlers)); ((TinyIoCServiceLocator)container.Resolve <IServiceProvider>()).SetRequestContainer(container); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { #region Repository // https://radu-matei.github.io/blog/aspnet-core-json-dependency-injection/ // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration // The following is a hybrid of the two patterns described above. // A configuration section from applicationsettings.json both selects the instance type of the repository and sets its connections string. var repositoryConfig = Configuration.GetSection("Repository"); string assemblyName = repositoryConfig.GetValue <string>("AssemblyName"); string repositoryTypeName = repositoryConfig.GetValue <string>("RepositoryType"); string connectionString = repositoryConfig.GetValue <string>("ConnectionString"); string repoNS = repositoryConfig.GetValue <string>("RepositoryNameSpace"); Assembly assembly = Assembly.Load(new AssemblyName(assemblyName)); Type repositoryType = assembly.GetTypes().First(t => t.Name.StartsWith(repositoryTypeName)); Type baseballPlayRepositoryType = repositoryType.MakeGenericType(typeof(BaseballPlayRM)); services.AddSingleton <BaseballPlayRepository>(y => new BaseballPlayRepository(Activator.CreateInstance(baseballPlayRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <BaseballPlayRM>)); Type lineupRepositoryType = repositoryType.MakeGenericType(typeof(LineupChangeRM)); services.AddSingleton <LineupChangeRepository>(y => new LineupChangeRepository(Activator.CreateInstance(lineupRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <LineupChangeRM>)); Type battingOrderRepositoryType = repositoryType.MakeGenericType(typeof(LineupRM)); services.AddSingleton <LineupRepository>(y => new LineupRepository(Activator.CreateInstance(battingOrderRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <LineupRM>)); Type teamRepositoryType = repositoryType.MakeGenericType(typeof(TeamRM)); services.AddSingleton <TeamRepository>(y => new TeamRepository(Activator.CreateInstance(teamRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <TeamRM>)); Type rosterMemberRepositoryType = repositoryType.MakeGenericType(typeof(RosterMemberRM)); services.AddSingleton <RosterMemberRepository>(y => new RosterMemberRepository(Activator.CreateInstance(rosterMemberRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <RosterMemberRM>)); Type playerRepositoryType = repositoryType.MakeGenericType(typeof(PlayerRM)); services.AddSingleton <PlayerRepository>(y => new PlayerRepository(Activator.CreateInstance(playerRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <PlayerRM>)); Type gameSummaryRepositoryType = repositoryType.MakeGenericType(typeof(GameSummaryRM)); services.AddSingleton <GameSummaryRepository>(y => new GameSummaryRepository(Activator.CreateInstance(gameSummaryRepositoryType, connectionString, repoNS, LoggerFactory) as IRepository <GameSummaryRM>)); #endregion services.AddAutoMapper(typeof(Startup)); services.AddMemoryCache(); //Add Cqrs services services.AddSingleton <Router>(new Router()); services.AddSingleton <ICommandSender>(y => y.GetService <Router>()); services.AddSingleton <IEventPublisher>(y => y.GetService <Router>()); services.AddSingleton <IHandlerRegistrar>(y => y.GetService <Router>()); services.AddScoped <CQRSlite.Domain.ISession, Session>(); services.AddSingleton <IEventStore, InMemoryEventStore>(); services.AddScoped <ICache, MemoryCache>(); services.AddScoped <IRepository>(y => new CacheRepository(new Repository(y.GetService <IEventStore>()), y.GetService <IEventStore>(), y.GetService <ICache>())); // Don't want this. Will use repository instead. // services.AddTransient<IReadModelFacade, ReadModelFacade>(); // https://github.com/gautema/CQRSlite/tree/master/Sample/CQRSCode/ReadModel //Scan for commandhandlers and eventhandlers services.Scan(scan => scan .FromAssemblies(typeof(RetrosheetCommandHandlers).GetTypeInfo().Assembly) // https://github.com/gautema/CQRSlite/blob/master/Sample/CQRSCode/WriteModel/Handlers/InventoryCommandHandlers.cs .AddClasses(classes => classes.Where(x => { var allInterfaces = x.GetInterfaces(); return (allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IHandler <>)) || allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICancellableHandler <>))); })) .AsSelf() .WithTransientLifetime() ); // Add framework services. // https://github.com/JeremySkinner/FluentValidation/issues/412 services .AddMvc(options => { options.Filters.Add(typeof(ValidationActionFilter)); }) .AddFluentValidation(fv => { }); // do not add validator for CreateBaseballPlayRequest services.AddTransient <IValidator <CreateTeamRequest>, CreateTeamRequestValidator>(); services.AddTransient <IValidator <CreateRosterMemberRequest>, CreateRosterMemberRequestValidator>(); services.AddTransient <IValidator <CreateLineupChangeRequest>, CreateLineupChangeRequestValidator>(); services.AddTransient <IValidator <CreateGameSummaryRequest>, CreateGameSummaryRequestValidator>(); //Register router var serviceProvider = services.BuildServiceProvider(); //var registrar = new RouteRegistrar(new DependencyResolver(serviceProvider)); var registrar = new RouteRegistrar(new Provider(serviceProvider)); registrar.Register(typeof(RetrosheetCommandHandlers)); return(serviceProvider); }