Skip to main content
All docs
V25.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(); 
        } 
    } 
    

    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);
        }
    }
    

    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>(...)
    

    XPO ORM-specific Object Space Provider and Data Store Customizations