asp.net-mvcentity-frameworkrepository-patternunit-of-workcustom-membershipprovider

How to inject IUserRepository into custom membership provider using Ninject?


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;}