private void RegisterQueryProcessor() { var registry = new QueryHandlerRegistry(); registry.Register <ToDoByIdQuery, ToDoByIdQuery.Result, ToDoByIdQueryHandlerAsync>(); registry.Register <ToDoQueryAll, ToDoQueryAll.Result, ToDoQueryAllHandlerAsync>(); _container.Register <IQueryHandler <ToDoByIdQuery, ToDoByIdQuery.Result>, ToDoByIdQueryHandlerAsync>(Lifestyle.Scoped); _container.Register <IQueryHandler <ToDoQueryAll, ToDoQueryAll.Result>, ToDoQueryAllHandlerAsync>(Lifestyle.Scoped); var retryPolicy = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) }); var circuitBreakerPolicy = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500)); var policyRegistry = new Darker.PolicyRegistry { { QueryProcessor.RetryPolicyName, retryPolicy }, { QueryProcessor.CircuitBreakerPolicyName, circuitBreakerPolicy } }; Func <Type, object> simpleFactory = type => _container.GetInstance(type); IQueryProcessor queryProcessor = QueryProcessorBuilder.With() .Handlers(registry, simpleFactory, Activator.CreateInstance) .Policies(policyRegistry) .InMemoryRequestContextFactory() .Build(); _container.RegisterSingleton <IQueryProcessor>(queryProcessor); }
public void QueryHandlerRegistry_ShouldRegister_QueryHandler() { // Arrange var queryHandlerRegistry = new QueryHandlerRegistry(); // Act queryHandlerRegistry.Register <AllBooksQuery, AllBooksQueryHandler>(); // Assert }
public Benchmark() { var handlerRegistry = new QueryHandlerRegistry(); handlerRegistry.Register <BasicSyncQuery, bool, BasicSyncQueryHandler>(); handlerRegistry.Register <BasicAsyncQuery, bool, BasicAsyncQueryHandler>(); _queryProcessor = QueryProcessorBuilder.With() .Handlers(handlerRegistry, t => (IQueryHandler)Activator.CreateInstance(t), t => (IQueryHandlerDecorator)Activator.CreateInstance(t)) .InMemoryQueryContextFactory() .Build(); }
public void ThrowsConfigurationExceptionWhenResultTypeDoesnotMatch() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); // Act var exception = Assert.Throws <ConfigurationException>(() => handlerRegistry.Register( typeof(TestQueryA), typeof(string), typeof(IQueryHandler <TestQueryA, object>))); // Assert exception.Message.ShouldBe($"Result type not valid for query {typeof(TestQueryA).Name}"); }
public void ReturnsNullForNotRegisteredHandler() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); handlerRegistry.Register <TestQueryA, object, IQueryHandler <TestQueryA, object> >(); // Act var handlerType = handlerRegistry.Get(typeof(TestQueryB)); // Assert handlerType.ShouldBeNull(); }
public void ReturnsRegisteredHandler() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); handlerRegistry.Register(typeof(TestQueryA), typeof(object), typeof(IQueryHandler <TestQueryA, object>)); // Act var handlerType = handlerRegistry.Get(typeof(TestQueryA)); // Assert handlerType.ShouldBe(typeof(IQueryHandler <TestQueryA, object>)); }
public void ReturnsRegisteredHandler() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); handlerRegistry.Register <TestQueryA, TestQueryA.Response, IQueryHandler <TestQueryA, TestQueryA.Response> >(); // Act var handlerType = handlerRegistry.Get(typeof(TestQueryA)); // Assert handlerType.ShouldBe(typeof(IQueryHandler <TestQueryA, TestQueryA.Response>)); }
public void FindsAndRegistersAllHandlersInSingleAssembly() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); // Act handlerRegistry.RegisterFromAssemblies(new[] { typeof(TestQueryHandler).Assembly }); // Assert handlerRegistry.Get(typeof(TestQueryA)).ShouldNotBeNull(); handlerRegistry.Get(typeof(TestQueryB)).ShouldBeNull(); handlerRegistry.Get(typeof(TestQueryC)).ShouldBeNull(); }
public void QueryHandlerRegistry_ShouldResolve_QueryHandler() { // Arrange var queryHandlerRegistry = new QueryHandlerRegistry(); // Act queryHandlerRegistry.Register <AllBooksQuery, AllBooksQueryHandler>(); // Assert var resolved = queryHandlerRegistry.Resolve <AllBooksQuery>(); Assert.NotNull(resolved); Assert.Equal(nameof(AllBooksQueryHandler), resolved.Name); }
public void ThrowsConfigurationExceptionWhenAddingADuplicatedRegistration() { // Arrange var handlerRegistry = new QueryHandlerRegistry(); handlerRegistry.Register <TestQueryA, object, IQueryHandler <TestQueryA, object> >(); // Act var exception = Assert.Throws <ConfigurationException>(() => handlerRegistry.Register <TestQueryA, object, IQueryHandler <TestQueryA, object> >()); // Assert exception.Message.ShouldBe($"Registry already contains an entry for {typeof(TestQueryA).Name}"); handlerRegistry.Get(typeof(TestQueryA)).ShouldNotBeNull(); handlerRegistry.Get(typeof(TestQueryB)).ShouldBeNull(); handlerRegistry.Get(typeof(TestQueryC)).ShouldBeNull(); }
private static void RegisterQueryProcessor(Container container) { var registry = new QueryHandlerRegistry(); registry.Register <GetWorksheetByIdQuery, Worksheet, GetWorksheetByIdQueryHandler>(); var decoratorRegistry = new QueryHandlerDecoratorRegistry(container); var queryHandlerFactory = new QueryHandlerFactory(container); var queryHandlerDecoratorFactory = new QueryHandlerDecoratorFactory(container); IQueryProcessor queryProcessor = QueryProcessorBuilder.With() .Handlers(registry, queryHandlerFactory, decoratorRegistry, queryHandlerDecoratorFactory) .InMemoryQueryContextFactory() .Build(); container.Register <IQueryProcessor>(() => { return(queryProcessor); }); }
public static void RegQueryProcessor(this IServiceCollection services, Action <QueryHandlerRegistry> action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } var queryHandlerRegistry = new QueryHandlerRegistry(); action(queryHandlerRegistry); foreach (var registeredHandler in queryHandlerRegistry.RegisteredHandlers) { services.AddScoped(registeredHandler); } services.AddSingleton <IQueryHandlerRegistry>(queryHandlerRegistry); services.AddScoped <IQueryProcessor, QueryProcessor>(); }
public static INeedAQueryContext SimpleInjectorHandlers(this INeedHandlers handlerBuilder, Container container, Action <HandlerSettings> settings) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var factory = new SimpleInjectorHandlerFactory(container); var handlerRegistry = new QueryHandlerRegistry(); var handlerSettings = new HandlerSettings(container, handlerRegistry); settings(handlerSettings); var handlerConfiguration = new HandlerConfiguration(handlerRegistry, factory); return(handlerBuilder.Handlers(handlerConfiguration)); }