12

I'm new to Ninject and I'm having problems using it with a custom membership provider.

My membership provider has a repository interface passed in. It looks like:

public class CustomMembershipProvider : MembershipProvider
{
  public CustomMembershipProvider( IRepository repository )
  {
  }
}

I'm using the code thats part of the Account Model in the MVC app as a starting point.

However when it calls Membership.Provider I get an error saying No parameterless constructor defined for this object.

I've setup the bindings in ninject to bind a IRepository to a Repository class which work as I've testing this in a controller.

What are the correct bindings in Ninject to use for Membership.Provider?

tereško
  • 58,060
  • 25
  • 98
  • 150
lancscoder
  • 8,658
  • 8
  • 48
  • 68

4 Answers4

10

This is how it should be done today with new versions of both MVC and Ninject (version 3):

You have access to the DependencyResolver instance and Ninject sets itself as the current DependencyResolver. That way you don't need hacks to get access to the static Ninject kernel. Please note, my example uses my own IUserService repository for Membership...

IUserService _userService = DependencyResolver.Current.GetService<IUserService>();
mare
  • 13,033
  • 24
  • 102
  • 191
9

The best solution I found was the following:

private IRepository _repository;

[Inject]
public IRepository Repository
{
    get { return _repository; }
    set { _repository= value; }
}

public CustomMembershipProvider()
{
    NinjectHelper.Kernel.Inject(this);
}

Where NinjectHelper is a static helper class to get the Kernal from.

lancscoder
  • 8,658
  • 8
  • 48
  • 68
  • 4
    What is NinjectHelper? I can't find it. Do I need to create it by myself? – Tomas Apr 13 '12 at 08:50
  • +1 for using the attribute, but not for using the "helper". You're doing the Service Locator antipattern here (sort of). You can call the "Inject" method in the WebActivator.PostApplicationStartMethod – Matteo Mosca Oct 12 '12 at 06:42
5

Since the membership collection and the Membership.Provider instance are created before Ninject can instantiate them, you need to perform post creation activation on the object. If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(Membership.Provider) - this will assign all dependencies to your properties.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Ian Davis
  • 3,848
  • 1
  • 24
  • 30
  • One draw back to this is that it creates a Ninject dependency on the provider. Not a huge deal, but if your provider is part of a library meant to have as few external dependencies as possible, then it's no fun. – crush Nov 20 '14 at 21:16
0

I haven't used Ninject ever. but in StructureMap i set this dependency:

expression.For<MembershipProvider>().Add(System.Web.Security.Membership.Provider);

and it works fine.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Eugene Gluhotorenko
  • 3,094
  • 2
  • 33
  • 52
  • 2
    This does not answer the question. He is asking how to create the provider instance, not how to inject the provider into something else. – Ian Davis Nov 08 '10 at 13:48