Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Ninject:将同一接口绑定到两个实现_C#_Asp.net Mvc_Ninject_Ninject.web.mvc - Fatal编程技术网

C# Ninject:将同一接口绑定到两个实现

C# Ninject:将同一接口绑定到两个实现,c#,asp.net-mvc,ninject,ninject.web.mvc,C#,Asp.net Mvc,Ninject,Ninject.web.mvc,我有一个名为Infrastructure的项目,其中包含一个接口IRepository public interface IRepository<T> { /// <summary> /// Adds the specified entity to the respository of type T. /// </summary> /// <param name="entity">The entity to add.

我有一个名为
Infrastructure
的项目,其中包含一个接口
IRepository

public interface IRepository<T>
{
    /// <summary>
    /// Adds the specified entity to the respository of type T.
    /// </summary>
    /// <param name="entity">The entity to add.</param>
    void Add(T entity);

    /// <summary>
    /// Deletes the specified entity to the respository of type T.
    /// </summary>
    /// <param name="entity">The entity to delete.</param>
    void Delete(T entity);
}
两种实现都使用不同的
数据上下文

我怎样才能用ninject绑定这个

private static void RegisterServices(IKernel kernel)
{
    // bind IRepository for `Application.Web` project
    kernel.Bind(typeof(IRepository<>)).To(typeof(Application.Web.EFRepository<>));

    // bind IRepository for `Application.Web.Api' project
    kernel.Bind(typeof(IRepository<>)).To(typeof(Application.Web.Api.EFRepository<>));    
}
私有静态无效注册服务(IKernel内核)
{
//为'Application.Web'项目绑定IRepository
Bind(typeof(IRepository)).To(typeof(Application.Web.EFRepository));
//为“Application.Web.Api”项目绑定IRepository
Bind(typeof(IRepository)).To(typeof(Application.Web.Api.EFRepository));
}
解决这种情况有很多方法

命名绑定 最简单的是,只需提供依赖项的名称:

kernel
        .Bind(typeof(IRepository<>))
        .To(typeof(WebApiEFRepository<>))
        // named binding
        .Named("WebApiEFRepository");
上下文绑定 查找要绑定的类型在基于目标命名空间的示例中

kernel
    .Bind(typeof(IRepository<>))
    .To(typeof(WebDbRepository<>))
    // using thins binding when injected into special namespace
    .When(request => request.Target.Type.Namespace.StartsWith("Application.Web"));
内核
.Bind(类型(i假定))
.To(类型(WebDbRepository))
//在注入特殊名称空间时使用thins绑定
.When(request=>request.Target.Type.Namespace.StartsWith(“Application.Web”);
从内核获取依赖项:

// WebDbRepository<> will be injected
var serice = kernel.Get<Application.Web.Service>();
//将注入WebDbRepository
var serice=kernel.Get();

使用每个存储库时是否有一些标准?恐怕没有。它们只使用相同的接口,因此我不必为我拥有的每个项目复制IRepository接口,当您请求存储库时,Ninject如何知道应该使用这两个实现中的哪一个?它应该随机挑选一个吗?这是我的问题..我认为这可能是一种实现这一目标的方式吗???需要有一些标准来选择哪一个,否则有两个实现是没有意义的。制定这个标准,然后你就能得到帮助。此类标准的一个示例可能是:“如果需要存储库的类在程序集
应用程序.Web.Api
中定义,请使用implementation
应用程序.Web.Api.EFRepository
”。另一个标准可能是:如果需要存储库的类是从API进程实例化的,请使用
Application.Web.API.EFRepository
kernel
    .Bind(typeof(IRepository<>))
    .To(typeof(WebDbRepository<>))
    // using thins binding when injected into special namespace
    .When(request => request.Target.Type.Namespace.StartsWith("Application.Web"));
// WebDbRepository<> will be injected
var serice = kernel.Get<Application.Web.Service>();