Dependency injection with CodeFirstMembershipProvider()

May 29, 2012 at 5:09 PM

It is possible to realize dependency injection in the CodeFirstMembershipProvider constructor?

I'm using Unity for the dependency injection and I don't wanna generate on the constructor of the CodeFirstMembershipProvider a new instance of my user service provider.

In other words, I want to repleace:

 private readonly IUserService _userService;

 public CodeFirstMembershipProvider(){

_userService = new UserService(new UserRepository());   


to this:

 private readonly IUserService _userService;
 public CodeFirstMembershipProvider(IUserService userService){

_userService = userService;


Sorry about my English if it's bad  :)

May 29, 2012 at 5:25 PM

Current implementation is simplified as possible, no repository and DI patterns are used, and it’s a super-fast quick start to custom membership, for many but not all this is enough. You can certainly use this code and take it to the next level.

Jun 27, 2012 at 6:05 PM

I also needed this. So I refactored it.

Now it's DI ready. If you're still looking for this I can put it somewhere in the internet if you want? CodeFirst, DDD, Repository/UnitOfWork, DI.

With kind regards,

Frank vd G

Jun 27, 2012 at 6:28 PM

Fantastic job, if you have the time and want to contribute to the project I can make you coordinator/developer

In any case if you want to share this implementation you can send it to my email I will upload it

Oct 4, 2012 at 10:03 PM


I'm also interested in DI support for the CodeFirstMembershipProvider. @frankvandergeld, could you share your code with me. Could you also explain how you you instantiate the  CodeFirstMembershipProvider with DI?

Thanks in advance,