Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject with MembershipProvider | RoleProvider

I'm using ninject as my IoC and I wrote a role provider as follows:

public class BasicRoleProvider : RoleProvider
{
    private IAuthenticationService authenticationService;

    public BasicRoleProvider(IAuthenticationService authenticationService)
    {
        if (authenticationService == null) throw new ArgumentNullException("authenticationService");
        this.authenticationService = authenticationService;
    }

    /* Other methods here */
}

I read that Provider classes get instantiated before ninject gets to inject the instance. How do I go around this? I currently have this ninject code:

Bind<RoleProvider>().To<BasicRoleProvider>().InRequestScope();

From this answer here.

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.

I do not understand this.

like image 552
Shawn Mclean Avatar asked Jan 10 '11 18:01

Shawn Mclean


1 Answers

I believe this aspect of the ASP.NET framework is very much config driven.

For your last comment, what they mean is that instead of relying on constructor injection (which occurs when the component is being created), you can use setter injection instead, e.g:

public class BasicRoleProvider : RoleProvider
{
  public BasicRoleProvider() { }

  [Inject]
  public IMyService { get; set; }
}

It will automatically inject an instance of your registered type into the property. You can then make the call from your application:

public void Application_Start(object sender, EventArgs e)
{
  var kernel = // create kernel instance.
  kernel.Inject(Roles.Provider);
}

Assuming you have registered your role provider in the config. Registering the provider this way still allows great modularity, as your provider implementation and application are still very much decoupled.

like image 63
Matthew Abbott Avatar answered Nov 09 '22 11:11

Matthew Abbott