Skip to main content
.NET 6.0+

IXafEntityObject Interface

Declares methods that are called automatically when a business object is being created, loaded and saved.

Namespace: DevExpress.ExpressApp

Assembly: DevExpress.ExpressApp.v24.1.dll

NuGet Package: DevExpress.ExpressApp

Declaration

public interface IXafEntityObject

Remarks

When a business object supports IXafEntityObject, the Object Space automatically calls the IXafEntityObject.OnCreated, IXafEntityObject.OnLoaded and IXafEntityObject.OnSaving methods of this interface when an object is being created, loaded and saved. Thus, you can implement these methods and add certain business logic directly in the business object code, without the use of Controllers. In most cases, you may need to access other business objects from your code, so you can implement the IObjectSpaceLink interface to access an IObjectSpace instance and use its methods to query objects.

Note

  • The IXafEntityObject interface is intended for use with Entity Framework and Non-Persistent Objects. For eXpress Persistent Objects (XPO), use the BaseObject.AfterConstruction and BaseObject.OnSaving methods.
  • You can use the XAF Business Object | EF Core Business Object from Template Gallery to add a class that supports both IXafEntityObject and IObjectSpaceLink.

In the example below, the CreatedBy property refers to the user who created the current object, and the UpdatedBy property specifies the user who last changed the object. Note that the example code uses the SecuredPropertySetter.SetPropertyValueWithSecurityBypass method to set values of these properties from the OnSaving method. This method allows you to modify the value of a business object’s property even if write access to this property is denied for the current user by the XAF Security System.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.EFCore;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Security;
using DevExpress.Persistent.Base;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
[DefaultClassOptions]
public class MyEntityObject : IXafEntityObject {
    [Key, Browsable(false)]
    public virtual Guid ID { get; protected set; }
    public virtual string Name { get; set; }
    [ModelDefault(nameof(IModelCommonMemberViewItem.AllowEdit), "False")]
    public virtual ApplicationUser CreatedBy { get; set; }
    [ModelDefault(nameof(IModelCommonMemberViewItem.AllowEdit), "False")]
    public virtual ApplicationUser UpdatedBy { get; set; }

    ApplicationUser GetCurrentUser() {
        return ObjectSpace.GetObjectByKey<ApplicationUser>(
            ObjectSpace.ServiceProvider.GetRequiredService<ISecurityStrategyBase>().UserId);
    }

    void IXafEntityObject.OnCreated() {
        // ...
    }

    void IXafEntityObject.OnLoaded() {
        // ...
    }

    void IXafEntityObject.OnSaving() {
        if (ObjectSpace.IsNewObject(this)) {
            SecuredPropertySetter.SetPropertyValueWithSecurityBypass(this, nameof(CreatedBy), GetCurrentUser());
        }
        else {
            SecuredPropertySetter.SetPropertyValueWithSecurityBypass(this, nameof(UpdatedBy), GetCurrentUser());
        }
    }

    IObjectSpace ObjectSpace {
        get {
            return ((IObjectSpaceLink)this).ObjectSpace;
        }
    }
    // ...
}
// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

You can also inherit your class from the DevExpress.Persistent.BaseImpl.EF.BaseObject class that implements this interface.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using YourApplicaionName.Module.BusinessObjects;
[DefaultClassOptions]
public class MyEntityObject : BaseObject {
    public virtual string Name { get; set; }
    [ModelDefault(nameof(IModelCommonMemberViewItem.AllowEdit), "False")]
    public virtual ApplicationUser CreatedBy { get; set; }
    [ModelDefault(nameof(IModelCommonMemberViewItem.AllowEdit), "False")]
    public virtual ApplicationUser UpdatedBy { get; set; }

    ApplicationUser GetCurrentUser() {
        return ObjectSpace.GetObjectByKey<ApplicationUser>(
            ObjectSpace.ServiceProvider.GetRequiredService<ISecurityStrategyBase>().UserId);
    }

    public override void OnSaving() {
        base.OnSaving();
        if (ObjectSpace.IsNewObject(this)) {
            // You can use BaseObject.SetPropertyValueWithSecurityBypass
            // to set the property value.
            SetPropertyValueWithSecurityBypass(nameof(CreatedBy), GetCurrentUser());
        }
        else {
            SetPropertyValueWithSecurityBypass(nameof(UpdatedBy), GetCurrentUser());
        }
    }
    // ...
}
See Also