自定义成员资格提供程序的依赖注入
我有一个 ASP.NET MVC Web 应用程序,它实现了自定义成员资格提供程序。自定义成员资格提供程序将 UserRepository
添加到其构造函数中,该构造函数提供成员资格提供程序和 NHibernate 之间的接口。 UserRepository
由 Ninject IoC 容器提供。
然而,显然,当提供程序由 .NET 实例化时,这不起作用:无参数构造函数没有 UserRepository 并且无法创建 UserRepository(UserRepository 需要将 NHibernate 会话传递给其构造函数),这意味着提供程序无法访问其数据存储。如何解决我的对象依赖性?
可能值得注意的是,这是一个已使用 Ninject 进行改造的现有应用程序。以前,我使用无参数构造函数,它们能够与带参数构造函数结合创建所需的依赖项,以协助单元测试。
有什么想法,还是我把自己逼到了一个角落?
I have an ASP.NET MVC web application that implements a custom membership provider. The custom membership provider takes a UserRepository
to its constructor that provides an interface between the membership provider and NHibernate. The UserRepository
is provided by the Ninject IoC container.
Obviously, however, this doesn't work when the provider is instantiated by .NET: the parameterless constructor does not have a UserRepository and cannot create one (the UserRepository requires an NHibernate session be passed to its constructor), which then means that the provider cannot access its data store. How can I resolve my object dependency?
It's probably worth noting that this is an existing application that has been retrofitted with Ninject. Previously I used parameterless constructors that were able to create their required dependencies in conjunction with the parametered constructors to assist unit testing.
Any thoughts, or have I built myself into a corner here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能希望保留无参数构造函数,它使用 Ninject 初始化所需的存储库。您可能想要使用公共服务定位器,因此您不需要引用 Ninject 或它是您的自定义提供程序内的容器。看起来 Ninject 没有针对 CSL 的官方适配器实现,但是编写一个适配器应该不难(检查其他实现,例如 Windsor 的实现),并且我确信某个地方有一个非官方实现。
You might want to keep the parameterless constructor, that initializes the needed repositories using Ninject. You might want to use the Common Service Locator, so you won't need to have neither a reference to Ninject nor it's container inside your custom provider. It seems Ninject doesn't have an official an adapter implementation for CSL, but writing one shouldn't be to hard (check the other implementations, like Windsor's), and I'm sure there's an unofficial implementation somewhere.
Michael B. 的正确答案来自: 依赖注入和 ASP.Net会员提供者
您可以通过以下方式获取您的存储库(或服务)注入:
Correct answer from Michael B. from: Dependency injection and ASP.Net Membership Providers
You can obtain your repository (or service) injection in this way:
由于成员资格集合和 MemberShip.Provider 实例是在 Ninject 实例化它们之前创建的,因此您需要对对象执行创建后激活。如果您在提供程序类中为属性标记了 [Inject] 依赖项,则可以调用 kernel.Inject(MemberShip.Provider) - 这会将所有依赖项分配给您的属性。
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.
我遇到了同样的问题,我通过使用身份验证票证将所需数据传递给身份对象来解决它。
然后不需要将任何对象注入到成员资格提供程序中。
在我的身份验证代码中
,在 Global.asax 中
,在 Identity 类中,我有 MemberName、Firstname 和 Lastname 属性,它们返回票证字符串的一部分。
I had same problem, I solved it by passing required data using authentication ticket to identity object.
Then no objects need to be injected into membership providers.
In my authentication code I have
And in Global.asax
And in Identity class I have MemberName, Firstname and Lastname properties that return parts of ticket string.