依赖注入与自定义会员提供商 [英] Dependency Injection with Custom Membership Provider

查看:105
本文介绍了依赖注入与自定义会员提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现自定义成员资格提供者的ASP.NET MVC Web应用程序。自定义成员资格提供者为其构造函数提供一个 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屋!

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