当前位置: 首页>>代码示例>>C#>>正文


C# UnitOfWorkScope.Commit方法代码示例

本文整理汇总了C#中UnitOfWorkScope.Commit方法的典型用法代码示例。如果您正苦于以下问题:C# UnitOfWorkScope.Commit方法的具体用法?C# UnitOfWorkScope.Commit怎么用?C# UnitOfWorkScope.Commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnitOfWorkScope的用法示例。


在下文中一共展示了UnitOfWorkScope.Commit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Can_attach

        public void Can_attach()
        {
            var customer = new Customer
            {
                FirstName = "Jane",
                LastName = "Doe"
            };
            var session = NHTestUtil.OrdersDomainFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.Save(customer);
            transaction.Commit();
            session.Evict(customer); //Detching from owning session
            session.Dispose(); //Auto flush

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new NHRepository<Customer,int>();
                repository.Attach(customer);
                customer.LastName = "Changed";
                scope.Commit(); //Should change since the customer was attached to repository.
            }

            using (var testData = new NHTestData(NHTestUtil.OrdersDomainFactory.OpenSession()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.IsNotNull(savedCustomer);
                Assert.AreEqual(savedCustomer.LastName, "Changed");
            }
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:30,代码来源:NHRepositoryQueryTest.cs

示例2: Test_Can_Get_MonthlySalesSummary_With_Money_Type

        public void Test_Can_Get_MonthlySalesSummary_With_Money_Type()
        {
            IList<MonthlySalesSummary> report;
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            using (var scope = new UnitOfWorkScope())
            {
                testData.Batch(action => action.CreateMonthlySalesSummaryForMonth(1));

                var repository = new NHRepository<MonthlySalesSummary>();
                report = (from summary in repository
                          where summary.Month == 1
                          select summary).ToList();

                scope.Commit();
            }

            Assert.That(report, Is.Not.Null);
            Assert.That(report.Count, Is.GreaterThan(0));

            report.ForEach(rep =>
                               {
                                   Assert.That(rep.Month == 1);
                                   Assert.That(rep.TotalSale, Is.Not.Null);
                                   Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0));
                                   Assert.That(rep.TotalSale.Currency, Is.Not.Null);
                               });
        }
开发者ID:Chris-Kim,项目名称:ncommon,代码行数:27,代码来源:CompositeUserTypeBaseTests.cs

示例3: can_commit_multiple_db_operations

        public void can_commit_multiple_db_operations()
        {
            var customer = new Customer { FirstName = "John", LastName = "Doe" };
            var salesPerson = new SalesPerson { FirstName = "Jane", LastName = "Doe", SalesQuota = 2000 };

            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                new EFRepository<SalesPerson>().Add(salesPerson);
                scope.Commit();
            }

            using (var ordersTestData = new EFTestData(OrdersContextProvider()))
            using (var hrTestData = new EFTestData(HRContextProvider()))
            {
                Customer savedCustomer = null;
                SalesPerson savedSalesPerson = null;
                ordersTestData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerID));
                hrTestData.Batch(action => savedSalesPerson = action.GetSalesPersonById(salesPerson.Id));

                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedSalesPerson, Is.Not.Null);
                Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
                Assert.That(savedSalesPerson.Id, Is.EqualTo(salesPerson.Id));
            }
        }
开发者ID:jordanyaker,项目名称:ncommon,代码行数:26,代码来源:EFRepositoryTransactionTests.cs

示例4: Test_Can_Query_MonthlySalesSummary_Based_On_Currency

        public void Test_Can_Query_MonthlySalesSummary_Based_On_Currency()
        {
            IList<MonthlySalesSummary> report;
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            using (var scope = new UnitOfWorkScope())
            {
                testData.Batch(actions => actions.CreateMonthlySalesSummaryWithAmount(
                    new Money{Amount = 100, Currency = "YEN"}
                    ));

                var repository = new NHRepository<MonthlySalesSummary>();
                report = (from summary in repository
                          where summary.TotalSale.Currency == "YEN"
                          select summary).ToList();

                scope.Commit();
            }

            Assert.That(report, Is.Not.Null);
            Assert.That(report.Count, Is.GreaterThan(0));

            report.ForEach(rep =>
            {
                Assert.That(rep.TotalSale, Is.Not.Null);
                Assert.That(rep.TotalSale.Amount, Is.GreaterThan(0));
                Assert.That(rep.TotalSale.Currency, Is.Not.Null);
                Assert.That(rep.TotalSale.Currency, Is.EqualTo("YEN"));
            });
        }
开发者ID:Chris-Kim,项目名称:ncommon,代码行数:29,代码来源:CompositeUserTypeBaseTests.cs

示例5: Can_attach_modified_entity

        public void Can_attach_modified_entity()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe"
            };

            var context = (OrderEntities) OrdersContextProvider();
            context.AddToCustomers(customer);
            #if EF_1_0
            context.SaveChanges(true);
            #else
            context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            #endif
            context.Detach(customer);
            context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                customer.LastName = "Changed";
                var repository = new EFRepository<Customer>();
                repository.Attach(customer);
                scope.Commit();
            }

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.LastName, Is.EqualTo("Changed"));
            }
        }
开发者ID:jordanyaker,项目名称:ncommon,代码行数:34,代码来源:EFRepositoryQueryTests.cs

示例6: ChangePassword

 public virtual void ChangePassword(Guid id, string oldPassword, string newPassword)
 {
     using (var scope = new UnitOfWorkScope())
     {
         _userAccountService.ChangePassword(id,oldPassword,newPassword);
         scope.Commit();
     }
 }
开发者ID:jmptrader,项目名称:WebFrameworkSPA,代码行数:8,代码来源:UserService.cs

示例7: CancelVerification

 public void CancelVerification(string key, out bool accountClosed)
 {
     using (var scope = new UnitOfWorkScope())
     {
         _userAccountService.CancelVerification(key,out accountClosed);
         scope.Commit();
     }
 }
开发者ID:jmptrader,项目名称:WebFrameworkSPA,代码行数:8,代码来源:UserService.cs

示例8: ChangePasswordFromResetKey

 public bool ChangePasswordFromResetKey(string key, string newPassword, out NhUserAccount account)
 {
     bool success;
     using (var scope = new UnitOfWorkScope())
     {
         success=_userAccountService.ChangePasswordFromResetKey(key,newPassword,out account);
         scope.Commit();
     }
     return success;
 }
开发者ID:jmptrader,项目名称:WebFrameworkSPA,代码行数:10,代码来源:UserService.cs

示例9: AddUsersToRoles

        public virtual void AddUsersToRoles(List<Guid> userId, List<string> roleName)
        {
            if (userId == null || userId.Count == 0 || roleName == null || roleName.Count == 0)
                return;
            StringBuilder message=new StringBuilder();
            var users = _userRepository.Query.Where(x => userId.Contains(x.ID)).ToList();
            var roles = _roleRepository.Query.Where(x => roleName.Contains(x.Name)).ToList();
            foreach (var userEntity in users)
            {
                if (userEntity.Roles != null && userEntity.Roles.Any())
                {
                    var newRoles = roles.Except(userEntity.Roles);

                    foreach (var role in newRoles)
                        userEntity.Roles.Add(role);
                    if(newRoles!=null && newRoles.Count()>0)
                        message.AppendFormat("User {0} is added to role(s) {1}.",userEntity.Username,string.Join(",", newRoles.Select(x => x.Name)));
                }
                else
                {
                    foreach (var role in roles)
                        userEntity.Roles.Add(role);
                    if(roles!=null && roles.Count()>0)
                        message.AppendFormat("User {0} is added to role(s) {1}.",userEntity.Username,string.Join(",", roles.Select(x => x.Name)));
                }
                using (var scope = new UnitOfWorkScope())
                {
                    _userRepository.Update(userEntity);
                    scope.Commit();
                }
            }
            foreach (var uid in userId)
            {
                if (!users.Any(u => u.ID == uid))
                {
                    var user = _userRepository.Query.Where(x => x.ID == uid).SingleOrDefault();
                    if (user != null)
                    {
                        user.Roles = roles;
                        using (var scope = new UnitOfWorkScope())
                        {
                            _userRepository.Update(user);
                            scope.Commit();
                        }
                        if(roles!=null && roles.Count()>0)
                            message.AppendFormat("User {0} is added to role(s) {1}.",user.Username,string.Join(",", roles.Select(x => x.Name)));
                    }
                }
            }
            if (message.Length > 0)
            {
                ActivityLog item = new ActivityLog(ActivityType.AddUserToRole.ToString(), message.ToString());
                _activityLogService.Add(item);
            }
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:55,代码来源:RoleService.cs

示例10: Delete_Deletes_Record

        public void Delete_Deletes_Record()
        {
            //Adding a dummy record.
            var newAddress = new Address
            {
                StreetAddress1 = "This record was inserted for deletion",
                City = "Fictional city",
                State = "LA",
                ZipCode = "12345"
            };

            var newCustomer = new Customer
            {
                FirstName = ("John_DELETE_ME_" + DateTime.Now),
                LastName = ("Doe_DELETE_ME_" + DateTime.Now),
                Address = newAddress
            };

            //Re-usable query to query for the matching record.
            var queryForCustomer = new Func<NHRepository<Customer>, Customer>
                (x => (from cust in x
                       where cust.FirstName == newCustomer.FirstName && cust.LastName == newCustomer.LastName
                       select cust).FirstOrDefault()
                );

            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new NHRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Null);

                customerRepository.Add(newCustomer);
                scope.Commit();
            }

            //Retrieve the record for deletion.
            using (var scope = new UnitOfWorkScope())
            {
                var customerRepository = new NHRepository<Customer>();
                var customerToDelete = queryForCustomer(customerRepository);
                Assert.That(customerToDelete, Is.Not.Null);
                customerRepository.Delete(customerToDelete);
                scope.Commit();
            }

            //Ensure customer record is deleted.
            using (new UnitOfWorkScope())
            {
                var customerRepository = new NHRepository<Customer>();
                var recordCheckResult = queryForCustomer(customerRepository);
                Assert.That(recordCheckResult, Is.Null);
            }
        }
开发者ID:Chris-Kim,项目名称:ncommon,代码行数:53,代码来源:NHRepositoryTests.cs

示例11: Add

        //public virtual bool Exists(string name)
        //{
        //    if (string.IsNullOrEmpty(name))
        //        throw new ArgumentNullException("name");
        //    int count = _repository.Query.Where(x => x.Name == name.Trim()).Count();
        //    return count > 0 ? true : false;
        //}
        /// <summary>
        /// Adds a item
        /// </summary>
        /// <param name="item">item</param>
        public virtual void Add(ActivityLog item)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            using (var scope = new UnitOfWorkScope())
            {
                _repository.Add(item);
                scope.Commit();
            }

            //event notification
            _eventPublisher.EntityInserted(item);
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:24,代码来源:ActivityLogService.cs

示例12: Delete

        public virtual bool Delete(Guid id)
        {
            if (id == default(Guid))
                throw new ArgumentNullException("id");
            ActivityLog item = _repository.Query.FirstOrDefault(x => x.Id == id);
            if (item == null)
                return false;
            using (var scope = new UnitOfWorkScope())
            {
                _repository.Delete(item);
                scope.Commit();
            }

            //event notification
            _eventPublisher.EntityDeleted(item);
            return true;
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:17,代码来源:ActivityLogService.cs

示例13: Add

        /// <summary>
        /// Adds a item
        /// </summary>
        /// <param name="item">item</param>
        public virtual void Add(ScheduleTask item)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            using (var scope = new UnitOfWorkScope())
            {
                _repository.Add(item);
                scope.Commit();
            }

            //event notification
            _eventPublisher.EntityInserted(item);
            TaskManager.Instance.Refresh();
            StringBuilder message = new StringBuilder();
            message.AppendFormat("Task {0} is created.", item.Name);
            ActivityLog activityItem = new ActivityLog(ActivityType.AddTask.ToString(), message.ToString());
            _activityLogService.Add(activityItem);
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:22,代码来源:TaskService.cs

示例14: can_commit

        public void can_commit()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe"
            };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer,int>()
                    .Add(customer);
                scope.Commit();
            }

            var savedCustomer = new EFRepository<Customer, int>().Query
                .First(x => x.CustomerID == customer.CustomerID);
            Assert.IsNotNull(savedCustomer);
        }
开发者ID:jmptrader,项目名称:WebFrameworkMVC,代码行数:18,代码来源:EFRepositoryTransactionTest.cs

示例15: NHUOW_Issue_6_Replication

        public void NHUOW_Issue_6_Replication()
        {
            var readCustomerFunc = new Func<Customer>(() =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var customer = new NHRepository<Customer>().First();
                    scope.Commit();
                    return customer;
                }
            });

            var updateCustomerFunc = new Func<Customer, Customer>(customer =>
            {
                using (var scope = new UnitOfWorkScope())
                {
                    var repository = new NHRepository<Customer>();
                    repository.Attach(customer);
                    scope.Commit();
                    return customer;
                }
            });

            var newCustomerName = "Changed" + new Random().Next(0, int.MaxValue);
            using (var testData = new NHTestDataGenerator(Factory.OpenSession()))
            {
                testData.Batch(actions => actions.CreateCustomer());

                using (var masterScope = new UnitOfWorkScope())
                {
                    using (var childScope = new UnitOfWorkScope(UnitOfWorkScopeTransactionOptions.CreateNew))
                    {
                        var customer = readCustomerFunc();
                        customer.FirstName = newCustomerName;
                        updateCustomerFunc(customer);
                        childScope.Commit();
                    }
                } //Rollback

                var checkCustomer = readCustomerFunc();
                Assert.That(checkCustomer.FirstName, Is.EqualTo(newCustomerName));
            }
        }
开发者ID:Chris-Kim,项目名称:ncommon,代码行数:43,代码来源:NHUnitOfWorkTransactionTests.cs


注:本文中的UnitOfWorkScope.Commit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。