Exemplo n.º 1
0
        public void Setup()
        {
            var host = new HostBuilder()
                       .ConfigureServices((hostContext, services) =>
            {
                //DbContext
                services.AddDbContext <OrderContext>(o => o.UseSqlServer(TestSettings.EventuateDB));
                // Kafka Transport
                services.AddEventuateTramSqlKafkaTransport(TestSettings.EventuateTramDbSchema, TestSettings.KafkaBootstrapServers, EventuateKafkaConsumerConfigurationProperties.Empty(),
                                                           (provider, o) =>
                {
                    var applicationDbContext = provider.GetRequiredService <OrderContext>();
                    applicationDbContext.Database.Migrate();
                    o.UseSqlServer(applicationDbContext.Database.GetDbConnection());
                });
                // Publisher
                services.AddEventuateTramEventsPublisher();
                // Consumer
                services.AddSingleton <TestEventConsumer>();
                // Dispatcher
                services.AddEventuateTramDomainEventDispatcher(Guid.NewGuid().ToString(),
                                                               provider => DomainEventHandlersBuilder.ForAggregateType(typeof(Order).Name)
                                                               .OnEvent <OrderCreatedEvent, TestEventConsumer>()
                                                               .OnEvent <OrderCancelledEvent, TestEventConsumer>()
                                                               .OnEvent <OrderRejectedEvent, TestEventConsumer>()
                                                               .OnEvent <OrderApprovedEvent, TestEventConsumer>()
                                                               .Build());
                // Repository
                services.AddTransient <IOrderRepository, OrderRepository>();
            }).Build();

            host.StartAsync().Wait();

            //Services
            domainEventPublisher = host.Services.GetService <IDomainEventPublisher>();
            consumer             = host.Services.GetService <TestEventConsumer>();
            var orderRepository = host.Services.GetService <IOrderRepository>();

            orderService = new OrderService.Service.OrderService(orderRepository, domainEventPublisher);
            //Initialize Money
            Money orderTotal = new Money("12.10");

            orderDetails = new OrderDetails(1, orderTotal);
        }