예제 #1
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());
            }
        }
        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 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);
        }
        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 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 EventSourcedDomainRepositoryTests_UpdateSnapshotTest()
        {
            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 5 events to trigger snapshot creation
            for (int i = 0; i < 5; i++)
            {
                customer.ChangeName("qingyang" + i.ToString(), "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>(10, Convert.ToInt64(dr["Version"]));

            // produce another 6 events, the snapshot should be updated...
            for (int i = 0; i < 6; i++)
            {
                customer.ChangeName("qingyang", "chen" + i.ToString());
            }
            using (IDomainRepository domainRepository = app.ObjectContainer.GetService <IDomainRepository>())
            {
                domainRepository.Save <SourcedCustomer>(customer);
                domainRepository.Commit();
            }
            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>(16, Convert.ToInt64(dr["Version"]));
        }
 public bool Handle(CreateBookCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         Book book = Book.Create(message.Title, message.Author, message.Description, message.ISBN, message.Pages, message.Inventory);
         domainRepository.Save <Book>(book);
         domainRepository.Commit();
         return(true);
     }
 }
 public bool Handle(RegisterAdminAccountCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         AdminAccount adminAccount = AdminAccount.Create(message.UserName,
                                                         message.Password, message.DisplayName, message.Email);
         domainRepository.Save <AdminAccount>(adminAccount);
         domainRepository.Commit();
         return(true);
     }
 }
 public bool Handle(UpdateBookCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         Book book = domainRepository.Get <Book>(message.AggregateRootId);
         book.UpdateBasicInformation(message.Title, message.Author, message.Description, message.ISBN, message.Pages, message.Inventory);
         domainRepository.Save <Book>(book);
         domainRepository.Commit();
         return(domainRepository.Committed);
     }
 }
 public bool Handle(UpdateUserAccountCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         UserAccount userAccount = domainRepository.Get <UserAccount>(message.AggregateRootId);
         userAccount.UpdateBasicInformation(message.DisplayName, message.Email, message.ContactPhone,
                                            message.ContactAddressCountry, message.ContactAddressState, message.ContactAddressCity,
                                            message.ContactAddressStreet, message.ContactAddressZip);
         domainRepository.Save(userAccount);
         domainRepository.Commit();
         return(domainRepository.Committed);
     }
 }
예제 #11
0
 public override void Handle(ChangeEmailCommand command)
 {
     using (IDomainRepository repository = this.DomainRepository)
     {
         var cust = repository.Get <SourcedCustomer>(command.CustomerId);
         for (int i = 0; i < 1005; i++)
         {
             cust.ChangeEmail(command.NewEmail);
         }
         repository.Save <SourcedCustomer>(cust);
         repository.Commit();
     }
 }
 public bool Handle(ReturnBookCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         UserAccount user = domainRepository.Get <UserAccount>(message.UserAccountAggregateRootId);
         Book        book = domainRepository.Get <Book>(message.BookAggregateRootId);
         IUserAccountBookTransferService bookTransService = this.GetBookTransferService();
         bookTransService.ReturnBookFromUser(user, book);
         domainRepository.Save(user);
         domainRepository.Save(book);
         domainRepository.Commit();
         return(domainRepository.Committed);
     }
 }
        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"]));
        }
 public bool Handle(RegisterUserAccountCommand message)
 {
     using (IDomainRepository domainRepository = this.GetDomainRepository())
     {
         Address address = new Address();
         address.City    = message.ContactAddressCity;
         address.Country = message.ContactAddressCountry;
         address.State   = message.ContactAddressState;
         address.Street  = message.ContactAddressStreet;
         address.Zip     = message.ContactAddressZip;
         UserAccount userAccount = UserAccount.Create(message.UserName,
                                                      message.Password, message.DisplayName, message.Email, message.ContactPhone, address);
         domainRepository.Save <UserAccount>(userAccount);
         domainRepository.Commit();
         return(true);
     }
 }
예제 #15
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();
            }
        }
예제 #16
0
        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);
        }
예제 #17
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);
            }
        }
예제 #18
0
        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);
        }
예제 #19
0
        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());
            }
        }
예제 #20
0
        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);
            }
        }