Skip to main content
.NET 8.0+

IObjectSpaceLink Interface

Implemented by business classes that provide a reference to an associated Object Space.

Namespace: DevExpress.ExpressApp

Assembly: DevExpress.ExpressApp.v24.2.dll

NuGet Package: DevExpress.ExpressApp

Declaration

Remarks

XAF automatically adds the IObjectSpaceLink interface to all persistent EF Core classes at runtime. Cast an existing object to the IObjectSpaceLink type to use this interface from the Controller:

private void MyAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
    var cnt = View.CurrentObject as Contact;
    if(cnt == null)
        return;
    var cntOS = ((IObjectSpaceLink)cnt).ObjectSpace;
}

The code below uses the IObjectSpaceLink interface directly in the object:

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;
        }
    }
    // ...
}

The snippet below uses the IObjectSpaceLink interface in a non-persistent class:

[DomainComponent]
public class MyNonPersistentClass : IObjectSpaceLink {
    //...
    IObjectSpace objectSpace;
    IObjectSpace IObjectSpaceLink.ObjectSpace {
        get { return objectSpace; }
        set { objectSpace = value; }
    }
}

An Object Space reference is automatically assigned to the IObjectSpaceLink.ObjectSpace property when IObjectSpace methods are used to create a business object that supports IObjectSpaceLink. Data management is performed using these methods in internal XAF code, and we recommended that you use IObjectSpace in your custom code as well. You can implement this interface and access other business objects directly in the current object code. For example, you can write certain business logic in the IXafEntityObject.OnCreated, IXafEntityObject.OnLoaded and IXafEntityObject.OnSaving methods, as demonstrated in the IXafEntityObject interface description.

Note

  • The Object Space reference is set to null (Nothing in VB) when an object is being deleted.
  • You cannot share non-persistent objects that implement IObjectSpaceLink between different Object Spaces. For example, an exception is thrown when you open a Detail View for an object selected in a List View. To address this exception, subscribe to the NonPersistentObjectSpace.ObjectGetting event, create a new object instance, and copy property values from the source object.

See Also