public async Task MultipleEventHandlersTest() { bool finished = false; var serviceCollection = new ServiceCollection(); var names = new List <string>(); serviceCollection.AddSingleton(names); var messageHandlerProvider = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var eventPublisher = new RabbitEventBus(connectionFactory, serializer, messageHandlerProvider, "RabbitMQMessageBusTests.SimpleEventHandlerTest")) using (var eventSubscriber = new RabbitEventBus(connectionFactory, serializer, messageHandlerProvider, "RabbitMQMessageBusTests.SimpleEventHandlerTest")) { eventSubscriber.MessageAcknowledged += (x, y) => finished = true; eventSubscriber.Subscribe <NameChangedEvent, NameChangedEventHandler>(); eventSubscriber.Subscribe <NameChangedEvent, NameChangedEventHandler2>(); await eventPublisher.PublishAsync(new NameChangedEvent("daxnet")); while (!finished) { ; } Assert.Equal(2, names.Count); Assert.Equal("daxnet", names[0]); Assert.Equal("daxnet2", names[1]); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var rabbitHost = this.Configuration["rabbit:host"]; var exchangeName = this.Configuration["rabbit:exchange"]; var queueName = this.Configuration["rabbit:queue"]; var connectionFactory = new ConnectionFactory { HostName = rabbitHost }; var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(services, x => x.BuildServiceProvider()); var messageSerializer = new MessageJsonSerializer(); var eventSubscriber = new RabbitEventBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, exchangeName, ExchangeType.Topic); eventSubscriber.Subscribe <AccountAuthenticatedEvent, AccountAuthenticatedEventHandler>(); services.AddSingleton <IEventSubscriber>(eventSubscriber); services .AddApworks() .WithDataServiceSupport(new DataServiceConfigurationOptions(sp => new EntityFrameworkRepositoryContext(new AuditingDataContext()))) .Configure(); }
public void SimpleCQRSScenarioTest1() { var serviceCollection = new ServiceCollection(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); var changedName = string.Empty; var eventStore = new DictionaryEventStore(); var eventBus = new SimpleEventBus(new MessageJsonSerializer(), messageHandlerExecutionContext); var snapshotProvider = new SuppressedSnapshotProvider(); eventBus.MessageReceived += (s, e) => { if (e.Message is NameChangedEvent) { changedName = (e.Message as NameChangedEvent).Name; } }; var domainRepository = new EventSourcingDomainRepository(eventStore, eventBus, snapshotProvider); var id = Guid.NewGuid(); var model = new Employee(id); model.ChangeName("daxnet"); domainRepository.Save <Guid, Employee>(model); Assert.Equal("daxnet", changedName); }
public void PublishMessageTest() { int numOfMessagesReceived = 0; bool finished = false; var serviceCollection = new ServiceCollection(); var names = new List <string>(); serviceCollection.AddSingleton(names); var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var bus = new RabbitMessageBus(connectionFactory, serializer, executionContext, "RabbitMQMessageBusTests.PublishMessageTest", queueName: "RabbitMQMessageBusTests.PublishMessageTestQueue")) { // When any message received, increase the counter bus.MessageReceived += (x, y) => numOfMessagesReceived++; // When any message acknowledged, stop waiting the do the assertion. bus.MessageAcknowledged += (x, y) => finished = true; bus.Subscribe <NameChangedEvent, NameChangedEventHandler>(); var event1 = new NameChangedEvent("daxnet"); bus.Publish(event1); while (!finished) { ; } } Assert.Equal(1, numOfMessagesReceived); }
public void SaveAggregateRootAndSubscribeEventTest() { var serviceCollection = new ServiceCollection(); var names = new List <string>(); serviceCollection.AddSingleton(names); var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name)) using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer)) using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider)) { int eventsReceived = 0; var ackCnt = 0; var subscriber = (IEventSubscriber)eventPublisher; subscriber.MessageReceived += (a, b) => eventsReceived++; subscriber.MessageAcknowledged += (x, y) => ackCnt++; subscriber.Subscribe <NameChangedEvent, NameChangedEventHandler>(); subscriber.Subscribe <TitleChangedEvent, TitleChangedEventHandler>(); var aggregateRootId = Guid.NewGuid(); var employee = new Employee(aggregateRootId); employee.ChangeName("daxnet"); employee.ChangeTitle("developer"); repository.Save <Guid, Employee>(employee); while (ackCnt < 2) { ; } Assert.Equal(2, eventsReceived); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); var mongoServer = this.Configuration["mongo:server"]; var mongoPort = Convert.ToInt32(this.Configuration["mongo:port"]); var mongoDatabase = this.Configuration["mongo:db"]; var rabbitHost = this.Configuration["rabbit:host"]; var exchangeName = this.Configuration["rabbit:exchange"]; var connectionFactory = new ConnectionFactory { HostName = rabbitHost }; var messageSerializer = new MessageJsonSerializer(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(services, x => x.BuildServiceProvider()); var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, exchangeName, ExchangeType.Topic); services.AddSingleton <IEventPublisher>(eventPublisher); services.AddApworks() // .WithEventPublisher(new EventBus(new ConnectionFactory { HostName = rabbitHost }, messageSerializer, exchangeName, ExchangeType.Topic)) .WithDataServiceSupport(new DataServiceConfigurationOptions (new MongoRepositoryContext (new MongoRepositorySettings(mongoServer, mongoPort, mongoDatabase)))) .Configure(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Reads the host machine which runs the RabbitMQ. var rabbitHost = this.Configuration["rabbit:host"]; // Reads the RabbitMQ exchange setting. var rabbitExchangeName = this.Configuration["rabbit:exchange"]; // Reads the name of the event queue. var rabbitEventQueueName = this.Configuration["rabbit:eventQueue"]; // Reads the name of the command queue. var rabbitCommandQueueName = this.Configuration["rabbit:commandQueue"]; // Reads connection string of the event store database. var eventStoreConnectionString = this.Configuration["mssql:event.db"]; // Reads connection string of the query database. var queryDatabaseConnectionString = this.Configuration["mssql:query.db"]; // Event/Command subscribers/publishers var connectionFactory = new ConnectionFactory { HostName = rabbitHost }; var messageSerializer = new MessageJsonSerializer(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(services, x => x.BuildServiceProvider()); var eventSubscriber = new RabbitEventBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, rabbitExchangeName, ExchangeType.Topic, rabbitEventQueueName); eventSubscriber.Subscribe <AccountCreatedEvent, AccountCreatedEventHandler>(); eventSubscriber.Subscribe <TextContentChangedEvent, TextContentChangedEventHandler>(); var commandSender = new RabbitCommandBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, rabbitExchangeName, ExchangeType.Topic); var commandSubscriber = new RabbitCommandBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, rabbitExchangeName, ExchangeType.Topic, rabbitCommandQueueName); commandSubscriber.Subscribe <PostTextCommand, PostTextCommandHandler>(); services.AddSingleton <IEventSubscriber>(eventSubscriber); services.AddSingleton <ICommandSender>(commandSender); services.AddSingleton <ICommandSubscriber>(commandSubscriber); // Domain Repository var eventStorageConfig = new AdoNetEventStoreConfiguration(eventStoreConnectionString); var objectSerializer = new ObjectJsonSerializer(); services.AddSingleton <ISnapshotProvider, SuppressedSnapshotProvider>(); services.AddTransient <IEventPublisher>(x => new RabbitEventBus(connectionFactory, messageSerializer, messageHandlerExecutionContext, rabbitExchangeName, ExchangeType.Topic)); services.AddTransient <IEventStore>(x => new SqlServerEventStore(eventStorageConfig, objectSerializer)); services.AddTransient <IDomainRepository, EventSourcingDomainRepository>(); // Add framework services. services.AddMvc(); }
public void PublishMessageTest() { var serviceCollection = new ServiceCollection(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); var mb = new SimpleMessageBus(new MessageJsonSerializer(), messageHandlerExecutionContext); int numOfMessagesReceived = 0; mb.MessageReceived += (x, y) => numOfMessagesReceived++; var event1 = new NameChangedEvent("daxnet"); mb.Publish(event1); Assert.Equal(1, numOfMessagesReceived); }
public void MessagePublishAndHandleWithEventHandlerTest() { var serviceCollection = new ServiceCollection(); var list = new List <string>(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); serviceCollection.AddSingleton(list); var eventBus = new SimpleEventBus(new MessageJsonSerializer(), messageHandlerExecutionContext); eventBus.Subscribe <NameChangedEvent, NameChangedEventHandler>(); eventBus.Publish(new NameChangedEvent("myName")); Assert.Single(list); Assert.Equal("myName", list[0]); }
public void SaveAggregateRootTest() { var serviceCollection = new ServiceCollection(); var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name)) using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer)) using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider)) { var aggregateRootId = Guid.NewGuid(); var employee = new Employee(aggregateRootId); employee.ChangeName("daxnet"); employee.ChangeTitle("developer"); Assert.Equal(3, employee.Version); repository.Save <Guid, Employee>(employee); Assert.Equal(3, employee.Version); } }
public void PublishMultipleMessagesTest() { var serviceCollection = new ServiceCollection(); var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); var mb = new SimpleMessageBus(new MessageJsonSerializer(), messageHandlerExecutionContext); int numOfMessagesReceived = 0; mb.MessageReceived += (x, y) => numOfMessagesReceived++; var events = new List <IMessage> { new NameChangedEvent("daxnet"), new TitleChangedEvent("SE"), new RegisteredEvent() }; mb.PublishAll(events); Assert.Equal(3, numOfMessagesReceived); }
public void GetByVersionTest1() { var serviceCollection = new ServiceCollection(); var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name)) using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer)) using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider)) { var aggregateRootId = Guid.NewGuid(); var employee = new Employee(aggregateRootId); employee.ChangeName("daxnet"); employee.ChangeTitle("developer"); employee.Register(); repository.Save <Guid, Employee>(employee); var employee2 = repository.GetById <Guid, Employee>(aggregateRootId, 2); // Load 2 events as the first one is the aggregate created event. Assert.Equal(employee.Name, employee2.Name); Assert.Null(employee2.Title); Assert.Equal(DateTime.MinValue, employee2.DateRegistered); } }
public void EventSequenceAfterSaveTest() { var serviceCollection = new ServiceCollection(); var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection); using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name)) using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer)) using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider)) { var aggregateRootId = Guid.NewGuid(); var employee = new Employee(aggregateRootId); employee.ChangeName("daxnet"); employee.ChangeTitle("developer"); repository.Save <Guid, Employee>(employee); var events = eventStore.Load <Guid>(typeof(Employee).AssemblyQualifiedName, aggregateRootId).ToList(); Assert.Equal(3, events.Count); Assert.Equal(1, (events[0] as IDomainEvent).Sequence); Assert.Equal(2, (events[1] as IDomainEvent).Sequence); Assert.Equal(3, (events[2] as IDomainEvent).Sequence); } }