Skip to main content
A newer version of this page is available. .
All docs
V21.2

Audit Changes in a Manually Created ObjectSpace

  • 2 minutes to read

Object Space API helps you manage data stores in XAF applications. To access the API, you usually need to call the XafApplication.CreateObjectSpace method that initializes an XPObjectSpace object. The Audit Trail Module then automatically tracks all changes in the new object space.

If you create an XPObjectSpace manually (for example, a non-secured object space in a secured application), the Audit Trail Module does not log changes made within this XPObjectSpace. To enable audit, register the object space and handle its events as shown below:

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Security.ClientServer;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.AuditTrail;
using DevExpress.Persistent.Base;
using MySolution.Module.BusinessObjects;
using System;
using System.Linq;

namespace MySolution.Module.Win.Controllers {
    public class CustomWinController : ObjectViewController<DetailView,Contact> {
        public CustomWinController() {
             var myAction1 = new SimpleAction(this, "MyWinAction1", PredefinedCategory.Edit);
              myAction1.Execute += MyAction1_Execute;
        }

        private void MyAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
            SecuredObjectSpaceProvider securedObjectSpaceProvider = (SecuredObjectSpaceProvider)
              Application.ObjectSpaceProviders.First(
                p => p is SecuredObjectSpaceProvider
              );
            // Create a 'free' XPObjectSpace
            IObjectSpace unsecuredObjectSpace = securedObjectSpaceProvider.CreateNonsecuredObjectSpace(); 
            var _contact = (Contact)unsecuredObjectSpace.GetObject(View.CurrentObject);

            AuditTrailService.Instance.BeginSessionAudit(((XPObjectSpace)unsecuredObjectSpace).Session, AuditTrailStrategy.OnObjectChanged);
            unsecuredObjectSpace.Committed += new EventHandler(ObjectSpace_Committed);
            unsecuredObjectSpace.Reloaded += new EventHandler(ObjectSpace_Reloaded);

            _contact.LastName = "NewName" + DateTime.Now.Millisecond;
            unsecuredObjectSpace.CommitChanges();
        }

        private void ObjectSpace_Reloaded(object sender, EventArgs e) {
            AuditTrailService.Instance.EndSessionAudit(((XPObjectSpace)sender).Session);
            AuditTrailService.Instance.BeginSessionAudit(((XPObjectSpace)sender).Session, AuditTrailStrategy.OnObjectChanged);
        }
        private void ObjectSpace_Committed(object sender, EventArgs e) {
            AuditTrailService.Instance.SaveAuditData(((XPObjectSpace)sender).Session);
            AuditTrailService.Instance.BeginSessionAudit(((XPObjectSpace)sender).Session, AuditTrailStrategy.OnObjectChanged);
        }

    }
}