Наследование: IAmAHandlerFactory
Пример #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add framework serviceProvider.
            services.AddMvc();

            services.AddSingleton <ITaskListRetriever, TaskListRetriever>();
            services.AddSingleton <ITaskRetriever, TaskRetriever>();
            services.AddSingleton <ITasksDAO, TasksDAO>();

            //create handler

            var subscriberRegistry = new SubscriberRegistry();

            services.AddTransient <IHandleRequests <AddTaskCommand>, AddTaskCommandHandler>();
            subscriberRegistry.Register <AddTaskCommand, AddTaskCommandHandler>();

            //complete handler
            services.AddTransient <IHandleRequests <CompleteTaskCommand>, CompleteTaskCommandHandler>();
            subscriberRegistry.Register <CompleteTaskCommand, CompleteTaskCommandHandler>();

            //create policies
            var retryPolicy          = Policy.Handle <Exception>().WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicy = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry       = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            };

            //create message mappers
            services.AddTransient <IAmAMessageMapper <TaskReminderCommand>, TaskReminderCommandMessageMapper>();

            var messagingGatewayConfiguration = RmqGatewayBuilder.With.Uri(new Uri(Configuration["RabbitMQ:Uri"])).Exchange(Configuration["RabbitMQ:Exchange"]).DefaultQueues();

            var gateway = new RmqMessageProducer(messagingGatewayConfiguration);
            IAmAMessageStore <Message> sqlMessageStore = new SqliteMessageStore(new SqliteMessageStoreConfiguration("Data Source = tasks.db", "MessageStores"));

            var container = new Container();

            container.Configure(config =>
            {
                var servicesMessageMapperFactory = new ServicesMessageMapperFactory(container);

                var messageMapperRegistry = new MessageMapperRegistry(servicesMessageMapperFactory)
                {
                    { typeof(TaskReminderCommand), typeof(TaskReminderCommandMessageMapper) },
                    { typeof(TaskAddedEvent), typeof(TaskAddedEventMapper) },
                    { typeof(TaskEditedEvent), typeof(TaskEditedEventMapper) },
                    { typeof(TaskCompletedEvent), typeof(TaskCompletedEventMapper) },
                    { typeof(TaskReminderSentEvent), typeof(TaskReminderSentEventMapper) }
                };

                var servicesHandlerFactory = new ServicesHandlerFactory(container);

                var commandProcessor = CommandProcessorBuilder.With()
                                       .Handlers(new HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                                       .Policies(policyRegistry)
                                       .TaskQueues(new MessagingConfiguration(sqlMessageStore, gateway, messageMapperRegistry))
                                       .RequestContextFactory(new InMemoryRequestContextFactory())
                                       .Build();

                config.For <IAmACommandProcessor>().Singleton().Use(() => commandProcessor);
            });
            container.Populate(services);

            return(container.GetInstance <IServiceProvider>());
        }
Пример #2
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
           // Add framework serviceProvider.
            services.AddMvc();

            services.AddSingleton<ITaskListRetriever, TaskListRetriever>();
            services.AddSingleton<ITaskRetriever, TaskRetriever>();
            services.AddSingleton<ITasksDAO, TasksDAO>();

            //create handler 

            var subscriberRegistry = new SubscriberRegistry();
            services.AddTransient<IHandleRequests<AddTaskCommand>, AddTaskCommandHandler>();
            subscriberRegistry.Register<AddTaskCommand, AddTaskCommandHandler>();

            //complete handler 
            services.AddTransient<IHandleRequests<CompleteTaskCommand>, CompleteTaskCommandHandler>();
            subscriberRegistry.Register<CompleteTaskCommand, CompleteTaskCommandHandler>();

            //create policies
            var retryPolicy = Policy.Handle<Exception>().WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicy = Policy.Handle<Exception>().CircuitBreaker(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry = new PolicyRegistry() { { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy } };

            //create message mappers
            services.AddTransient<IAmAMessageMapper<TaskReminderCommand>, TaskReminderCommandMessageMapper>();
            
            var messagingGatewayConfiguration = RmqGatewayBuilder.With.Uri(new Uri(Configuration["RabbitMQ:Uri"])).Exchange(Configuration["RabbitMQ:Exchange"]).DefaultQueues();

            var gateway = new RmqMessageProducer(messagingGatewayConfiguration);
            IAmAMessageStore<Message> sqlMessageStore = new SqliteMessageStore(new SqliteMessageStoreConfiguration("Data Source = tasks.db", "MessageStores"));

            var container = new Container();
            container.Configure(config =>
            {
                var servicesMessageMapperFactory = new ServicesMessageMapperFactory(container);

                var messageMapperRegistry = new MessageMapperRegistry(servicesMessageMapperFactory)
                    {
                        {typeof(TaskReminderCommand), typeof(TaskReminderCommandMessageMapper)},
                        {typeof(TaskAddedEvent), typeof(TaskAddedEventMapper)},
                        {typeof(TaskEditedEvent), typeof(TaskEditedEventMapper)},
                        {typeof(TaskCompletedEvent), typeof(TaskCompletedEventMapper)},
                        {typeof(TaskReminderSentEvent), typeof(TaskReminderSentEventMapper)}
                    };

                var servicesHandlerFactory = new ServicesHandlerFactory(container);

                var commandProcessor = CommandProcessorBuilder.With()
                    .Handlers(new HandlerConfiguration(subscriberRegistry, servicesHandlerFactory))
                    .Policies(policyRegistry)
                    .TaskQueues(new MessagingConfiguration(sqlMessageStore, gateway, messageMapperRegistry))
                    .RequestContextFactory(new InMemoryRequestContextFactory())
                    .Build();

                config.For<IAmACommandProcessor>().Singleton().Use(() => commandProcessor);

            });
            container.Populate(services);

            return container.GetInstance<IServiceProvider>();
        }