I'm working on a larger scale MVC App and have been trying to use DI with Ninject. I have the following projects:
Core - Class Library (contains a custom membership provider) Domain - Class Library (contains interfaces and EF to access data such as IUserRepository) WebUI - MVC 3 Project
I have bound IUserRepository to UserRepository in the Ninject startup within the App_Start container of my WebUI.
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserRepository>().To<UserRepository>();
}
}
However I want to use the UserRepository in my Customer Membership Provider with the Core Class Library...
public class MyMembershipProvider : MembershipProvider
{
private IUserRepository userRepository;
public MyMembershipProvider (IUserRepository repository)
{
this.userRepository = repository;
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
// Rest of methods....
However this fails as my Ninject start is within the WebUI project and presumably knows nothing about the class library. Is there a way to get it to inject the dependencies for those class libraries also?
Or should I be looking at a different way to code those and their dependencies?