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);
            }
        }
Ejemplo n.º 2
0
 public void AggregateRootVersionTests_SimpleVersionTest()
 {
     SourcedCustomer customer = new SourcedCustomer();
     customer.ChangeName("Qingyang", "Chen");
     customer.ChangeEmail("*****@*****.**");
     Assert.AreEqual<long>(3, customer.Version);
 }
 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);
 }
        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 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());
            }
        }
        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);
            }
        }
        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);
            }
        }
        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.º 9
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_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);
 }