I’m using Autofac with ASP.NET MVC so all my controllers have their dependencies resolved nicely. But I have a custom membership provider with two dependencies. I suspect that the code to instantiate the membership provider is deep in ASP.NET, miles away from Autofac.
Is there a way to get Autofac to resolve my custom membership provider?
I assume that calling something like .Resolve
Here is the membership provider and its constructor :
public class MongoDBMembershipProvider : MembershipProvider
{
private IEmailService m_oEmailService;
private IUserRepository m_oUsers;
public MongoDBMembershipProvider() {
}
public MongoDBMembershipProvider( IEmailService oEmailservice, IUserRepository oUserRepository) {
m_oEmailService = oEmailservice;
m_oUsers = oUserRepository;
}
public override void Initialize( string name, NameValueCollection config) {
//TODO: Let Autofac do this work... how?
m_oEmailService = new EmailService();
m_oUsers = new Users();
}
public override bool ValidateUser( string sUsername, string sPassword) {
return m_oUsers.ValidateAttemptedLogon( sUsername, sPassword);
}
public override string GetUserNameByEmail( string sEmailAddress) {
return m_oUsers.GetUsernameByEmail( sEmailAddress);
}
}
Even though asp.net membership providers are static, you can still wrap them in an interface. The one that I have been using is here:
http://gpsnerd.codeplex.com/SourceControl/changeset/view/03279dbfcef5#Infrastructure%2fMembership%2fIMembershipService.cs
You would then create an implementation of this interface that uses the asp.net membership. Then you can let your ioc bind this interface to the implementation. By extracting out this interface, now you can test your code using a fake.
bob