Skip to main content
All docs
V24.2

Custom Object Space Provider

  • 5 minutes to read

Follow the steps below to add a custom Object Space provider to your application:

  • Create a custom object space class
  • Create a custom object space provider class
  • Configure your application to use custom object space provider

The following steps add a custom XPO object space provider in a .NET Framework application.

1. Create a Custom Object Space Class

Create a custom object space class and override the methods you want to customize. The following examples customize the DoCommit method:

// integrated security 
public class MyCustomSecuredEFCoreObjectSpace : SecuredEFCoreObjectSpace { 
    public MyCustomSecuredEFCoreObjectSpace(ITypesInfo typesInfo, IEntityStore entityStore, Func<DbContext> createDbContext, ISecurityStrategyBase security)  
        : base(typesInfo, entityStore, createDbContext, security) { } 
    protected override void DoCommit() { 
        // Write your custom code here or override other methods.  
        base.DoCommit(); 
    } 
} 

// no security or middle-tier security 
public class MyCustomEFCoreObjectSpace : EFCoreObjectSpace { 
    public MyCustomEFCoreObjectSpace(ITypesInfo typesInfo, IEntityStore entityStore, Func<DbContext> createDbContext)  
        : base(typesInfo, entityStore, createDbContext) { } 
    protected override void DoCommit() { 
        // Write your custom code here or override other methods.  
        base.DoCommit(); 
    } 
} 

Inherit from the XPObjectSpace class and override the methods you want to customize. The following example customizes the DoCommit method:

public class MyCustomXPObjectSpace : XPObjectSpace {
    public MyCustomXPObjectSpace(ITypesInfo typesInfo, XpoTypeInfoSource xpoTypeInfoSource, CreateUnitOfWorkHandler createUnitOfWorkDelegate)
        : base(typesInfo, xpoTypeInfoSource, createUnitOfWorkDelegate) { }
    protected override void DoCommit() {
        // Write your custom code here or override other methods.
        base.DoCommit();
    }
}

2. Create a Custom Object Space Provider Class

Create a custom object space provider class. Override the CreateObjectSpaceCore method (CreateObjectSpace for non-persistent object space provider) to return your custom object space instance.

// integrated security
public class MyCustomSecuredEFCoreObjectSpaceProvider<TDbContext>
    : SecuredEFCoreObjectSpaceProvider<TDbContext> where TDbContext : DbContext {
    readonly ISelectDataSecurityProvider selectDataSecurityProvider;
    public MyCustomSecuredEFCoreObjectSpaceProvider(ISelectDataSecurityProvider selectDataSecurityProvider, IDbContextFactory<TDbContext> dbContextFactory) 
        : base(selectDataSecurityProvider, dbContextFactory) {
        this.selectDataSecurityProvider = selectDataSecurityProvider;
    }
    protected override EFCoreObjectSpace CreateObjectSpaceCore() {
        return new MyCustomSecuredEFCoreObjectSpace(TypesInfo, EntityStore, CreateDbContext, (ISecurityStrategyBase)selectDataSecurityProvider);
    }
}

// no security or middle-tier security 
public class MyCustomEFCoreObjectSpaceProvider<TDbContext> 
    : EFCoreObjectSpaceProvider<TDbContext> where TDbContext: DbContext {
    public MyCustomEFCoreObjectSpaceProvider(IDbContextFactory<TDbContext> dbContextFactory)
        : base(dbContextFactory) { }
    protected override EFCoreObjectSpace CreateObjectSpaceCore() {
        return new MyCustomEFCoreObjectSpace(TypesInfo, EntityStore, CreateDbContext);
    }
}

Inherit from the XPObjectSpaceProvider class, override the CreateObjectSpaceCore method to return your custom MyCustomXPObjectSpace instance.

If you use integrated mode of the security system or middle tier security, inherit SecuredObjectSpaceProvider or MiddleTierServerObjectSpaceProvider instead of XPObjectSpaceProvider accordingly.

public class MyCustomXPObjectSpaceProvider : XPObjectSpaceProvider {
    public MyCustomXPObjectSpaceProvider(IXpoDataStoreProvider dataStoreProvider, bool threadSafe) : base(dataStoreProvider, threadSafe) { }  
    public MyCustomXPObjectSpaceProvider(string connectionString, IDbConnection connection, bool threadSafe) : base(connectionString, connection, threadSafe) { }
    protected override IObjectSpace CreateObjectSpaceCore() {
        return new MyCustomXPObjectSpace(TypesInfo, XpoTypeInfoSource, () => CreateUnitOfWork(DataLayer));
    }
}

3. Configure Your Application to Use Custom Object Space Provider

Use the CustomCreateObjectSpaceProvider method to add your custom object space provider to the application.

File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs, MySolution.WebApi\Startup.cs

// integrated security
builder.ObjectSpaceProviders
    .AddSecuredEFCore(o => {
        // ...
        o.CustomCreateObjectSpaceProvider = (context) => {
            var selectDataSecurityProvider = context.ServiceProvider.GetRequiredService<ISelectDataSecurityProvider>();
            var dbContextFactory = context.ServiceProvider.GetRequiredService<IDbContextFactory<MainDemoDbContext>>();
            return new MyCustomSecuredEFCoreObjectSpaceProvider<ApplicationDbContext>(selectDataSecurityProvider, dbContextFactory);
        };
    })
    .WithDbContext<ApplicationDbContext>(...)

// no security or middle-tier security 
builder.ObjectSpaceProviders
    .AddEFCore(o => {
        // ...
        o.CustomCreateObjectSpaceProvider = (context) => {
            var dbContextFactory = context.ServiceProvider.GetRequiredService<IDbContextFactory<MainDemoDbContext>>();
            return new MyCustomEFCoreObjectSpaceProvider<ApplicationDbContext>(dbContextFactory);
        };
    })
    .WithDbContext<ApplicationDbContext>(...)

Call the CreateDefaultObjectSpaceProvider method and add a MyCustomXPObjectSpaceProvider instance to the ObjectSpaceProviders list.

File: WinApplication.cs or WebApplication.cs

protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
    // ...
    args.ObjectSpaceProviders.Add(new MyCustomXPObjectSpaceProvider(args.ConnectionString, args.Connection));
}