0

Trying to inject a repository into my CustomMembershipProvider.

Let me start that I am not in fond of the Singleton pattern or some other global public solutions.

Here is a sample of the CustomMemberhipProvider

public class CustomMembershipProvider : BaseMembershipProvider
{
    private readonly UserRepository _userRepository;

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        // Magically pull the userrepository in here?
        base.Initialize(name, config);
    }
}

The only working example I found so far is to add a Factory, which is yet-another-alternative for some global public class.

  • Is there nothing out there that allows me properly inject an object (UserRepository) to the CustomMembershipProvider?
  • If not, is there a good alternative? I am not married to the CustomMembershipProvider, so if there is something else out there that allows me to use [Authorize] attributes in my controller, then I'm all for it.

Thanks a lot in advance!

bas
  • 13,550
  • 20
  • 69
  • 146
  • Related: [Custom ResourceProviderFactory Dependency Injection](http://stackoverflow.com/questions/10619962/custom-resourceproviderfactory-dependency-injection) – Steven Feb 10 '13 at 13:03
  • So the best you can get here is some kind of global public repository provider? – bas Feb 10 '13 at 13:16
  • In MVC there already is an global public provider, it is called `DependencyResolver.Current`. It's not advisable to call it from within your application logic (since this makes testing harder), but it's okay to call it from within some piece of MVC infrastructure logic. My advice: Create your `MvcMembershipProvider` just like the `MvcResourceProviderFactory` and call `DependencyResolver.Current.GetService(typeof(MembershipProvider))` to resolve the real provider. Or use the Griffin framework as referenced in the duplicate question. – Steven Feb 10 '13 at 16:43
  • Another solution is to write your own AuthorizeAttribute, like http://www.diaryofaninja.com/blog/2011/07/24/writing-your-own-custom-aspnet-mvc-authorize-attributes . You can use dependency injection for asp.net action filters so I think that you can use it also for authorize attribute. – Davide Icardi Feb 10 '13 at 23:01
  • Cool, bookmarked that. Thanks for the help! – bas Feb 10 '13 at 23:08

0 Answers0