Ejemplo n.º 1
0
 public void AggregateRootVersionTests_SimpleVersionTest()
 {
     SourcedCustomer customer = new SourcedCustomer();
     customer.ChangeName("Qingyang", "Chen");
     customer.ChangeEmail("*****@*****.**");
     Assert.AreEqual<long>(3, customer.Version);
 }
        public void SnapshotDomainRepositoryTests_SaveAggregateRootAndPublishToDirectBusTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_SnapshotDomainRepository_DirectBus;
            IApp application = AppRuntime.Create(configSource);
            application.Initialize += new System.EventHandler<AppInitEventArgs>(Helper.AppInit_Repositories_SnapshotDomainRepository_DirectBus);
            application.Start();

            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            customer.ChangeName("Qingyang", "Chen");
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            int cnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            Assert.AreEqual<int>(1, cnt);
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                SourcedCustomer sourcedCustomer = null;
                sourcedCustomer = domainRepository.Get<SourcedCustomer>(id);
                Assert.AreEqual<string>("Qingyang", sourcedCustomer.FirstName);
                Assert.AreEqual<string>("Chen", sourcedCustomer.LastName);
            }
        }
        public void EventSourcedDomainRepositoryTests_CreateSnapshotTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_EventSourcedDomainRepositoryWithMSMQBusAndSnapshotProvider;
            IApp app = AppRuntime.Create(configSource);
            app.Initialize += Helper.AppInit_Repositories_EventSourcedDomainRepositoryWithMSMQBusAndSnapshotProvider;
            app.Start();
            SourcedCustomer customer = new SourcedCustomer();
            for (int i = 0; i < 4; i++)
            {
                customer.ChangeEmail("acqy" + i.ToString() + "@163.com");
            }
            Assert.AreEqual<long>(5, customer.Version);
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();

                SourcedCustomer cust2 = domainRepository.Get<SourcedCustomer>(customer.ID);
                Assert.AreEqual<long>(5, cust2.Version);
                Assert.AreEqual<string>("*****@*****.**", cust2.Email);

                cust2.ChangeName("sunny", "chen");
                domainRepository.Save(cust2);
                domainRepository.Commit();
            }
            int snapshotCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            Assert.AreEqual<int>(1, snapshotCnt);
            DataTable dt = Helper.ReadRecordsFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            DataRow dr = dt.Rows[0];
            Assert.AreEqual<string>(typeof(SourcedCustomer).AssemblyQualifiedName, dr["AggregateRootType"].ToString());
            Assert.AreEqual<Guid>(customer.ID, (Guid)(dr["AggregateRootID"]));
        }
Ejemplo n.º 4
0
 public void DomainEvents_ApplyDomainEventTest()
 {
     SourcedCustomer sourcedCustomer = new SourcedCustomer();
     Assert.AreEqual<string>("daxnet", sourcedCustomer.Username);
     Assert.AreEqual<string>("Sunny", sourcedCustomer.FirstName);
     Assert.AreEqual<string>("Chen", sourcedCustomer.LastName);
     sourcedCustomer.ChangeName("sunny", "chen");
     Assert.AreEqual<string>("sunny", sourcedCustomer.FirstName);
     Assert.AreEqual<string>("chen", sourcedCustomer.LastName);
     Assert.AreEqual<int>(2, sourcedCustomer.UncommittedEvents.Count());
 }
 public void RegularEventPublisherDomainRepositoryTests_SaveAggregateRootTest()
 {
     SourcedCustomer customer = new SourcedCustomer();
     Guid id = customer.ID;
     customer.ChangeName("Qingyang", "Chen");
     customer.ChangeEmail("*****@*****.**");
     using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
     {
         domainRepository.Save<SourcedCustomer>(customer);
         domainRepository.Commit();
     }
     int msgCnt = Helper.GetMessageQueueCount(Helper.EventBus_MessageQueue);
     Assert.AreEqual<int>(3, msgCnt);
     int recordCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
     Assert.AreEqual<int>(1, recordCnt);
 }
Ejemplo n.º 6
0
        public void Buses_CommandBus_ChangeEmailFor1005TimesTest()
        {
            SourcedCustomer customer = new SourcedCustomer();
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save(customer);
                domainRepository.Commit();
            }

            ChangeEmailCommand cmd = new ChangeEmailCommand(customer.ID, "*****@*****.**");
            using (ICommandBus bus = application.ObjectContainer.GetService<ICommandBus>())
            {
                bus.Publish(cmd);
                bus.Commit();
            }
        }
        public void EventSourcedDomainRepositoryTests_SaveAggregateRootTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_EventSourcedDomainRepositoryWithMSMQBusButWithoutSnapshotProvider;
            IApp app = AppRuntime.Create(configSource);
            app.Initialize += Helper.AppInit_Repositories_EventSourcedDomainRepositoryWithMSMQBusButWithoutSnapshotProvider;
            app.Start();

            SourcedCustomer customer = new SourcedCustomer();
            customer.ChangeName("sunny", "chen");
            Assert.AreEqual<long>(2, customer.Version);
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            Assert.AreEqual<long>(2, customer.Version);
            int recordCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_DomainEvents);
            Assert.AreEqual<int>(2, recordCnt);
            int msgCnt = Helper.GetMessageQueueCount(Helper.EventBus_MessageQueue);
            Assert.AreEqual<int>(2, msgCnt);
        }
        public void RegularDomainRepositoryTests_SaveAggregateRootTest()
        {
            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            customer.ChangeEmail("*****@*****.**");
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();

                int cnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
                Assert.AreEqual<int>(1, cnt);
                SourcedCustomer customer2 = null;

                customer2 = domainRepository.Get<SourcedCustomer>(id);
                Assert.IsNotNull(customer2);
                Assert.AreEqual<string>("Sunny", customer2.FirstName);
                Assert.AreEqual<string>("Chen", customer2.LastName);
                Assert.AreEqual<string>("*****@*****.**", customer2.Email);
            }
        }
        public void AggregateRootVersionTests_AlongWithEventSourcedDomainRepositoryTest()
        {
            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            customer.ChangeName("Qingyang", "Chen");
            customer.ChangeEmail("*****@*****.**");
            Assert.AreEqual<long>(3, customer.Version);
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            Assert.AreEqual<long>(3, customer.Version);
            Assert.AreEqual<int>(0, customer.UncommittedEvents.Count());

            SourcedCustomer customer2 = null;
            using (IDomainRepository domainRepository2 = application.ObjectContainer.GetService<IDomainRepository>())
            {
                customer2 = domainRepository2.Get<SourcedCustomer>(id);
                Assert.AreEqual<long>(3, customer2.Version);
                customer2.ChangeName("Joe", "Li");
                customer2.ChangeEmail("*****@*****.**");
                Assert.AreEqual<long>(5, customer2.Version);
                Assert.AreEqual<int>(2, customer2.UncommittedEvents.Count());
                domainRepository2.Save<SourcedCustomer>(customer2);
                domainRepository2.Commit();
                Assert.AreEqual<long>(5, customer2.Version);
                Assert.AreEqual<int>(0, customer2.UncommittedEvents.Count());
            }

            SourcedCustomer customer3 = null;
            using (IDomainRepository domainRepository3 = application.ObjectContainer.GetService<IDomainRepository>())
            {
                customer3 = domainRepository3.Get<SourcedCustomer>(id);
                Assert.AreEqual<long>(5, customer3.Version);
                Assert.AreEqual<int>(0, customer3.UncommittedEvents.Count());
            }
        }
Ejemplo n.º 10
0
 public static CreateCustomerDomainEvent[] CreateCreateCustomerDomainEvents()
 {
     var sourcedCustomer1 = new SourcedCustomer { ID = AggregateRootId1 };
     var sourcedCustomer2 = new SourcedCustomer { ID = AggregateRootId2 };
     var sourcedCustomer3 = new SourcedCustomer { ID = AggregateRootId3 };
     var createCustomerEvents = new CreateCustomerDomainEvent[]
     {
         new CreateCustomerDomainEvent()
         {
             ID = Id1,
             Branch = 1,
             Username = "******",
             Timestamp = DateTime.Now,
             Version = 3,
             Source = sourcedCustomer1
         },
         new CreateCustomerDomainEvent()
         {
             ID = Id2,
             Branch = 1,
             Username = "******",
             Timestamp = DateTime.Now,
             Version = 1,
             Source = sourcedCustomer2
         },
         new CreateCustomerDomainEvent()
         {
             ID = Id3,
             Branch = 1,
             Username = "******",
             Timestamp = DateTime.Now,
             Version = 2,
             Source = sourcedCustomer3
         }
     };
     return createCustomerEvents;
 }
        public void EventSourcedDomainRepositoryTests_SaveAndLoadAggregateRootTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_EventSourcedDomainRepositoryWithMSMQBusButWithoutSnapshotProvider;
            IApp app = AppRuntime.Create(configSource);
            app.Initialize += Helper.AppInit_Repositories_EventSourcedDomainRepositoryWithMSMQBusButWithoutSnapshotProvider;
            app.Start();

            SourcedCustomer customer = new SourcedCustomer();
            customer.ChangeName("sunny", "chen");
            customer.ChangeEmail("*****@*****.**");
            Assert.AreEqual<long>(3, customer.Version);
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            Assert.AreEqual<long>(3, customer.Version);

            using (IDomainRepository domainRepository2 = app.ObjectContainer.GetService<IDomainRepository>())
            {
                SourcedCustomer cust = domainRepository2.Get<SourcedCustomer>(customer.ID);
                Assert.AreEqual<long>(3, cust.Version);
                Assert.AreEqual<string>("sunny", cust.FirstName);
                Assert.AreEqual<string>("chen", cust.LastName);
                Assert.AreEqual<string>("*****@*****.**", cust.Email);
            }
        }
Ejemplo n.º 12
0
 public void AggregateRootVersionTests_BuildFromHistoryTest()
 {
     var aggregateRootId = Guid.NewGuid();
     SourcedCustomer sourcedCustomer = new SourcedCustomer { ID = aggregateRootId };
     List<IDomainEvent> historicalEvents = new List<IDomainEvent>();
     historicalEvents.Add(new CreateCustomerDomainEvent
     {
         Source = sourcedCustomer,
         Branch = 0,
         FirstName = "Sunny",
         ID = Helper.AggregateRootId1,
         LastName = "Chen",
         Timestamp = DateTime.UtcNow,
         Username = "******",
         Version = 1
     });
     historicalEvents.Add(new ChangeCustomerNameDomainEvent
     {
         Source = sourcedCustomer,
         Branch = 0,
         FirstName = "Qingyang",
         ID = Helper.AggregateRootId2,
         LastName = "Chen",
         Timestamp = DateTime.UtcNow,
         Version = 2
     });
     historicalEvents.Add(new ChangeEmailDomainEvent
     {
         Source = sourcedCustomer,
         Branch = 0,
         ID = Helper.AggregateRootId3,
         Timestamp = DateTime.UtcNow,
         Email = "*****@*****.**",
         Version = 3
     });
     SourcedCustomer customer = new SourcedCustomer();
     Assert.AreEqual<long>(1, customer.Version);
     customer.BuildFromHistory(historicalEvents);
     Assert.AreEqual<long>(3, customer.Version);
 }
        public void SnapshotDomainRepositoryTests_SaveAggregateRootButFailPublishToMSMQTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_SnapshotDomainRepository_SaveButFailPubToMSMQ;
            IApp application = AppRuntime.Create(configSource);
            application.Initialize += new System.EventHandler<AppInitEventArgs>(Helper.AppInit_Repositories_SnapshotDomainRepository_SaveButFailPubToMSMQ);
            application.Start();

            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            customer.ChangeName("Qingyang", "Chen");
            IDomainRepository domainRepository = null;
            try
            {
                using (domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
                {
                    domainRepository.Save<SourcedCustomer>(customer);
                    domainRepository.Commit();
                }
            }
            catch { }
            int cnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            Assert.AreEqual<int>(0, cnt);
        }
Ejemplo n.º 14
0
        public void RegularDomainRepositoryTests_GetAggregateRootTest()
        {
            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }

            using (IDomainRepository domainRepository2 = application.ObjectContainer.GetService<IDomainRepository>())
            {
                SourcedCustomer customer2 = domainRepository2.Get<SourcedCustomer>(id);
                Assert.AreEqual<string>("Sunny", customer2.FirstName);
                Assert.AreEqual<string>("Chen", customer2.LastName);
                Assert.AreEqual<string>("daxnet", customer2.Username);
            }
        }
Ejemplo n.º 15
0
        public void RegularDomainRepositoryTests_UpdateAggregateRootTest()
        {
            SourcedCustomer customer = new SourcedCustomer();
            Guid id = customer.ID;
            customer.ChangeEmail("*****@*****.**");
            using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();

                int cnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
                Assert.AreEqual<int>(1, cnt);
                customer.ChangeEmail("*****@*****.**");

                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();

                int cnt2 = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
                Assert.AreEqual<int>(1, cnt2);
                DataTable dt = Helper.ReadRecordsFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
                DataRow dr = dt.Rows[0];
                Assert.AreEqual<string>("*****@*****.**", dr["Email"].ToString());
            }
        }
Ejemplo n.º 16
0
 public void DomainEvents_CreateSourcedAggregateRootTest()
 {
     SourcedCustomer sourcedCustomer = new SourcedCustomer();
     Assert.AreNotEqual<Guid>(Guid.Empty, sourcedCustomer.ID);
     Assert.AreEqual<int>(1, sourcedCustomer.UncommittedEvents.Count());
     var uncommittedEvent = sourcedCustomer.UncommittedEvents.First();
     Assert.IsInstanceOfType(uncommittedEvent, typeof(CreateCustomerDomainEvent));
 }
 public void RegularEventPublisherDomainRepositoryTests_SaveAndLoadAggregateRootTest()
 {
     SourcedCustomer cust1 = new SourcedCustomer();
     Guid id1=cust1.ID;
     cust1.ChangeName("Qingyang", "Chen");
     cust1.ChangeEmail("*****@*****.**");
     SourcedCustomer cust2 = new SourcedCustomer();
     Guid id2 = cust2.ID;
     cust2.ChangeName("Jim", "Liu");
     cust2.ChangeEmail("*****@*****.**");
     using (IDomainRepository domainRepository = application.ObjectContainer.GetService<IDomainRepository>())
     {
         domainRepository.Save(cust1);
         domainRepository.Save(cust2);
         domainRepository.Commit();
     }
     int msgCnt = Helper.GetMessageQueueCount(Helper.EventBus_MessageQueue);
     Assert.AreEqual<int>(6, msgCnt);
     int recordCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
     Assert.AreEqual<int>(2, recordCnt);
     SourcedCustomer cust3 = new SourcedCustomer();
     using (IDomainRepository domainRepository2 = application.ObjectContainer.GetService<IDomainRepository>())
     {
         domainRepository2.Save(cust3);
         SourcedCustomer cust4 = domainRepository2.Get<SourcedCustomer>(id1);
         int c = cust4.UncommittedEvents.Count();
         if (c > 0)
         {
             var evt = cust4.UncommittedEvents.FirstOrDefault();
         }
         cust4.ChangeEmail("*****@*****.**");
         domainRepository2.Save(cust4);
         domainRepository2.Commit();
     }
     msgCnt = Helper.GetMessageQueueCount(Helper.EventBus_MessageQueue);
     Assert.AreEqual<int>(8, msgCnt);
     recordCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_SourcedCustomer);
     Assert.AreEqual<int>(3, recordCnt);
 }
        public void EventSourcedDomainRepositoryTests_GetFromSnapshotTest()
        {
            IConfigSource configSource = Helper.ConfigSource_Repositories_EventSourcedDomainRepositoryWithMSMQBusAndSnapshotProvider;
            IApp app = AppRuntime.Create(configSource);
            app.Initialize += Helper.AppInit_Repositories_EventSourcedDomainRepositoryWithMSMQBusAndSnapshotProvider;
            app.Start();
            SourcedCustomer customer = new SourcedCustomer();
            for (int i = 0; i < 4; i++)
            {
                customer.ChangeEmail("acqy" + i.ToString() + "@163.com");
            }
            Assert.AreEqual<long>(5, customer.Version);
            // first to produce 5 events
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            // produce another 1 event to trigger snapshot creation
            customer.ChangeName("qingyang111", "chen");
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            int snapshotCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            Assert.AreEqual<int>(1, snapshotCnt);
            DataTable dt = Helper.ReadRecordsFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            DataRow dr = dt.Rows[0];
            Assert.AreEqual<string>(typeof(SourcedCustomer).AssemblyQualifiedName, dr["AggregateRootType"].ToString());
            Assert.AreEqual<Guid>(customer.ID, (Guid)(dr["AggregateRootID"]));
            Assert.AreEqual<long>(6, Convert.ToInt64(dr["Version"]));
            // produce another 3 events
            for (int i = 0; i < 3; i++)
            {
                customer.ChangeName("sunny" + i.ToString(), "chen");
            }
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                domainRepository.Save<SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            // the snapshot won't be updated...
            snapshotCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            Assert.AreEqual<int>(1, snapshotCnt);
            dt = Helper.ReadRecordsFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_Snapshots);
            dr = dt.Rows[0];
            Assert.AreEqual<string>(typeof(SourcedCustomer).AssemblyQualifiedName, dr["AggregateRootType"].ToString());
            Assert.AreEqual<Guid>(customer.ID, (Guid)(dr["AggregateRootID"]));
            Assert.AreEqual<long>(6, Convert.ToInt64(dr["Version"]));

            int eventCnt = Helper.ReadRecordCountFromSQLExpressCQRSTestDB(Helper.CQRSTestDB_Table_DomainEvents);
            Assert.AreEqual<int>(9, eventCnt);

            // now retrieve
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService<IDomainRepository>())
            {
                SourcedCustomer cust2 = domainRepository.Get<SourcedCustomer>(customer.ID);
                Assert.AreEqual<long>(9, cust2.Version);
                Assert.AreEqual<string>("sunny2", cust2.FirstName);
                Assert.AreEqual<string>("*****@*****.**", cust2.Email);
            }
        }
Ejemplo n.º 19
0
 public void DomainEvents_ApplyDomainEventOnConstructionTest()
 {
     SourcedCustomer sourcedCustomer = new SourcedCustomer();
     Assert.AreEqual<string>("daxnet", sourcedCustomer.Username);
     Assert.AreEqual<int>(1, sourcedCustomer.UncommittedEvents.Count());
 }