如何在 Prism 中注册一个 class 到 IoC
How to register a class to IoC in Prism
我用 WPF 写了一个应用程序。我将 Prism 库与 IoC 一起使用 Prism.DryIoC。
我有一个 AppDbContext.cs class 来声明数据库的连接字符串(这里是 MongoDB)
public class AppDbContext : BaseMongoRepository
{
public AppDbContext(string connectionString, string databaseName = null) : base(connectionString, databaseName)
{
}
}
我有一个 class MyService.cs 使用 AppDbContext class,我在构造函数中声明。
public class 我的服务:我的服务
{
私人 AppDbContext _dbContext;
public IdentifierRepository(AppDbContext dbContext)
{
_dbContext = dbContext;
}
public void AddCustomer(Customer model)
{
// Some code....
_dbContext.Add(model);
}
}
在App.xaml.csclass我覆盖了方法
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IAuthenticationService, AuthenticationService>();
// MongoDB
var connectionString = SharedCommon.LocalAppSettings.Database.ConnectionString;
var database = SharedCommon.LocalAppSettings.Database.DatabaseName;
// How to register class MyService.cs here?
// I dont known.
containerRegistry<MyService>(() => new MyService(new AppDbContext(connectionString, database))); // Wrong code
}
您可以找到所有的注册方式here。
对于单例 MyService
:
var myService = new MyService(new AppDbContext(connectionString, database)));
containerRegistry.RegisterInstance(myService);
对于多个实例,您可以改用工厂。
public class MyServiceFactory
{
private readonly AppDbContext appDbContext;
public MyServiceFactory(AppDbContext appDbContext)
{
this.appDbContext = appDbContext;
}
public MyService Create() => new MyService(appDbContext);
}
注册工厂实例:
var context = new AppDbContext(connectionString, database);
var factory = new MyServiceFactory(context);
containerRegistry.RegisterInstance(factory);
然后创建您的服务实例:
var factory = container.Resolve<MyServiceFactory>();
var service = factory.Create();
我用 WPF 写了一个应用程序。我将 Prism 库与 IoC 一起使用 Prism.DryIoC。 我有一个 AppDbContext.cs class 来声明数据库的连接字符串(这里是 MongoDB)
public class AppDbContext : BaseMongoRepository
{
public AppDbContext(string connectionString, string databaseName = null) : base(connectionString, databaseName)
{
}
}
我有一个 class MyService.cs 使用 AppDbContext class,我在构造函数中声明。 public class 我的服务:我的服务 { 私人 AppDbContext _dbContext;
public IdentifierRepository(AppDbContext dbContext)
{
_dbContext = dbContext;
}
public void AddCustomer(Customer model)
{
// Some code....
_dbContext.Add(model);
}
}
在App.xaml.csclass我覆盖了方法
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IAuthenticationService, AuthenticationService>();
// MongoDB
var connectionString = SharedCommon.LocalAppSettings.Database.ConnectionString;
var database = SharedCommon.LocalAppSettings.Database.DatabaseName;
// How to register class MyService.cs here?
// I dont known.
containerRegistry<MyService>(() => new MyService(new AppDbContext(connectionString, database))); // Wrong code
}
您可以找到所有的注册方式here。
对于单例 MyService
:
var myService = new MyService(new AppDbContext(connectionString, database)));
containerRegistry.RegisterInstance(myService);
对于多个实例,您可以改用工厂。
public class MyServiceFactory
{
private readonly AppDbContext appDbContext;
public MyServiceFactory(AppDbContext appDbContext)
{
this.appDbContext = appDbContext;
}
public MyService Create() => new MyService(appDbContext);
}
注册工厂实例:
var context = new AppDbContext(connectionString, database);
var factory = new MyServiceFactory(context);
containerRegistry.RegisterInstance(factory);
然后创建您的服务实例:
var factory = container.Resolve<MyServiceFactory>();
var service = factory.Create();