Ninject用的MembershipProvider | RoleProvider [英] Ninject with MembershipProvider | RoleProvider

查看:128
本文介绍了Ninject用的MembershipProvider | RoleProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ninject作为我的IoC和我写了一个角色提供程序如下:

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 */
}

我读提供类被实例化ninject获得注入实例之前。我怎么去解决这个?目前,我有这个ninject code:

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();

从这个答案<一个href=\"http://stackoverflow.com/questions/4123092/using-ninject-with-membership-provider/4124404#4124404\">here.

如果你用标记[注入]你的依赖于你的提供商类的属性,可以调用kernel.Inject(MemberShip.Provider) - 这将赋予所有依赖于你的属性

我不明白这一点。

推荐答案

我相信ASP.NET框架的这一方面非常多配置驱动。

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

有关你最后的评论,他们的意思是,不是依靠构造器注入(正在创建组件时发生),则可以使用setter注入来代替,例如:

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.

这篇关于Ninject用的MembershipProvider | RoleProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆