• 框架重构:记录创建人、最后修改人、创建时间、最后修改时间


    Entity(框架代码)

    public interface IEntity
    {
        Guid DBID { get; }
        DateTime CreateTime { get; set; }
        DateTime UpdateTime { get; set; }
        string CreateUser { get; set; }
        string UpdateUser { get; set; }
        string Remark { get; set; }
    }
    
    [Serializable]
    public class Entity<TEntity> : IEntity 
        where TEntity : IEntity
    {
        public virtual Guid DBID { get; protected set; }
        public virtual DateTime CreateTime { get; set; }
        public virtual DateTime UpdateTime { get; set; }
        public virtual string CreateUser { get; set; }
        public virtual string UpdateUser { get; set; }
        public virtual string Remark { get; set; }
    }
    

    Command(框架代码)

    • 所有的实体(聚合根)都会经由Command来设置自身的属性值,在Command的基类中为每个实体自动填写创建人、最后修改人、创建时间、最后修改时间是非常合适的。
    • Entity声明在Command中
    • Repository声明在Command中
    public interface ICommand<T> 
        where T : Entity<T>
    {
        T Entity { get; set; }
    }
    
    public class Command<T> : ICommand<T>
        where T: Entity<T>
    {
        public T Entity { get; set; }
        protected IRepository repository;
        protected Command(T entity)
        {
            Entity = entity;
            repository = IoC.Get<IRepository>();
            SetUpdateTime();
            SetUpdateUser();
        }
        private void SetUpdateUser()
        {
            var user = IoC.Get<IAuthenticationService>().GetCurrentRealName();
            if (string.IsNullOrEmpty(Entity.CreateUser))
                Entity.CreateUser = user;
            Entity.UpdateUser = user;
        }
        private void SetUpdateTime()
        {
            if (Entity.CreateTime == new DateTime().SqlServerMinValue())
                Entity.CreateTime = DateTime.Now;
            Entity.UpdateTime = DateTime.Now;
        }
    }
    

    使用Command(业务代码)

    public interface ISaftLawCommand : ICommand<SaftLaw>
    {
        ......
    }
    
    public class SaftLawCommand : Command<SaftLaw>, ISaftLawCommand
    {
        public SaftLawCommand(SaftLaw saftLaw) : base(saftLaw)
        {
        }
        
        ......
    }
    

    注意事项

    • 不要忘了在会话中记录用户名(账号/真实姓名)
    // 在登录的时候记录
    authenticationService.SaveCurrentRealName(account.Id.UserName);
    

    效果

  • 相关阅读:
    BISDN上收集到的SAP BI的极好文章的链接
    如何设置'REUSE_ALV_GRID_DISPLAY'的单个单元格的颜色
    如何设置REUSE_ALV_GRID_DISPLAY'的单个单元格的是否可以输入
    BWABAP to copy aggregates from one cube to another
    SDva01的屏幕增强
    js鼠标悬停效果
    MySQL更新UPDATA的使用
    使用mysql C语言API编写程序—MYSQL数据库查询操作
    MySQL的部分基础语句
    MySQLdelete某个元组||、&&操作
  • 原文地址:https://www.cnblogs.com/tuty/p/6741618.html
Copyright © 2020-2023  润新知