Example E2274
Visible to All Users

How to reuse XAF Audit Trail module functionality in a non-XAF WinForms application

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 ASP.NET application
How to track changes made to persistent objects, and write them into a separate table

Does this example address your development requirements/objectives?

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

Example Code

WinExample/Form1.cs
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.XtraEditors.DXErrorProvider; using DevExpress.Persistent.AuditTrail; using System.Reflection; using System.Security.Principal; using DevExpress.Persistent.BaseImpl; using DevExpress.Xpo; namespace WinExample { public partial class Form1 : Form { public Form1() { //XpoDefault.DataLayer = XpoDefault.GetDataLayer("XpoProvider=InMemoryDataStore", DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema); XpoDefault.DataLayer = XpoDefault.GetDataLayer(@"Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\mssqllocaldb;Initial Catalog=myAudit", DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema); InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { AuditTrailService.GetService(null).QueryCurrentUserName += new QueryCurrentUserNameEventHandler(Instance_QueryCurrentUserName); session1.Dictionary.GetDataStoreSchema(typeof(PersistentObject1).Assembly); AuditTrailService.GetService(null).SetXPDictionary(session1.Dictionary); AuditTrailService.GetService(null).AuditDataStore = new AuditDataStore<AuditDataItemPersistent, AuditedObjectWeakReference>(); AuditTrailService.GetService(null).BeginSessionAudit(session1, AuditTrailStrategy.OnObjectChanged, ObjectAuditingMode.Full); } void Instance_QueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e) { e.CurrentUserName = WindowsIdentity.GetCurrent().Name; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { AuditTrailService.GetService(null).SaveAuditData(session1); } } }

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.