Dependency injection 使用structuremap 2.6注入常规参数
我需要将泛型存储库(IRepository)注入到我的类的构造函数中,但我不知道如何使用structuremap 2.6中的新DSL,有人知道怎么做吗?这可以通过配置中的一行代码来完成。假设你有: 实体: -顾客 -命令 并具有如下通用存储库模型:Dependency injection 使用structuremap 2.6注入常规参数,dependency-injection,structuremap,Dependency Injection,Structuremap,我需要将泛型存储库(IRepository)注入到我的类的构造函数中,但我不知道如何使用structuremap 2.6中的新DSL,有人知道怎么做吗?这可以通过配置中的一行代码来完成。假设你有: 实体: -顾客 -命令 并具有如下通用存储库模型: 存储库:IRepository 并且有一个应用程序服务,看起来像: public AppService(IRepository<Customer> custRepo, IRepository<Order> orderRep
- 存储库:IRepository
public AppService(IRepository<Customer> custRepo, IRepository<Order> orderRepo)
公共应用服务(IRepository custRepo、IRepository orderRepo)
你会有这样的东西。请注意有关使用扫描仪连接自定义存储库的部分
public class SmRegistry : Registry
{
public SmRegistry()
{
For(typeof (IRepository<>))
.Use(typeof (Repository<>));
//using this will find any custom repos, like CustomerRepository : Repository<Customer>
//Scan(scanner =>
// {
// scanner.TheCallingAssembly();
// scanner.ConnectImplementationsToTypesClosing(typeof (IRepository<>));
// });
}
}
公共类SmRegistry:注册表
{
公共注册处()
{
用于(类型(假定))
.使用(存储库的类型);
//使用此选项将查找任何自定义回购,如CustomerRepository:Repository
//扫描(扫描仪=>
// {
//扫描器。装夹总成();
//扫描器.连接实现到类型关闭(类型关闭(IRepository));
// });
}
}
假设您的存储库是在应用程序的其他程序集中定义的,您可以使用注册表将其连接在一起。查看此帖子: