Example E2419
Visible to All Users

How to track changes made to persistent objects, and write them into a separate table

Files to look at:

The OnSaving and OnDeleting method of the persistent objects can be overridden to log create/update/delete actions into a separate table. This example demonstrates the basic implementation of this feature.

Note: eXpressApp Framework have built-in module for Audit purposes. This example demonstrates how to use this module in non XAF application: How to use XAF Audit Trail module outside XAF.

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Q149895/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.Xpo; using DevExpress.Xpo.DB; using DevExpress.XtraEditors; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; namespace Q149895 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnGridControlEmbeddedNavigatorButtonClick(object sender, NavigatorButtonClickEventArgs e) { if (e.Button.ButtonType == NavigatorButtonType.Remove) { ((XPBaseObject)((GridView)gridControl1.FocusedView).GetFocusedRow()).Delete(); e.Handled = true; } } } }
Q149895/Northwind.cs(vb)
C#
using System; using DevExpress.Xpo; using DevExpress.Xpo.Metadata; using System.Collections.Generic; namespace Northwind { [NonPersistent] public class Base : XPLiteObject { public Base(Session session) : base(session) { } private void UpdateAudit(Action act) { Audit audit = new Audit(Session); audit.Action = act; audit.Date = DateTime.Now; audit.AuditedRecord = string.Format("{0}({1})", ClassInfo.FullName, Session.GetKeyValue(this)); audit.User = Environment.UserName; foreach(Change change in changes) { ModificationInfo modInfo = new ModificationInfo(Session); modInfo.Audit = audit; modInfo.PropertyName = change.PropertyName; modInfo.OldValue = change.PrevValue; modInfo.NewValue = change.Value; modInfo.Save(); } audit.Save(); } protected override void OnSaving() { base.OnSaving(); if(Session.IsNewObject(this)) UpdateAudit(Action.Insert); else UpdateAudit(Action.Update); } protected override void OnDeleting() { base.OnDeleting(); Change change = new Change(); change.PropertyName = this.ClassInfo.KeyProperty.Name; change.PrevValue = this.Session.GetKeyValue(this).ToString(); change.Value = "<DELETED>"; changes.Add(change); UpdateAudit(Action.Delete); } private List<Change> changes = new List<Change>(); protected override void OnChanged(string propertyName, object oldValue, object newValue) { base.OnChanged(propertyName, oldValue, newValue); Change change = new Change(); change.PropertyName = propertyName; change.PrevValue = oldValue == null ? "<NULL>" : oldValue.ToString(); change.Value = newValue == null ? "<NULL>" : newValue.ToString(); changes.Add(change); } private struct Change { public string PropertyName; public string PrevValue; public string Value; } } [Persistent("Category")] public class Categories : Base { int fCategoryID; [Key(true)] [Persistent("Id")] public int CategoryID { get { return fCategoryID; } set { SetPropertyValue<int>("CategoryID", ref fCategoryID, value); } } string fCategoryName; [Size(15)] public string CategoryName { get { return fCategoryName; } set { SetPropertyValue<string>("CategoryName", ref fCategoryName, value); } } string fDescription; [Size(1073741823)] public string Description { get { return fDescription; } set { SetPropertyValue<string>("Description", ref fDescription, value); } } public Categories(Session session) : base(session) { } public Categories() : base(Session.DefaultSession) { } public override void AfterConstruction() { base.AfterConstruction(); } } public class Audit : XPObject { public Audit(Session session) : base(session) { } private string fUser; public string User { get { return fUser; } set { SetPropertyValue<string>("User", ref fUser, value); } } private DateTime fDate; public DateTime Date { get { return fDate; } set { SetPropertyValue<DateTime>("Date", ref fDate, value); } } private Action fAction; public Action Action { get { return fAction; } set { SetPropertyValue<Action>("Action", ref fAction, value); } } private string fAuditedRecord; public string AuditedRecord { get { return fAuditedRecord; } set { SetPropertyValue<string>("AuditedRecord", ref fAuditedRecord, value); } } [Association("Audit-ModificationInfo")] public XPCollection<ModificationInfo> Modifications { get { return GetCollection<ModificationInfo>("Modifications"); } } } public class ModificationInfo : XPObject { public ModificationInfo(Session session) : base(session) { } private string fPropertyName; public string PropertyName { get { return fPropertyName; } set { SetPropertyValue<string>("PropertyName", ref fPropertyName, value); } } private string fOldValue; public string OldValue { get { return fOldValue; } set { SetPropertyValue<string>("OldValue", ref fOldValue, value); } } private string fNewValue; public string NewValue { get { return fNewValue; } set { SetPropertyValue<string>("NewValue", ref fNewValue, value); } } private Audit fAudit; [Association("Audit-ModificationInfo")] public Audit Audit { get { return fAudit; } set { SetPropertyValue<Audit>("Audit", ref fAudit, value); } } } public enum Action { Insert, Update, Delete } }
Q149895/Program.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; using DevExpress.Xpo; using DevExpress.Xpo.DB; namespace Q149895 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); XpoDefault.ConnectionString = SQLiteConnectionProvider.GetConnectionString(@"nwind.sqlite"); Application.Run(new Form1()); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.