Implementation Of Dependency Injection Support in Existing Applications
- 3 minutes to read
This article describes how to implement dependency injection support in the following applications:
- Standalone Web API applications created before v23.1
- XAF ASP.NET Core Blazor applications that don’t include the Application Builder
Project Converter Tool
Use the Project Converter tool to update your application to the latest XAF version. Note that this technique only works if your application code base does not differ significantly from the application code generated by the Solution Wizard.
Manual Update Of a Standalone Web API Application
Standalone Web API applications use the IObjectSpaceProviderFactory
service autogenerated by the XAF Solution Wizard. This service creates two default IObjectSpaceProvider
objects: for persistent and non-persistent data. To pass IServiceProvider
to IObjectSpaceProvider
objects, follow the steps below:
- In the ObjectSpaceProviderFactory.cs file, add the
IServiceProvider
parameter to theObjectSpaceProviderFactory
constructor. - Pass the
IServiceProvider
object to theNonPersistentObjectSpaceProvider
constructor.
EF Core
using DevExpress.EntityFrameworkCore.Security;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.EFCore;
using DevExpress.ExpressApp.Security;
public sealed class ObjectSpaceProviderFactory : IObjectSpaceProviderFactory {
readonly ISecurityStrategyBase security;
readonly ITypesInfo typesInfo;
readonly IXafDbContextFactory<AppDbContext> dbFactory;
readonly IServiceProvider serviceProvider;
public ObjectSpaceProviderFactory(IServiceProvider serviceProvider, ISecurityStrategyBase security, ITypesInfo typesInfo, IXafDbContextFactory<AppDbContext> dbFactory) {
this.serviceProvider = serviceProvider;
this.security = security;
this.typesInfo = typesInfo;
this.dbFactory = dbFactory;
}
IEnumerable<IObjectSpaceProvider> IObjectSpaceProviderFactory.CreateObjectSpaceProviders() {
yield return new SecuredEFCoreObjectSpaceProvider<AppDbContext>((ISelectDataSecurityProvider)security, dbFactory, typesInfo);
yield return new NonPersistentObjectSpaceProvider(serviceProvider, typesInfo, null);
}
}
Additionally, in the Startup.cs file, in the ConfigureServices
method, call the DbContextOptionsBuilder.UseXafServiceProviderContainer
method and pass IServiceProvider
as its parameter.
services.AddDbContextFactory<AppDbContext>((serviceProvider, options) => {
//...
options.UseSecurity(serviceProvider);
options.UseXafServiceProviderContainer(serviceProvider);
}, ServiceLifetime.Scoped);
services.AddDbContextFactory<AuditingDbContext>((serviceProvider, options) => {
//...
options.UseLazyLoadingProxies();
options.UseXafServiceProviderContainer(serviceProvider);
}, ServiceLifetime.Scoped);
XPO Secured
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Security.ClientServer;
using DevExpress.ExpressApp.Xpo;
public sealed class ObjectSpaceProviderFactory : IObjectSpaceProviderFactory {
readonly ISecurityStrategyBase security;
readonly ITypesInfo typesInfo;
readonly IXpoDataStoreProvider dataStoreProvider;
readonly IServiceProvider serviceProvider;
public ObjectSpaceProviderFactory(IServiceProvider serviceProvider, ISecurityStrategyBase security, ITypesInfo typesInfo, IXpoDataStoreProvider dataStoreProvider) {
this.serviceProvider = serviceProvider;
this.security = security;
this.typesInfo = typesInfo;
this.dataStoreProvider = dataStoreProvider;
}
public IEnumerable<IObjectSpaceProvider> CreateObjectSpaceProviders() {
return new IObjectSpaceProvider[] {
new SecuredObjectSpaceProvider((ISelectDataSecurityProvider)security, dataStoreProvider, true),
new NonPersistentObjectSpaceProvider(serviceProvider, typesInfo, null)
};
}
}
XPO Non-Secured
If your XAF XPO application uses non-secured XPObjectSpaceProvider
, pass the IServiceProvider
object to the XPObjectSpaceProvider
constructor.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Xpo;
public sealed class ObjectSpaceProviderFactory : IObjectSpaceProviderFactory {
readonly ITypesInfo typesInfo;
readonly IXpoDataStoreProvider dataStoreProvider;
readonly IServiceProvider serviceProvider;
public ObjectSpaceProviderFactory(ITypesInfo typesInfo, IXpoDataStoreProvider dataStoreProvider, IServiceProvider serviceProvider) {
this.typesInfo = typesInfo;
this.dataStoreProvider = dataStoreProvider;
this.serviceProvider = serviceProvider;
}
public IEnumerable<IObjectSpaceProvider> CreateObjectSpaceProviders() {
yield return new XPObjectSpaceProvider(serviceProvider, dataStoreProvider, true);
yield return new NonPersistentObjectSpaceProvider(serviceProvider, typesInfo, null);
}
}
Manual Update of an ASP.NET Core Blazor Application
To update an XAF ASP.NET Core Blazor application without the Application Builder, go to the YourApplicationName.Blazor.Server\YourApplicationNameBlazorApplication.cs file and override the XafApplication.CreateDefaultObjectSpaceProvider
method as shown in the examples below:
EF Core Secured
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
IXafDbContextFactory<YourSolutionNameDbContext> dbFactory =
ServiceProvider.GetService<IXafDbContextFactory<YourSolutionNameDbContext>>();
SecuredEFCoreObjectSpaceProvider efCoreObjectSpaceProvider =
new SecuredEFCoreObjectSpaceProvider((ISelectDataSecurityProvider)Security, dbFactory, TypesInfo);
args.ObjectSpaceProviders.Add(efCoreObjectSpaceProvider);
args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(ServiceProvider, TypesInfo, null));
}
Additionally, in the ConfigureServices
method of the YourApplicationName.Blazor.Server\Startup.cs file, call the DbContextOptionsBuilder.UseXafServiceProviderContainer
method and pass the IServiceProvider
as its parameter.
XPO Secured
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
IXpoDataStoreProvider dataStoreProvider = GetDataStoreProvider(args.ConnectionString, args.Connection);
args.ObjectSpaceProviders.Add(new SecuredObjectSpaceProvider((ISelectDataSecurityProvider)Security, dataStoreProvider, true));
args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(ServiceProvider, TypesInfo, null));
}
XPO Non-Secured
In non-secure XAF XPO applications, navigate to the YourApplicationName.Blazor.Server\BlazorApplication.cs file and pass the IServiceParameter
as shown in the example above: XPO: Non-Secured Standalone Web API Application.