コード例 #1
0
        /// <summary>
        /// 传入一个 dto 对象插入数据。dto 需要与实体建立映射关系,否则会报错
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="dbContext"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static TEntity InsertFromDto <TEntity, TDto>(this IDbContext dbContext, TDto dto)
        {
            /*
             * 支持自动设置 主键=guid
             * 支持自动设置 CreationTime=DateTime.Now
             * 支持自动设置 IsDeleted=false
             */

            Utils.CheckNull(dto);

            TEntity entity = AceMapper.Map <TEntity>(dto);

            Type           entityType     = typeof(TEntity);
            TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(entityType);

            /* 设置 主键=guid */
            if (typeDescriptor.PrimaryKeys.Count < 2) /* 只有无主键或单一主键的时候 */
            {
                MappingMemberDescriptor primaryKeyDescriptor = typeDescriptor.PrimaryKeys.FirstOrDefault();
                if (primaryKeyDescriptor != null && primaryKeyDescriptor.IsAutoIncrement == false)
                {
                    var keyValue = primaryKeyDescriptor.GetValue(entity);
                    if (keyValue.IsDefaultValueOfType(primaryKeyDescriptor.MemberInfoType) || string.Empty.Equals(keyValue))
                    {
                        /* 如果未设置主键值,则自动设置为 guid */
                        if (primaryKeyDescriptor.MemberInfoType == typeof(string))
                        {
                            primaryKeyDescriptor.SetValue(entity, IdHelper.CreateGuid());
                        }
                        else if (primaryKeyDescriptor.MemberInfoType.GetUnderlyingType() == typeof(Guid))
                        {
                            primaryKeyDescriptor.SetValue(entity, Guid.NewGuid());
                        }
                    }
                }
            }


            /* 设置 CreationTime=DateTime.Now */
            SetValueIfNeeded(entity, typeDescriptor, "CreationTime", DateTime.Now);

            /* 设置 IsDeleted=false */
            SetValueIfNeeded(entity, typeDescriptor, "IsDeleted", false);

            return(dbContext.Insert(entity));
        }
コード例 #2
0
        //[ValidateAntiForgeryToken]
        public ActionResult Add(AddRoleInput input)
        {
            input.Validate();
            var      services = CreateService <IAccountAppService>();
            Sys_Role role     = new Sys_Role();

            AceMapper.Map(input, role);
            role.CreateUserId = CurrentSession.UserId;
            role.CreateTime   = DateTime.Now;
            services.Insert(role);
            string[] permissionIds = input.GetPermissionIds();
            List <Sys_RoleAuthorize> roleAuthorizeEntitys = CreateRoleAuthorizes(role.Id, permissionIds);

            foreach (var roleAuthorizeEntity in roleAuthorizeEntitys)
            {
                services.Insert(roleAuthorizeEntity);
            }

            return(AddSuccessMsg());
        }
コード例 #3
0
        public void Add(AddRoleInput input)
        {
            input.Validate();
            Sys_Role role = this.CreateEntity <Sys_Role>();

            AceMapper.Map(input, role);

            string[] permissionIds = input.GetPermissionIds();
            List <Sys_RoleAuthorize> roleAuthorizeEntitys = this.CreateRoleAuthorizes(role.Id, permissionIds);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Insert(role);

                foreach (var roleAuthorizeEntity in roleAuthorizeEntitys)
                {
                    this.DbContext.Insert(roleAuthorizeEntity);
                }
            });
        }
コード例 #4
0
        //[ValidateAntiForgeryToken]
        public ActionResult Update(UpdateRoleInput input)
        {
            input.Validate();

            var      services = CreateService <IAccountAppService>();
            Sys_Role role     = services.GetById <Sys_Role>(input.Id);

            role.NotNull("角色不存在");

            AceMapper.Map(input, role);
            string[] permissionIds = input.GetPermissionIds();
            List <Sys_RoleAuthorize> roleAuthorizeEntitys = CreateRoleAuthorizes(role.Id, permissionIds);

            role.LastModifyTime   = DateTime.Now;
            role.LastModifyUserId = CurrentSession.UserId;
            services.Update(role);
            services.DeleteSys_RoleAuthorize(role.Id);
            foreach (var roleAuthorizeEntity in roleAuthorizeEntitys)
            {
                services.Insert(roleAuthorizeEntity);
            }
            return(UpdateSuccessMsg());
        }