C# 如何解决StructureMap DI和MVC 5中相同类型的多个实现
我有一个由4类实现的接口。在我的公司控制器类构造函数中,我注入了它 下面是我的代码:C# 如何解决StructureMap DI和MVC 5中相同类型的多个实现,c#,asp.net-mvc,dependency-injection,structuremap,C#,Asp.net Mvc,Dependency Injection,Structuremap,我有一个由4类实现的接口。在我的公司控制器类构造函数中,我注入了它 下面是我的代码: public interface ICompanyRepository { IEnumerable<Company> GetAll(); Company Get(int id); Company Add(Company item); bool Update(Company item); bool Delete(int id); } public class
public interface ICompanyRepository
{
IEnumerable<Company> GetAll();
Company Get(int id);
Company Add(Company item);
bool Update(Company item);
bool Delete(int id);
}
public class CompanyRepository1: ICompanyRepository
{
//Implemented all the methods of the interface
}
public class CompanyRepository2: ICompanyRepository
{
//Implemented all the methods of the interface
}
public class CompanyRepository3: ICompanyRepository
{
//Implemented all the methods of the interface
}
========================================================
public static class IoC
{
public static IContainer Initialize()
{
return new Container(c => c.AddRegistry<DefaultRegistry>());
}
}
public class DefaultRegistry : Registry
{
#region Constructors and Destructors
public DefaultRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
// scan.AddAllTypesOf<ICompanyRepository>();
scan.With(new ControllerConvention());
});
For<ICompanyRepository>().Add<CompanyRepository1>().Named("comRep1");
For<ICompanyRepository>().Add<CompanyRepository2>().Named("comRep2");
For<ICompanyRepository>().Add<CompanyRepository3>().Named("comRep3");
}
#endregion
}
现在,默认情况下,它只从comRep1为所有三个(comRep1、comRep2、comRep3)加载数据
这里有我遗漏的东西吗
还有一个问题:我的接口是由10个类实现的,所以我应该像下面那样指定所有10个类和命名实例吗
For<ICompanyRepository>().Add<CompanyRepository1>().Named("comRep1");
For<ICompanyRepository>().Add<CompanyRepository2>().Named("comRep2");
......
For<ICompanyRepository>().Add<CompanyRepository3>().Named("comRep10");
For().Add().Named(“comRep1”);
For().Add().Named(“comRep2”);
......
For().Add().Named(“comRep10”);
解决这个问题的典型方法是创建一个通用存储库。通用存储库使您不必为每个实体反复重写相同的CRUD代码
通用存储库
公共接口IRepository
地点:班级
{
IEnumerable GetAll();
TEntity-Get(int-id);
张力增加(张力项目);
bool更新(潜在项目);
bool-Delete(int-id);
}
公共类存储库:IRepository
地点:班级
{
//实现接口的所有方法
}
示例用法
然后,您可以使用泛型轻松选择服务中的一个存储库(尽管它们都使用相同的类)。不需要使用命名实例,因为它们是基于泛型类型设置键的
class Program
{
static void Main(string[] args)
{
var container = new Container(c =>
{
c.For<IService>().Use<Service>();
// Register the generic repository for any entity
c.For(typeof(IRepository<>)).Use(typeof(Repository<>));
});
// Resolve the service
var service = container.GetInstance<IService>();
}
}
public class Company { }
public class Employee { }
public class Timecard { }
public interface IService { }
public class Service : IService
{
public Service(
IRepository<Company> companyRepo,
IRepository<Employee> employeeRepo,
IRepository<Timecard> timecardRepo)
{
// All 3 repositories injected here
}
}
类程序
{
静态void Main(字符串[]参数)
{
var容器=新容器(c=>
{
c、 For().Use();
//为任何实体注册通用存储库
c、 用于(typeof(IRepository))。使用(typeof(Repository));
});
//解决服务问题
var service=container.GetInstance();
}
}
公共类公司{}
公共类雇员{}
公共类时间卡{}
公共接口IService{}
公共课服务:IService
{
公共服务(
I储蓄公司REPO,
I临时雇员EPO,
i存储时间卡(REPO)
{
//所有3个存储库都在这里注入
}
}
为什么需要这么多公司存储库?这东西有点臭。如果您是说每个实体类型都有一个存储库,那就更有意义了。它是每个实体类型的存储库
public interface IRepository<TEntity>
where TEntity : class
{
IEnumerable<TEntity> GetAll();
TEntity Get(int id);
TEntity Add(TEntity item);
bool Update(TEntity item);
bool Delete(int id);
}
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : class
{
// Implement all the methods of the interface
}
class Program
{
static void Main(string[] args)
{
var container = new Container(c =>
{
c.For<IService>().Use<Service>();
// Register the generic repository for any entity
c.For(typeof(IRepository<>)).Use(typeof(Repository<>));
});
// Resolve the service
var service = container.GetInstance<IService>();
}
}
public class Company { }
public class Employee { }
public class Timecard { }
public interface IService { }
public class Service : IService
{
public Service(
IRepository<Company> companyRepo,
IRepository<Employee> employeeRepo,
IRepository<Timecard> timecardRepo)
{
// All 3 repositories injected here
}
}