public static IQueryProcessorExtensionBuilder AddDarker(this IServiceCollection services, Action <DarkerOptions> configure = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } var options = new DarkerOptions(); configure?.Invoke(options); var registry = new AspNetHandlerRegistry(services); var factory = new AspNetHandlerFactory(services); registry.RegisterFromAssemblies(options.DiscoverQueriesAndHandlersFromAssemblies); var builder = QueryProcessorBuilder.With() .Handlers(registry, factory, registry, factory) .QueryContextFactory(options.QueryContextFactory); var queryProcessor = builder.Build(); services.AddSingleton(queryProcessor); return((QueryProcessorBuilder)builder); }
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); }
private void ConfigureDarker() { var queryProcessor = QueryProcessorBuilder.With() .SimpleInjectorHandlers(_container, opts => opts .WithQueriesAndHandlersFromAssembly(typeof(GetTodoHandler).GetTypeInfo().Assembly)) .InMemoryQueryContextFactory() .JsonRequestLogging() .Build(); _container.RegisterSingleton(queryProcessor); }
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(); }
private void ConfigureDarker() { var handlerConfiguration = new SimpleInjectorHandlerConfigurationBuilder(_container) .WithQueriesAndHandlersFromAssembly(typeof(GetTodoHandler).GetTypeInfo().Assembly) .Build(); var queryProcessor = QueryProcessorBuilder.With() .Handlers(handlerConfiguration) .DefaultPolicies() .InMemoryRequestContextFactory() .NewtonsoftJsonSerializer() .Build(); _container.RegisterSingleton <IQueryProcessor>(queryProcessor); }
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); }); }
private void RegisterQueryProcessor() { 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 Paramore.Darker.Policies.PolicyRegistry { { Paramore.Darker.Policies.Constants.RetryPolicyName, retryPolicy }, { Paramore.Darker.Policies.Constants.CircuitBreakerPolicyName, circuitBreakerPolicy } }; var queryProcessor = QueryProcessorBuilder.With() .SimpleInjectorHandlers(_container, opts => opts.WithQueriesAndHandlersFromAssembly(typeof(GreetingsAllQuery).Assembly)) .InMemoryQueryContextFactory() .Policies(policyRegistry) .JsonQueryLogging() .Build(); _container.Register <IQueryProcessor>(() => queryProcessor, Lifestyle.Singleton); }
public void HandlersGetWiredWithSimpleInjectors() { var container = new Container(); var queryProcessor = QueryProcessorBuilder.With() .SimpleInjectorHandlers(container, opts => opts.WithQueriesAndHandlersFromAssembly(typeof(TestQueryHandler).Assembly)) .InMemoryQueryContextFactory() .Build(); container.RegisterInstance(queryProcessor); var resolvedQueryProcessor = container.GetInstance <IQueryProcessor>(); resolvedQueryProcessor.ShouldNotBeNull(); var id = Guid.NewGuid(); var result = resolvedQueryProcessor.Execute(new TestQueryA(id)); result.ShouldBe(id); }
private void RegisterQueryProcessor() { 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 Paramore.Darker.Policies.PolicyRegistry { { Paramore.Darker.Policies.Constants.RetryPolicyName, retryPolicy }, { Paramore.Darker.Policies.Constants.CircuitBreakerPolicyName, circuitBreakerPolicy } }; Func <Type, object> simpleFactory = type => _container.GetInstance(type); IQueryProcessor queryProcessor = QueryProcessorBuilder.With() .SimpleInjectorHandlers(_container, opts => opts.WithQueriesAndHandlersFromAssembly(typeof(NewShipRegistrationHandlerAsync).Assembly)) .InMemoryQueryContextFactory() .Policies(policyRegistry) .JsonQueryLogging() .Build(); _container.RegisterInstance <IQueryProcessor>(queryProcessor); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { var container = new ServiceContainer { ScopeManagerProvider = new StandaloneScopeManagerProvider() }; // Configure and register Serilog. Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.LiterateConsole() .CreateLogger(); container.RegisterInstance(Log.Logger); // Configure and register Darker. var queryProcessor = QueryProcessorBuilder.With() .LightInjectHandlers(container, opts => opts .WithQueriesAndHandlersFromAssembly(typeof(GetPeopleQueryHandler).GetTypeInfo().Assembly)) .InMemoryQueryContextFactory() .JsonQueryLogging() .Policies(ConfigurePolicies()) .Build(); container.RegisterInstance(queryProcessor); // Don't forget to register the required decorators. todo maybe find a way to auto-discover these container.Register(typeof(QueryLoggingDecorator <,>)); container.Register(typeof(RetryableQueryDecorator <,>)); container.Register(typeof(FallbackPolicyDecorator <,>)); // Add framework services. services.AddMvc(); return(container.CreateServiceProvider(services)); }
public static IDarkerHandlerBuilder AddDarker(this IServiceCollection services, Action <DarkerOptions> configure = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } var options = new DarkerOptions(); configure?.Invoke(options); var registry = new AspNetHandlerRegistry(services, options.HandlerLifetime); var factory = new AspNetHandlerFactory(services); var builder = QueryProcessorBuilder.With() .Handlers(registry, factory, registry, factory) .QueryContextFactory(options.QueryContextFactory); var queryProcessor = builder.Build(); services.AddSingleton(queryProcessor); return(new AspNetHandlerBuilder(services, registry, (QueryProcessorBuilder)builder)); }
private static IQueryProcessor BuildDarker(IServiceProvider serviceProvider) { var options = serviceProvider.GetService <DarkerOptions>(); var handlerFactory = new ServiceProviderHandlerFactory(serviceProvider); var handlerRegistry = serviceProvider.GetService <ServiceCollectionHandlerRegistry>(); var decoratorFactory = new ServiceProviderHandlerDecoratorFactory(serviceProvider); var decoratorRegistry = serviceProvider.GetService <ServiceCollectionDecoratorRegistry>(); var contextBag = serviceProvider.GetService <DarkerContextBag>(); var builder = QueryProcessorBuilder.With() .Handlers(handlerRegistry, handlerFactory, decoratorRegistry, decoratorFactory) .QueryContextFactory(options.QueryContextFactory) as QueryProcessorBuilder; if (builder == null) { throw new ConfigurationException("Could not build a QueryProcessorBuilder"); } builder.AddContextBag(contextBag); return(builder.Build()); }