Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC MembershipProvider with repository and ninject

How do I communicate with the UserService through an overidden MembershipProvider class? I have no idea how to pass the connection string to the user repository inside the service.

This is how my app is structured:

Repository (constructor in the implementation takes a connection string)

public interface IUserRepository
{
    IQueryable<User> GetUsers();
    IQueryable<UserRole> GetUserRoles();
    void InsertUser(User user);
}

Service (Constructor takes a user repository)

public interface IUserService
{
    User GetUser(int userId);
    User GetUser(string email);
}

UserController (An example of my controller)

public class UsersController : Controller
{
    private IUserService userService;
    public UsersController(IUserService userServ)
    {
        userService = userServ;
    }
}

NinjectConfigurationModule

public class NinjectConfigurationModule : NinjectModule
{

    public override void Load()
    {
        Bind<IUserService>().To<UserService>();
        Bind<IUserRepository>().To<UserRepository>()
            .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString
            );
    }
}

NinjectControllerFactory

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel kernel = new StandardKernel(new NinjectConfigurationModule());

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        // We don't want to pass null to ninject as we'll get a strange error.
        return controllerType == null ? null
                                      : (IController)kernel.Get(controllerType);
    }
}

MembershipProvider (This is where my problem is)

public class SimpleMembershipProvider : MembershipProvider
{
     //How do I set up User Service here so that ninject can put my connection string here.
     public override bool ValidateUser(string username, string password)
     {
           //Code to use user service.
     }
}
like image 994
Shawn Mclean Avatar asked Oct 11 '10 17:10

Shawn Mclean


1 Answers

Already answered question, but I think the better answer is to make the repository a property on your MembershipProvider and inject into it at Application_Start. e.g.

public class AccountMembershipProvider : MembershipProvider
{
    [Inject]
    public IAccountRepository AccountRepository { get; set; }
    ...
}

and the injection:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        // Inject account repository into our custom membership & role providers.
        _kernel.Inject(Membership.Provider);

        // Register the Object Id binder.
        ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdModelBinder()); 
    }

I've written up a more in depth explanation here:

http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/

like image 128
DanH Avatar answered Sep 28 '22 04:09

DanH