/// <summary>
        /// 将指定的用户从角色中移除。
        /// </summary>
        /// <param name="user">用户实体。</param>
        /// <param name="role">角色实体,若为NULL,则表示从所有角色中移除。</param>
        public void UnassignRole(User user, Role role = null)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            Expression<Func<UserRole, bool>> specExpression;
            if (role == null)
                // ReSharper disable ImplicitlyCapturedClosure
                specExpression = ur => ur.UserID == user.ID;
                // ReSharper restore ImplicitlyCapturedClosure
            else
                specExpression = ur => ur.UserID == user.ID && ur.RoleID == role.ID;

            var userRole = _userRoleRepository.Find(Specification<UserRole>.Eval(specExpression));

            if (userRole == null) return;
            _userRoleRepository.Remove(userRole);
            _repositoryContext.Commit();
        }
        /// <summary>
        /// 通过指定的用户及其所拥有的购物篮实体,创建销售订单。
        /// </summary>
        /// <param name="user">用户实体。</param>
        /// <param name="shoppingCart">购物篮实体。</param>
        /// <returns>销售订单实体。</returns>
        public SalesOrder CreateSalesOrder(User user, ShoppingCart shoppingCart)
        {
            var shoppingCartItems = _shoppingCartItemRepository.FindItemsByCart(shoppingCart);
            var cartItems = shoppingCartItems as IList<ShoppingCartItem> ?? shoppingCartItems.ToList();
            if (shoppingCartItems == null ||
                !cartItems.Any())
                throw new InvalidOperationException("购物篮中没有任何物品。");

            var salesOrder = new SalesOrder {SalesLines = new List<SalesLine>()};

            foreach (var shoppingCartItem in cartItems)
            {
                var salesLine = shoppingCartItem.ConvertToSalesLine();
                salesLine.SalesOrder = salesOrder;
                salesOrder.SalesLines.Add(salesLine);
                _shoppingCartItemRepository.Remove(shoppingCartItem);
            }
            salesOrder.User = user;
            salesOrder.Status = SalesOrderStatus.Paid;
            _salesOrderRepository.Add(salesOrder);
            _repositoryContext.Commit();
            return salesOrder;
        }
 /// <summary>
 /// 将指定的用户赋予特定的角色。
 /// </summary>
 /// <param name="user">用户实体。</param>
 /// <param name="role">角色实体。</param>
 /// <returns>用以表述用户及其角色之间关系的实体。</returns>
 public UserRole AssignRole(User user, Role role)
 {
     if (user == null)
         throw new ArgumentNullException("user");
     if (role == null)
         throw new ArgumentNullException("role");
     UserRole userRole = _userRoleRepository.Find(Specification<UserRole>.Eval(ur => ur.UserID == user.ID));
     if (userRole == null)
     {
         userRole = new UserRole(user.ID, role.ID);
         _userRoleRepository.Add(userRole);
     }
     else
     {
         userRole.RoleID = role.ID;
         _userRoleRepository.Update(userRole);
     }
     _repositoryContext.Commit();
     return userRole;
 }
Пример #4
0
        /// <summary>
        /// 将指定的用户从角色中移除。
        /// </summary>
        /// <param name="user">用户实体。</param>
        /// <param name="role">角色实体,若为NULL,则表示从所有角色中移除。</param>
        public void UnassignRole(User user, Role role = null)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            Expression<Func<UserRole, bool>> specExpression = null;
            if (role == null)
                specExpression = ur => ur.UserID == user.ID;
            else
                specExpression = ur => ur.UserID == user.ID && ur.RoleID == role.ID;

            UserRole userRole = userRoleRepository.Find(Specification<UserRole>.Eval(specExpression));

            if (userRole != null)
            {
                userRoleRepository.Remove(userRole);
                repositoryContext.Commit();
            }
        }