我不知道如何使命名依赖与统一,以遵循不同的解决途径。所以如果我有
public interface IService
{
    SomeMethod();
}
public class Service : IService
{
    private readonly IRepository repository;
    public Service(IRepository repository)
    {
    this.repository = repository;
    }
    public SomeMethod 
    { 
        //some implementation here
    }
}在我的下面有一个仓库: IRepository,NHibernateContext : INHibernateContext,ISession等等。
我的问题是,如果我在我的Global.asax做下一步:
container.RegisterType<IService, Service>("GlobalContext");然后,如何使它在“NHibernateContext”路径(不使用默认注册类型)中注入GlobalContext(或其他层次依赖关系)?
非常感谢你帮忙。
发布于 2014-11-21 10:36:36
过了很长一段时间,我在其他项目上也有了类似的需求,但我使用的是温莎城堡。按照我现在的做法,我需要在某些应用程序路径中使用不同的依赖解决方案,我将使用一个子容器。
发布于 2013-08-14 21:16:53
当使用这样的命名注册,你不能再依赖于自动布线,所以你需要对你的注册更加具体。所以假设你有这些:
container.RegisterType<INHibernateContext, NHibernateContext>("GlobalContext");
container.RegisterType<ISession, NHibernateSession>("GlobalContext");当您解析IRepository时,"GlobalContext“需要注入这些特定的依赖项。假设您有一个接受这两个参数的构造函数,您可以显式地告诉容器要使用哪个名称:
container.RegisterType<IRepository, Repository>("GlobalContext",
    new InjectionConstructor(
        new ResolvedParameter<INHibernateContext>("GlobalContext"),
        new ResolvedParameter<ISession>("GlobalContext")
    )
);这告诉容器使用接受INHibernateContext和ISession的构造函数,通过容器解析这些参数,并在解析它们时使用GlobalContext名称。
同样,要连接您的服务:
container.RegisterType<IService, Service>("GlobalContext",
    new ResolvedParameter<IRepository>("GlobalContext")
);最后的决心是:
container.Resolve<IService>("GlobalContext");应该以你想要的方式构建你的对象图。
https://stackoverflow.com/questions/18192254
复制相似问题