Example #1
0
        /// <summary>
        /// 注册删除多个实体对象(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="ids">标识Id集合</param>
        /// <exception cref="ArgumentNullException">ids为null或长度为0</exception>
        public void RegisterRemoveRange <T>(IEnumerable <Guid> ids) where T : AggregateRootEntity
        {
            #region # 验证参数

            Guid[] entityIds = ids == null ? new Guid[0] : ids.ToArray();

            if (!entityIds.Any())
            {
                throw new ArgumentNullException("ids", string.Format("要删除的{0}的id集合不可为空!", typeof(T).Name));
            }

            #endregion

            LoginInfo loginInfo = null;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            IQueryable <T> entities = this.ResolveRange <T>(x => entityIds.Contains(x.Id));

            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
                entity.Deleted         = true;
                entity.DeletedTime     = DateTime.Now;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
Example #2
0
        /// <summary>
        /// 注册保存实体对象集合
        /// </summary>
        /// <typeparam name="T">聚合根类型</typeparam>
        /// <param name="entities">实体对象集合</param>
        /// <exception cref="ArgumentNullException">实体对象集合</exception>
        /// <exception cref="NullReferenceException">要保存的对象不存在</exception>
        public void RegisterSaveRange <T>(IEnumerable <T> entities) where T : AggregateRootEntity
        {
            #region # 验证参数

            entities = entities == null ? new T[0] : entities.ToArray();

            if (!entities.Any())
            {
                throw new ArgumentNullException("entities", string.Format("要保存的{0}实体对象集合不可为空!", typeof(T).Name));
            }

            #endregion

            LoginInfo loginInfo = null;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
                entity.SavedTime       = DateTime.Now;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
        /// <summary>
        /// 注册删除实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="rowNo">行号</param>
        /// <remarks>逻辑删除</remarks>
        public void RegisterRemove <T>(long rowNo) where T : AggregateRootEntity, IRowable
        {
            T entity = this.ResolveOptional <T>(x => x.RowNo == rowNo);

            #region # 验证

            if (entity == null)
            {
                throw new NullReferenceException($"行号为\"{rowNo}\"的{typeof(T).Name}实体不存在!");
            }

            #endregion

            #region # 设置操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
            }

            #endregion

            entity.Deleted     = true;
            entity.DeletedTime = DateTime.Now;
            EntityEntry entry = this._dbContext.Entry <T>(entity);
            entry.State = EntityState.Modified;
        }
Example #4
0
        /// <summary>
        /// 注册条件删除(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="predicate">条件表达式</param>
        protected void RegisterRemove <T>(Expression <Func <T, bool> > predicate) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate), "条件表达式不可为空!");
            }

            #endregion

            LoginInfo loginInfo = null;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            IQueryable <T> queryable   = this._dbContext.Set <T>().Where(x => !x.Deleted).Where(predicate);
            DateTime       deletedTime = DateTime.Now;

            foreach (T entity in queryable)
            {
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
                entity.Deleted         = true;
                entity.DeletedTime     = deletedTime;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
Example #5
0
        /// <summary>
        /// 注册添加实体对象集合
        /// </summary>
        /// <typeparam name="T">聚合根类型</typeparam>
        /// <param name="entities">实体对象集合</param>
        /// <exception cref="ArgumentNullException">实体对象集合为null或长度为0</exception>
        public void RegisterAddRange <T>(IEnumerable <T> entities) where T : AggregateRootEntity
        {
            #region # 验证参数

            entities = entities == null ? new T[0] : entities.ToArray();

            if (!entities.Any())
            {
                throw new ArgumentNullException("entities", string.Format("要添加的{0}实体对象集合不可为空!", typeof(T).Name));
            }

            #endregion

            #region # 设置创建/操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();

                foreach (T entity in entities)
                {
                    entity.CreatorAccount  = loginInfo == null ? null : loginInfo.LoginId;
                    entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
                }
            }

            #endregion

            this._dbContext.Set <T>().AddRange(entities);
        }
Example #6
0
        /// <summary>
        /// 注册删除单个实体对象(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">实体对象</param>
        public void RegisterRemove <T>(T entity) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity), $"要删除的{typeof(T).Name}实体对象不可为空!");
            }

            #endregion

            #region # 设置操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
            }

            #endregion

            entity.Deleted     = true;
            entity.DeletedTime = DateTime.Now;
            DbEntityEntry entry = this._dbContext.Entry <T>(entity);
            entry.State = EntityState.Modified;
        }
Example #7
0
        /**********Public**********/

        //Register部分

        #region # 注册添加单个实体对象 —— void RegisterAdd<T>(T entity)
        /// <summary>
        /// 注册添加单个实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">新实体对象</param>
        public void RegisterAdd <T>(T entity) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity), $"要添加的{typeof(T).Name}实体对象不可为空!");
            }

            #endregion

            #region # 设置创建/操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.CreatorAccount  = loginInfo?.LoginId;
                entity.CreatorName     = loginInfo?.RealName;
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
            }

            #endregion

            this._dbContext.Set <T>().Add(entity);
        }
Example #8
0
        /// <summary>
        /// 注册添加实体对象集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities">实体对象集合</param>
        public void RegisterAddRange <T>(IEnumerable <T> entities) where T : AggregateRootEntity
        {
            #region # 验证参数

            entities = entities?.ToArray() ?? new T[0];

            if (!entities.Any())
            {
                throw new ArgumentNullException(nameof(entities), $"要添加的{typeof(T).Name}实体对象集合不可为空!");
            }

            #endregion

            #region # 设置创建/操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();

                foreach (T entity in entities)
                {
                    entity.CreatorAccount  = loginInfo?.LoginId;
                    entity.CreatorName     = loginInfo?.RealName;
                    entity.OperatorAccount = loginInfo?.LoginId;
                    entity.OperatorName    = loginInfo?.RealName;
                }
            }

            #endregion

            this._dbContext.Set <T>().AddRange(entities);
        }
Example #9
0
        /// <summary>
        /// 注册保存实体对象集合
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities">实体对象集合</param>
        public void RegisterSaveRange <T>(IEnumerable <T> entities) where T : AggregateRootEntity
        {
            #region # 验证参数

            entities = entities?.ToArray() ?? new T[0];

            if (!entities.Any())
            {
                throw new ArgumentNullException(nameof(entities), $"要保存的{typeof(T).Name}实体对象集合不可为空!");
            }

            #endregion

            LoginInfo loginInfo = null;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            DateTime savedTime = DateTime.Now;

            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
                entity.SavedTime       = savedTime;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
Example #10
0
        /// <summary>
        /// 注册保存单个实体对象
        /// </summary>
        /// <typeparam name="T">聚合根类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <exception cref="ArgumentNullException">实体对象为空</exception>
        /// <exception cref="NullReferenceException">要保存的对象不存在</exception>
        public void RegisterSave <T>(T entity) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (entity == null)
            {
                throw new ArgumentNullException("entity", string.Format("要保存的{0}实体对象不可为空!", typeof(T).Name));
            }

            #endregion

            #region # 设置操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
            }

            #endregion

            entity.SavedTime = DateTime.Now;
            DbEntityEntry entry = this._dbContext.Entry <T>(entity);
            entry.State = EntityState.Modified;
        }
        /// <summary>
        /// 注册删除多个实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entities">实体对象集</param>
        /// <remarks>逻辑删除</remarks>
        public void RegisterRemoveRange <T>(IEnumerable <T> entities) where T : AggregateRootEntity
        {
            #region # 验证

            entities = entities?.ToArray() ?? Array.Empty <T>();
            if (!entities.Any())
            {
                throw new ArgumentNullException(nameof(entities), $"要删除的{typeof(T).Name}的实体对象集不可为空!");
            }

            #endregion

            LoginInfo loginInfo   = null;
            DateTime  deletedTime = DateTime.Now;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
                entity.Deleted         = true;
                entity.DeletedTime     = deletedTime;
                EntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
        /// <summary>
        /// 注册删除多个实体对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="rowNos">行号集</param>
        /// <remarks>逻辑删除</remarks>
        public void RegisterRemoveRange <T>(IEnumerable <long> rowNos) where T : AggregateRootEntity, IRowable
        {
            #region # 验证

            rowNos = rowNos?.ToArray() ?? new long[0];
            if (!rowNos.Any())
            {
                throw new ArgumentNullException(nameof(rowNos), $"要删除的{typeof(T).Name}的行号集不可为空!");
            }

            #endregion

            LoginInfo loginInfo   = null;
            DateTime  deletedTime = DateTime.Now;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            ICollection <T> entities = this.ResolveRange <T>(rowNos);
            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
                entity.Deleted         = true;
                entity.DeletedTime     = deletedTime;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }
        /// <summary>
        /// 获取实体历史
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns>实体历史</returns>
        public IEntityHistory GetEntityHistory <T>(T entity) where T : PlainEntity
        {
            DbEntityEntry <T> entry = this._dbContext.ChangeTracker.Entries <T>().FirstOrDefault(x => x.Entity.Id == entity.Id);

            #region # 验证

            if (entry == null)
            {
                return(null);
            }

            #endregion

            LoginInfo  loginInfo = GetLoginInfo?.Invoke();
            ActionType actualActionType;
            IDictionary <string, object> beforeSnapshot = new Dictionary <string, object>();
            IDictionary <string, object> afterSnapshot  = new Dictionary <string, object>();
            if (entry.State == EntityState.Added)
            {
                actualActionType = ActionType.Create;
                foreach (string propertyName in entry.CurrentValues.PropertyNames)
                {
                    DbPropertyEntry propertyEntry = entry.Property(propertyName);
                    afterSnapshot[propertyName] = propertyEntry.CurrentValue;
                }
            }
            else if (entry.State == EntityState.Modified)
            {
                actualActionType = ActionType.Update;
                foreach (string propertyName in entry.OriginalValues.PropertyNames)
                {
                    DbPropertyEntry propertyEntry = entry.Property(propertyName);
                    if (propertyEntry.OriginalValue?.ToString() != propertyEntry.CurrentValue?.ToString())
                    {
                        beforeSnapshot[propertyName] = propertyEntry.OriginalValue;
                        afterSnapshot[propertyName]  = propertyEntry.CurrentValue;
                    }
                }
            }
            else if (entry.State == EntityState.Deleted)
            {
                actualActionType = ActionType.Delete;
                foreach (string propertyName in entry.OriginalValues.PropertyNames)
                {
                    DbPropertyEntry propertyEntry = entry.Property(propertyName);
                    beforeSnapshot[propertyName] = propertyEntry.OriginalValue;
                }
            }
            else
            {
                return(null);
            }

            EntityHistory entityHistory = new EntityHistory(actualActionType, typeof(T), entry.Entity.Id, beforeSnapshot, afterSnapshot, loginInfo?.LoginId, loginInfo?.RealName);

            return(entityHistory);
        }
        /// <summary>
        /// 获取实体历史列表
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="actionType">动作类型</param>
        /// <returns>实体历史列表</returns>
        public ICollection <IEntityHistory> GetEntityHistories <T>(ActionType?actionType = null) where T : PlainEntity
        {
            LoginInfo loginInfo = GetLoginInfo?.Invoke();
            ICollection <IEntityHistory>     entityHistories = new HashSet <IEntityHistory>();
            IEnumerable <DbEntityEntry <T> > entries         =
                from entry in this._dbContext.ChangeTracker.Entries <T>()
                where actionType == null || entry.State == (EntityState)actionType.Value
                select entry;

            foreach (DbEntityEntry <T> entry in entries)
            {
                ActionType actualActionType;
                IDictionary <string, object> beforeSnapshot = new Dictionary <string, object>();
                IDictionary <string, object> afterSnapshot  = new Dictionary <string, object>();
                if (entry.State == EntityState.Added)
                {
                    actualActionType = ActionType.Create;
                    foreach (string propertyName in entry.CurrentValues.PropertyNames)
                    {
                        DbPropertyEntry propertyEntry = entry.Property(propertyName);
                        afterSnapshot[propertyName] = propertyEntry.CurrentValue;
                    }
                }
                else if (entry.State == EntityState.Modified)
                {
                    actualActionType = ActionType.Update;
                    foreach (string propertyName in entry.OriginalValues.PropertyNames)
                    {
                        DbPropertyEntry propertyEntry = entry.Property(propertyName);
                        if (propertyEntry.OriginalValue?.ToString() != propertyEntry.CurrentValue?.ToString())
                        {
                            beforeSnapshot[propertyName] = propertyEntry.OriginalValue;
                            afterSnapshot[propertyName]  = propertyEntry.CurrentValue;
                        }
                    }
                }
                else if (entry.State == EntityState.Deleted)
                {
                    actualActionType = ActionType.Delete;
                    foreach (string propertyName in entry.OriginalValues.PropertyNames)
                    {
                        DbPropertyEntry propertyEntry = entry.Property(propertyName);
                        beforeSnapshot[propertyName] = propertyEntry.OriginalValue;
                    }
                }
                else
                {
                    continue;
                }

                EntityHistory entityHistory = new EntityHistory(actualActionType, typeof(T), entry.Entity.Id, beforeSnapshot, afterSnapshot, loginInfo?.LoginId, loginInfo?.RealName);
                entityHistories.Add(entityHistory);
            }

            return(entityHistories);
        }
Example #15
0
        /// <summary>
        /// 注册删除单个实体对象(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="number">编号</param>
        public void RegisterRemove <T>(string number) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (string.IsNullOrWhiteSpace(number))
            {
                throw new ArgumentNullException(nameof(number), $"要删除的{typeof(T).Name}实体对象编号不可为空!");
            }

            #endregion

            T entity = this.ResolveOptional <T>(x => x.Number == number);

            #region # 验证为null

            if (entity == null)
            {
                throw new NullReferenceException($"编号为\"{number}\"的{typeof(T).Name}实体不存在!");
            }

            #endregion

            #region # 设置操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
            }

            #endregion

            entity.Deleted     = true;
            entity.DeletedTime = DateTime.Now;
            DbEntityEntry entry = this._dbContext.Entry <T>(entity);
            entry.State = EntityState.Modified;
        }
Example #16
0
        /// <summary>
        /// 注册删除单个实体对象(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="id">标识Id</param>
        public void RegisterRemove <T>(Guid id) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (id == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(id), $"要删除的{typeof(T).Name}实体对象Id不可为空!");
            }

            #endregion

            T entity = this.ResolveOptional <T>(x => x.Id == id);

            #region # 验证为null

            if (entity == null)
            {
                throw new NullReferenceException($"Id为\"{id}\"的{typeof(T).Name}实体不存在!");
            }

            #endregion

            #region # 设置操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.OperatorAccount = loginInfo?.LoginId;
                entity.OperatorName    = loginInfo?.RealName;
            }

            #endregion

            entity.Deleted     = true;
            entity.DeletedTime = DateTime.Now;
            DbEntityEntry entry = this._dbContext.Entry <T>(entity);
            entry.State = EntityState.Modified;
        }
Example #17
0
        //Public

        #region # 注册添加单个实体对象 —— void RegisterAdd<T>(T entity)
        /// <summary>
        /// 注册添加单个实体对象
        /// </summary>
        /// <typeparam name="T">聚合根类型</typeparam>
        /// <param name="entity">新实体对象</param>
        /// <exception cref="ArgumentNullException">新实体对象为空</exception>
        public void RegisterAdd <T>(T entity) where T : AggregateRootEntity
        {
            #region # 验证参数

            if (entity == null)
            {
                throw new ArgumentNullException("entity", string.Format(@"要添加的{0}实体对象不可为空!", typeof(T).Name));
            }

            #endregion

            #region # 设置创建/操作人信息

            if (GetLoginInfo != null)
            {
                LoginInfo loginInfo = GetLoginInfo.Invoke();
                entity.CreatorAccount  = loginInfo == null ? null : loginInfo.LoginId;
                entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
            }

            #endregion

            this._dbContext.Set <T>().Add(entity);
        }
Example #18
0
        /// <summary>
        /// 注册删除全部(逻辑删除)
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        public void RegisterRemoveAll <T>() where T : AggregateRootEntity
        {
            LoginInfo loginInfo = null;

            #region # 获取操作人信息

            if (GetLoginInfo != null)
            {
                loginInfo = GetLoginInfo.Invoke();
            }

            #endregion

            IQueryable <T> entities = this.ResolveRange <T>(x => true);

            foreach (T entity in entities)
            {
                entity.OperatorAccount = loginInfo == null ? null : loginInfo.LoginId;
                entity.Deleted         = true;
                entity.DeletedTime     = DateTime.Now;
                DbEntityEntry entry = this._dbContext.Entry <T>(entity);
                entry.State = EntityState.Modified;
            }
        }