How to inject IUserRepository into custom membership provider using Ninject?
Question
I have DI set up in my project using Ninject. Here is an example for the UserController.
private readonly IRoleRepository roleRepository;
private readonly IUserRepository userRepository;
public UserController(IUserRepository userRepository, IRoleRepository roleRepository)
{
this.roleRepository = roleRepository;
this.userRepository = userRepository;
}
In my NinjectWebCommon.cs file I have this:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IControllerFactory>().To<DefaultControllerFactory>();
kernel.Bind<IUnitOfWork>().To<PortalContext>().InSingletonScope();
kernel.Bind<IUserRepository>().To<UserRepository>();
kernel.Bind<IRoleRepository>().To<RoleRepositoroy>();
}
However, I can't seem to figure out how to inject the user and role repositories into the custom membership provider. Any help is much appreciated as I have hit a wall here. I am using Asp.net MVC4 and EF4 with the repository and unit of work patterns. Thanks.
Solution
As a simple solution you could just property inject them with Ninject.
[Inject]
public IUserRepository UserRepository {get;set;}
OTHER TIPS
You can use property injection
[Inject]
public IUserRepository userRepository {get;set;};
You will have to be careful regarding the scope of your DBContext though, as the MembershipProvider class is a singleton whose scope is the lifetime of the application.
@feanz, regarding your question:
@BBauer42 odd. IS the provider in another project to your MVC application?
What would you have to do different if the custom provider is in a separate project (class library)?