We’re using Ninject for IOC.
All of our Repository objects can (and should be) mocked for unit testing. I’d like to enforce that all developers code only to interfaces when interacting with Repositories. For this, I’d like to make the constructors private and create static accessor factory methods for construction:
public class SomeRepository : ISomeRepository
{
private SomeRepository()
{
}
public static ISomeRepository Create()
{
return StandardKernel.Get<ISomeRepository>();
}
}
Problem lies in this: how do I have Ninject create the actual object? I have repository interface and class in the same project
We’re ultimately going with the following:
and during module loading, we’re mapping the StandardKernel’s ISomeRepository interface to CreateForIOC() method.
This does not stop developers from calling CreateForIOC directly, but at least forces them to a) code to an interface, b) realize that CreateForIOC() is probably not the right method to call when instantiating the object and at least ask a question about it from a lead developer