我有一个名为 的项目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);
}
在我的解决方案中,我还有两个项目
- 应用程序.Web
- 应用程序.Web.Api
- 基础设施
两个项目,都包含IRepository
接口的实现
public class EFRepository<T> : IRepository<T>
{
// DbContext for Application.Web project
ApplicationWebDbContext _db;
public EFRepository(ApplicationWebDbContext context)
{
_db = context;
}
public void Add(T entity) { }
public void Delete(T entity) { }
}
public class EFRepository<T> : IRepository<T>
{
// DbContext for Application.Web.Api project
ApplicationWebAPIDbContext _db;
public EFRepository(ApplicationWebAPIDbContext context)
{
_db = context;
}
public void Add(T entity) { }
public void Delete(T entity) { }
}
两种实现都适用于不同的DataContexts
.
我如何在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<>));
}