Using Ninject with Membership.Provider
Asked Answered
S

4

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?

Solanaceous answered 8/11, 2010 at 10:40 Comment(1)
please review the answers and reward the best one if you canGlib
G
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>();
Glib answered 23/4, 2012 at 12:38 Comment(2)
How do you do this in WebForms instead of MVC?Stimulative
This is a better solution than direct ninject kernel use.Hanukkah
S
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.

Solanaceous answered 28/1, 2011 at 15:53 Comment(2)
What is NinjectHelper? I can't find it. Do I need to create it by myself?Keelykeen
+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.PostApplicationStartMethodMindi
B
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.

Barroom answered 8/11, 2010 at 13:47 Comment(1)
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.Caritacaritas
A
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.

Alberik answered 8/11, 2010 at 11:13 Comment(1)
This does not answer the question. He is asking how to create the provider instance, not how to inject the provider into something else.Barroom

© 2022 - 2024 — McMap. All rights reserved.