依赖注入与自定义会员提供商 [英] Dependency Injection with Custom Membership Provider
问题描述
UserRepository
,该构造函数提供了成员资格提供者和NHibernate之间的接口。 UserRepository
由Ninject IoC容器提供。 然而,显然,当提供程序由.NET实例化时,这不起作用:无参数构造函数没有UserRepository,并且无法创建一个(UserRepository需要NHibernate会话传递给它的构造函数),这意味着提供者无法访问其数据存储。我如何解决我的对象依赖关系?
值得注意的是,这是一个已经与Ninject进行了改进的现有应用程序。以前,我使用无参数构造函数,可以与参数化构造函数一起创建所需的依赖关系,以协助单元测试。
任何想法,还是将自己建立在这里的一个角落?
您可能希望保留无参数的构造函数,使用Ninject初始化所需的存储库。您可能需要使用通用服务定位器,因此您不需要对Ninject没有引用也不是您的定制提供商中的容器。似乎Ninject没有CSL的官方适配器实现,但是写一个应该不是很难(检查其他实现,如Windsor),我确定在某个地方有一个非官方的实现。
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?
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.
这篇关于依赖注入与自定义会员提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!