I have three projects
- Application.Infrastructure
- Application.A (has reference from
Application.Infrastructure
) - Application.B (has reference from
Application.Infrastructure
) - Application.Web (has reference from all)
In Application.Infrastructure
i have a generic repository class
public interface IRepository<T>
{
T FirstOrDefault(Expression<Func<T, bool>> where);
}
In Application.A
i have an implementation of this repository
public class ApplicationARepository<T> : IRepository<T>
{
private readonly IApplicationADBContext _context;
public ApplicationARepository(IApplicationADBContext context)
{
_context = context;
}
// implementation
}
In Application.B
i have another implementation of the repository interface
public class ApplicationBRepository<T> : IRepository<T>
{
private readonly IApplicationBDBContext _context;
public ApplicationBRepository(IApplicationBDBContext context)
{
_context = context;
}
// implementation
}
In Application.Web i bind the interfaces using Ninject
// Bind implementations from Application.A
kernel.Bind<IApplicationADBContext>().To<ApplicationADBContext>().InRequestScope();
kernel.Bind(typeof(IRepository<>)).To(typeof(ApplicationARepository<>));
// Bind implementations from Application.B
kernel.Bind<IApplicationBDBContext>().To<ApplicationBDBContext>().InRequestScope();
// Here should fail. I already binded typeof(IRepository<>) to typeof(ApplicationARepository<>)
kernel.Bind(typeof(IRepository<>)).To(typeof(ApplicationBRepository<>));
Even if i bind the same interface to two different types, without specifying any .Where() clause, it is working and i don't get any errors.
Why? How Ninject knows how to differentiate them?
BDB
andADB
... – Simon Whitehead