custom membership provider and unity dependency injection
H

1

8

I have found a few questions similar to the one I'm posting but I'm not getting from them what I really need. I'm still struggling to implement my CustomMembershipProvider using Microsoft Unity DI.

Custom Membership:

public class CustomMembershipProviderService : MembershipProvider
    {
        private readonly IUserService _userService;

        public CustomMembershipProviderService(IUserService userService)
        {
            this._userService = userService;
        }

        public override string ApplicationName
        {
        ...

User Service:

public class UserService : IUserService
    {
        private readonly IUserRepository _repository;
        private readonly IUnitOfWork _unitOfWork;
        public UserService(IUserRepository repository, IUnitOfWork unitOfWork)
        {
            this._repository = repository;
            this._unitOfWork = unitOfWork;
        }
        ...

AccountController:

public class AccountController : Controller
{
     // next line is what I don't feel too sure about what to do?
     // shouldn't my controller constructor use an Interface as its property?

     private readonly CustomMembershipProviderService _customMembershipProviderService;

     public AccountController(CustomMembershipProviderService customMembershipProviderService)
     {
         this._customMembershipProviderService = customMembershipProviderService;
     }
     ...

How can I create an interface for my MembershipProvider class?

I've tried:

public interface ICustomMembershipProvider : MembershipProvider

but I don't think that works, so I'm stuck, and don't know how to implement the MembershipProvider using my repositories, UoW, services, and Unity DI

Hundredweight answered 18/5, 2013 at 16:42 Comment(0)
C
8

Answer to the second question

The issue with the MembershipProvider and RoleProvider is that they are not built by the container, but by the framework. AFAIK the only way to set the active membership provider is the web.config file. So, if you want to use the container to resolve dependencies like repositories, services etc. via the container, you have to use the good old (I'm kidding) Service Locator pattern.

public class CustomMebershipProvider : ICustomMebershipProvider  {
   public bool ValidateUser(string user, string pwd) {
     var svc = Global.Container.Resolve<IMyService>();
     /* ... */
   }
}

To the first question

You can still inject the active membership provider in the controllers using Constructor Injection. First of all, create an interface with all methods you need. The issue here is that MembershipProvider is a concrete class, so you can't inherit an interface from it. Just create your new interface, copying method signatures of function you need from MembershipProvider:

public interface ICustomMembershipProvider {
    bool MyMethod();


    /* From MembershipProvider */
    bool ValidateUser(string username,  string password);
    /* ... */
}

Then let your provider implement it and inject:

Container
  .RegisterType<ICustomMembershipProvider>(
     new InjectionFactory(c => (ICustomMembershipProvider) Membership.Provider))

Of course, you still have to register your custom membership provider in the web.config.

Cajolery answered 20/5, 2013 at 6:32 Comment(3)
Based on your response then, my AccountController would take the ICustomMembershipProvider as a parameter in the constructor?Hundredweight
Sorry, one more question, couldn't my CustomMembershipProvider, have a constructor that includes the IService? or do I have to use the Global.Container.Resolve<IMyService>() in every method?. Is there another approach other than the service locator pattern?Hundredweight
IFAIK you can't because CustomMembershipProvider is not created by the container, but by the ASP.NET framework at application start.Cajolery

© 2022 - 2024 — McMap. All rights reserved.