English 中文(简体)
扩展基类和自动更新实体的审计信息
原标题:Extend base type and automatically update audit information on Entity

我有一个实体模式,每个表格(50+表格)都有审计信息。

 CreateDate
 CreateUser
 UpdateDate
 UpdateUser

目前,我们计划更新审计信息。

  if(changed){
        entity.UpdatedOn = DateTime.Now;
        entity.UpdatedBy = Environment.UserName;
        context.SaveChanges();
   }

但是,我正在寻求一个更加自动化的解决办法。 在进行节省开支期间,如果一个实体成立/更新,我想自动更新这些领域,然后将其送往数据库储存。

任何关于如何做到这一点的建议? 我宁愿不作任何思考,因为使用案文模板并不是问题。

有人提出解决办法,以压倒“拯救”并做它,但为了做到这一点,要么必须利用思考(我不想这样做)或得出基类。 假定我走这条路将如何做到这一点?

例如

EXAMPLE_DB_TABLE
 CODE
 NAME
 --Audit Tables
 CREATE_DATE
 CREATE_USER
 UPDATE_DATE
 UPDATE_USER

并且,如果建立基类

 public abstract class IUpdatable{

     public virtual DateTime CreateDate {set;}
     public virtual string CreateUser { set;}
     public virtual DateTime UpdateDate { set;}
     public virtual string UpdateUser { set;}
 }

最终目标是能够做像......这样的事情。

   public overrride void SaveChanges(){
        //Go through state manager and update audit infromation
        //FOREACH changed entity in state manager
        if(entity is IUpdatable){
           //If state is created... update create audit.
           //if state is updated... update update audit
        }
   }

但我不敢肯定我如何制定扩大接口的守则。

最佳回答

这里是最后的解决办法。

我基本上检查了我的4个财产存在模式。 实体

插入和插入版By,更新和更新版By I, 产生以下实体(使用纸面文件),实施一个审计界面。

public interface IAuditable
{
    void SetInsertedOn(DateTime date);
    void SetInsertedBy(string user);
    void SetUpdatedOn(DateTime date);
    void SetUpdatedBy(string user);
}

public partial class ExampleDBtable: EntityObject, Audit.IAuditable
{
     //Normal EDMX stuff here..
     public static ExampleDBtable CreateExampleDBtable(int id, string code, string name, DateTime insertedOn, string insertedBy, DateTime updatedOn, string updatedBy)
       {

       }
       //Extension points.
    #region IAuditable Members

    public void SetInsertedOn(DateTime date)
    {
        this._InsertedOn = date;
    }

    public void SetInsertedBy(string user)
    {
        this._InsertedBy = user;
    }

    public void SetUpdatedOn(DateTime date)
    {
        this._UpdatedOn = date;
    }

    public void SetUpdatedBy(string user)
    {
        this._UpdatedBy = user;
    }

    #endregion

    }

我还制定了处理更新审计信息的准则。

   public ExampleDBObjectContext(string connectionString) : base(connectionString, "publicExampleDBObjectContext")
    {
        this.SavingChanges += new EventHandler(UpdateAuditInformation);
        this.OnContextCreated();
     }

最后,我实施了《最新情况》

   foreach (ObjectStateEntry entry in
            context.ObjectStateManager.GetObjectStateEntries(
            EntityState.Added | EntityState.Modified))
        {
              //Start pseudo code..
              if(entry.Entity is IAuditable){
                 IAuditable temp = entry.Entity as IAuditable;
                 temp.SetInsertedOn(DateTime.Now);
                 temp.SetInsertedBy(Env.GetUser());
                 ...

              }
        }

我选择不出示.子,因为它在编辑中看得很可怕。

问题回答

是的,我认为你可以这样做。 一般想法将是

  1. Handle ObjectContext.SavingChanges.
  2. In that event, call ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
  3. Iterate the results and set properties if needed.

查阅PostSharp。 这些任务的非常好的解决办法。

通过数据库触发器对更新的浏览量进行标记不简单吗? 数据库中提供的信息是否不够充分? (数据库用户与用户)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签