2012-07-27 80 views
1

我正在設計一個帶有數據庫後端的ASP.NET MVC web應用程序,需要完整的審計跟蹤以滿足法規要求。帶內置審計跟蹤的ORM

我已經在過去實現了審計跟蹤,但感覺像使用帶有內置功能的ORM工具更安全。它看起來像NHibernate將是一種方式 - 你能推薦其他選項嗎?

澄清 - 我對辯論或ORM比較不感興趣。我正在有效地詢問哪些ORM工具內置了對審計追蹤的支持。

+1

如果你與像短小精悍迷你ORM去,你可以在您的應用程序中包含http://miniprofiler.com/。 – Alex 2012-07-27 17:47:52

+0

感謝您的建議,我認爲miniprofiler可能會有用。如果您可以建議其他具有內置審計跟蹤的ORM,我將有一個很好的起點來研究最佳選擇。 – alexsome 2012-07-30 13:04:15

+0

我知道這不完全是你問的,但EventSourcing是另一種選擇。那麼持久性和審計線索是完全一樣的。 – 2012-10-17 11:19:14

回答

0

我認爲你可以在所有完整的ORM中攔截實際的數據庫操作(但通常不是像微軟這樣的微型操作系統)。您可以捕獲新記錄,刪除和修改,包括對象的原始狀態和修改狀態。

這裏是LINQ2SQL一個例子,使用Newtonsoft的性質在轉儲到一個StringBuilder(代碼添加到您的DataContext類):

public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode) 
    { 
     var changes = GetChangeSet(); 
     var inserts = changes.Inserts; 
     var deletes = changes.Deletes; 
     var updates = changes.Updates; 

     var sbLog = new StringBuilder(); 

     sbLog.AppendLine("Inserts:"); 
     sbLog.AppendLine(Newtonsoft.Json.JsonConvert.SerializeObject(inserts, Newtonsoft.Json.Formatting.Indented, 
      new Newtonsoft.Json.JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore })); 

     sbLog.AppendLine("Deletes:"); 
     sbLog.AppendLine(Newtonsoft.Json.JsonConvert.SerializeObject(deletes, Newtonsoft.Json.Formatting.Indented, 
      new Newtonsoft.Json.JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore })); 

     sbLog.AppendLine("Updates:"); 
     foreach(object x in updates) { 
      var original = this.GetTable(x.GetType()).GetOriginalEntityState(x); 

      sbLog.AppendLine(Newtonsoft.Json.JsonConvert.SerializeObject(new { original = original, mod = x }, 
       Newtonsoft.Json.Formatting.Indented, 
       new Newtonsoft.Json.JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore })); 
     } 

     //"logger" can be anything, you use to log the changes... 
     logger.Info(("db operations:" + Environment.NewLine + sbLog.ToString()).Replace(Environment.NewLine, Environment.NewLine + " | ")); 

     base.SubmitChanges(failureMode); 
    }