User Membership Provider and Single Dependency Injection

I found several questions similar to the one I am posting, but I am not getting from them what I really need. I'm still trying to implement my CustomMembershipProvider using Microsoft Unity DI .

User 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 to create an interface for my MembershipProvider class?

I tried:

 public interface ICustomMembershipProvider : MembershipProvider 

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

+8
dependency-injection asp.net-mvc-3 unity-container repository-pattern
source share
1 answer

The answer to the second question

The problem with MembershipProvider and RoleProvider is that they are not created by the container, but by the wireframe. AFAIK, the only way to install an active member provider is through the web.config file. Thus, if you want to use the container to resolve dependencies such as repositories, services, etc. Through the container, you must use the good old (I'm joking) Locator service template.

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

To the first question

You can still enter the active controller membership provider using the Injection Constructor. First of all, create an interface with all the methods you need. The problem here is that MembershipProvider is a specific class, so you cannot inherit an interface from it. Just create your new interface, copy the signature methods of the 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 enter:

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

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

+8
source share

All Articles