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);
效果
来源:https://www.cnblogs.com/tuty/p/6741618.html