Files to look at:
The XAF Audit Trail module is intended to track changes being made in persistent objects. This module uses the AuditTrailService class to track changes and write them to the database. It is possible to use this API in a non-XAF application based on XPO, without creating and configuring the DevExpress.ExpressApp > XafApplication object as it is usually done in XAF apps.
To use these features of the Audit Trial module, you will need to reference the DevExpress.Persistent.BaseImpl and DevExpress.Persistent.Base assemblies, which are part of the standard XAF delivery package.
Important notes:
1. You need to have a valid license for the eXpressApp Framework to use this example.
2. AuditTrailService cannot be used with ExplicitUnitOfWork. Use UnitOfWork or Session classes to audit data through AuditTrailService. AuditTrailService creates a new session to save audit records based on the data layer of the audited session. So, the data layer becomes shared among this new temporary session and ExplicitUnitOfWork. This violates the "exclusive owner of the database connection" requirement of ExplicitUnitOfWork.
See Also:
Audit Trail Module Overview
How to reuse XAF Audit Trail module functionality in a non-XAF WinForms application
How to track changes made to persistent objects, and write them into a separate table
Description
Starting from version 2013 vol 2, the GridViewCommandColumn.NewButton and GridViewCommandColumn.EditButton properties are obsolete. Use the GridViewCommandColumn.ShowNewButton, GridViewCommandColumn.ShowEditButton, ASPxGridView.SettingsCommandButton.NewButton, and ASPxGridView.SettingsCommandButton.EditButton properties instead.
Example Code
C#using DevExpress.Xpo;
namespace DXSample {
[Persistent("Persons")]
public class Person :XPObject {
public Person(Session session) : base(session) { }
private string fName;
public string Name {
get { return fName; }
set { SetPropertyValue<string>("Name", ref fName, value); }
}
}
}
C#using DXSample;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using DevExpress.Persistent.AuditTrail;
using bi = DevExpress.Persistent.BaseImpl;
/// <summary>
/// Summary description for XpoHelper
/// </summary>
public static class XpoHelper {
static readonly XPDictionary dict;
static XpoHelper() {
dict = new ReflectionDictionary();
dict.GetDataStoreSchema(typeof(Person).Assembly, typeof(bi.AuditDataItemPersistent).Assembly, typeof(AuditDataItem).Assembly);
AuditTrailService.Instance.SetXPDictionary(dict);
AuditTrailService.Instance.AuditDataStore = new AuditDataStore<bi.AuditDataItemPersistent, bi.AuditedObjectWeakReference>();
}
public static Session GetNewSession() {
return new Session(DataLayer);
}
public static UnitOfWork GetNewUnitOfWork() {
return new UnitOfWork(DataLayer);
}
private readonly static object lockObject = new object();
static volatile IDataLayer fDataLayer;
static IDataLayer DataLayer {
get {
if (fDataLayer == null) {
lock (lockObject) {
if (fDataLayer == null) {
fDataLayer = GetDataLayer();
}
}
}
return fDataLayer;
}
}
private static IDataLayer GetDataLayer() {
XpoDefault.Session = null;
return new ThreadSafeDataLayer(dict,
XpoDefault.GetConnectionProvider(MSSqlConnectionProvider.GetConnectionString("(local)", "Q295139"),
AutoCreateOption.DatabaseAndSchema));
}
}
C#using System;
using DevExpress.Xpo;
using DevExpress.Persistent.AuditTrail;
public partial class _Default : System.Web.UI.Page
{
Session session = XpoHelper.GetNewSession();
protected void Page_Load(object sender, EventArgs e)
{
source.Session = session;
AuditTrailService.Instance.BeginSessionAudit(session, AuditTrailStrategy.OnObjectChanged);
}
void OnAuditTrailServiceInstanceQueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e)
{
e.CurrentUserName = Context.User.Identity.Name;
}
protected override void OnUnload(EventArgs e) {
base.OnUnload(e);
AuditTrailService.Instance.QueryCurrentUserName += OnAuditTrailServiceInstanceQueryCurrentUserName;
AuditTrailService.Instance.SaveAuditData(session);
AuditTrailService.Instance.QueryCurrentUserName -= OnAuditTrailServiceInstanceQueryCurrentUserName;
}
}