Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom membership provider and unity dependency injection

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

like image 523
SOfanatic Avatar asked May 18 '13 16:05

SOfanatic


1 Answers

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.

like image 62
onof Avatar answered Nov 03 '22 14:11

onof