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);
}
私のソリューションでは、他に2つのプロジェクトがあります
- アプリケーション.Web
- Application.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<>));
}