Sha*_*ean 7 asp.net-mvc ninject asp.net-mvc-3
我正在使用ninject作为我的IoC,我编写了一个角色提供程序,如下所示:
public class BasicRoleProvider : RoleProvider
{
private IAuthenticationService authenticationService;
public BasicRoleProvider(IAuthenticationService authenticationService)
{
if (authenticationService == null) throw new ArgumentNullException("authenticationService");
this.authenticationService = authenticationService;
}
/* Other methods here */
}
Run Code Online (Sandbox Code Playgroud)
我读到Provider在ninject注入实例之前,类被实例化了.我该如何解决这个问题?我目前有这个ninject代码:
Bind<RoleProvider>().To<BasicRoleProvider>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
从这个答案在这里.
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.
我不明白.
我相信ASP.NET框架的这个方面是非常配置驱动的.
对于你的最后一条评论,它们的含义是,不是依赖于构造函数注入(在创建组件时发生),而是可以使用setter注入,例如:
public class BasicRoleProvider : RoleProvider
{
public BasicRoleProvider() { }
[Inject]
public IMyService { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它会自动将您注册类型的实例注入属性.然后,您可以从您的应用程序拨打电话:
public void Application_Start(object sender, EventArgs e)
{
var kernel = // create kernel instance.
kernel.Inject(Roles.Provider);
}
Run Code Online (Sandbox Code Playgroud)
假设您已在配置中注册了角色提供程序.以这种方式注册提供程序仍然允许很好的模块化,因为您的提供程序实现和应用程序仍然非常分离.
| 归档时间: |
|
| 查看次数: |
2750 次 |
| 最近记录: |