注射库与Ninject定义成员资格提供 [英] Inject repository to custom membership provider with Ninject
问题描述
我想在MVC 3注入的存储库与ninject一个自定义的成员提供。
I'm trying to inject a repository to a custom membership provider with ninject in MVC 3.
在我的MembershipProvider曾尝试以下内容:
In MembershipProvider I have tried the following:
[Inject]
public ICustomerRepository _customerRepository{ get; set; }
和
[Inject]
public TUMembershipProvider(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
在我ninject模块我尝试了以下内容:
In my ninject module i tried the following:
Bind<MembershipProvider>().ToConstant(Membership.Provider);
以上作品都没有。
None of the above works.
当我使用(Global.asa中)
When i use(in global.asa)
kernel.Inject(Membership.Provider);
与
[Inject]
public ICustomerRepository _customerRepository{ get; set; }
它的工作原理,但我没有生命周期管理,这将导致从NHibernate的一个的ISession是开放的错误,因为的ISession是InRequestScope和仓库不是。
it works, but i have no life cycle management and this will cause a "ISession is open" error from NHibernate, because the ISession is InRequestScope and the repository is not.
推荐答案
您可以使用这种方法的 @圣雷莫Gloor在他的供应商注入博客文章概述。它包括3个步骤:
You could use the approach @Remo Gloor outlines in his blog post on provider injection. It involves 3 steps:
-
添加
[注入]
s到你需要注入你的供应商的任何属性(虽然他显示模式 - 创建一个非常简单的类,它的唯一功能是成为财产注射容器内携带,并转发给使用构造器注入来实现真正的类中的任何请求 - 是非常值得以下)
Add
[Inject]
s to any properties on your provider you need injected (although the pattern he shows -- creating a very simple class whose only function is to be a receptable for property injection and forwards any requests to a real class implemented using constructor injection -- is well worth following)
public class MyMembershipProvider : SqlMembershipProvider
{
[Inject]
public SpecialUserProvider SpecialUserProvider { get;set;}
...
创建初始包装实现 IHttpModule的
这在拉动提供商,触发它的创作: -
Create an initializer wrapper that implements IHttpModule
which pulls the provider in, triggering its creation:-
public class ProviderInitializationHttpModule : IHttpModule
{
public ProviderInitializationHttpModule(MembershipProvider membershipProvider)
{
}
...
注册 IHttpModule的
在 RegisterServices
: -
kernel.Bind<IHttpModule>().To<ProviderInitializationHttpModule>();
有没有4; Ninject没有休息 - 自举所有注册 IHttpModules
包括在启动过程中添加的一个)
there is no 4; Ninject does the rest - bootstrapping all registered IHttpModules
including the one you added) during the startup sequence.
(不要忘记阅读博客文章重新寿命等方面的意见。)
(Don't forget to read the comments on the blog post re lifetimes etc.)
最后,如果你正在寻找的东西完全新空房禁地直接说巧妙地解决它,尝试这个@Remo Gloor回答,而不是
Finally, if you're looking for something completely braindead direct that solves it neatly, try this @Remo Gloor answer instead
PS上全乱了很大的书面记录是提供商是不是一个模式由@马克塞曼。 (和他的优秀著作的oboligatory插头: - 依赖注入.NET ,这将让你从第一原理出舒适搞清楚这个东西)
PS a great writeup on the whole mess is Provider is not a Pattern by @Mark Seemann. (and the oboligatory plug for his excellent book:- Dependency injection in .NET which will have you figuring this stuff out comfortably from first principles)
这篇关于注射库与Ninject定义成员资格提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!