繁体   English   中英

如何在StructureMap DI和MVC 5中解析相同类型的多个实现

[英]How to resolve multiple implementations of the same type in StructureMap DI and MVC 5

我有一个由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 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 
}

现在在我的StructureMap代码中

==================================================

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
}

==================================================

在客户控制器类中,我定义如下:

public class CompanyController : Controller
{
    readonly ICompanyRepository _companyRepository1;
    readonly ICompanyRepository _companyRepository2;
    readonly ICompanyRepository _companyRepository3;
    public CompanyController(ICompanyRepository comRep1,ICompanyRepository comRep2, ICompanyRepository comRep3)
    {
        _companyRepository1 = comRep1;
        _companyRepository2 = comRep2;
        _companyRepository2 = comRep3;
    }
}

================================================== ======

现在默认情况下,它仅从comRep1中加载所有三个(comRep1,comRep2,comRep3)的数据

我在这里想念什么吗?

还有一个问题:我的接口是由10个类实现的,所以我应该像下面那样指定所有10个类和命名实例吗?

For<ICompanyRepository>().Add<CompanyRepository1>().Named("comRep1");
For<ICompanyRepository>().Add<CompanyRepository2>().Named("comRep2");
......
For<ICompanyRepository>().Add<CompanyRepository3>().Named("comRep10");

解决此问题的典型方法是创建一个通用存储库。 通用存储库使您不必为每个实体一遍又一遍地重写相同的CRUD代码。

通用存储库

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM