Noah Blumenthal's Blog

February 19, 2009

Custom MembershipProvider using Repository & Dependency Injection pattern magic

Filed under: Uncategorized — noahblu @ 12:15 am
Tags: ,

I’m creating a custom MembershipProvider for a site I’m working on.  I’m not a huge fan of the way these Providers were implemented, but let’s put that aside because using Providers can save a lot of time.  When creating anything we want to make sure 1) it’s testable and 2) it’s easy to change.

It took me a little bit of prodding to figure this one out, so let me show you how I did it.

Here’s a peek at my custom Membership Provider class:

public class PropertyFalconMembershipProvider : MembershipProvider
private IMembershipRepository repository;

Notice that repository?  Nice!  Now if you’re like me, you’re probably thinking “ok, so you can create a constructor that takes an IMembershipRepository and you’re done!”.  You might think that, but then you’d be wrong.  Because when ASP.NET magically instantiates your MembershipProvider after pulling its data from your Web.Config, it’s going to call the Initialize() method.  So it’s a little more complicated.  Here’s how I did it:

public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
MembershipRepositoryFactory factory = new MembershipRepositoryFactory();
repository = factory.GetRepository(name);
base.Initialize(name, config);

What’s that MembershipRepositoryFactory?  Well it’s just a way of pulling the correct IMembershipRepository at runtime (using StructureMap but you can use any IoC tool you wish):

public class MembershipRepositoryFactory
public IMembershipRepository GetRepository(string ProviderName)
return ObjectFactory.GetNamedInstance<IMembershipRepository>(ProviderName);

That’s it.  Now you have a clean, testable MembershipProvider!

Just for completeness, here’s a test that shows the functionality:

public void Can_Get_User()
IMembershipRepository repository = MockRepository.GenerateStub<IMembershipRepository>();
User user = MockRepository.GenerateMock<User>();
user.Email = “Username”;
repository.Expect(x => x.GetUser(“Username”)).Return(user);
StructureMap.ObjectFactory.Inject<IMembershipRepository>(“MyCustomMembershipProvider”, repository);
MembershipProvider provider = new MyCustomMembershipProvider();
provider.Initialize(“PropertyFalconMembershipProvider”, new System.Collections.Specialized.NameValueCollection());
MembershipUser membershipUser = provider.GetUser(“Username”, false);
Assert.AreEqual(user.Email, “Username”);


1 Comment »

  1. Very click. I love seeing other people use the Factory approach instead of Dependency Injection; because sometimes, you cannot cleanly implement DI – as you demostrated here.

    Good job!

    Comment by Eric Duncan — February 24, 2009 @ 3:41 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: