依赖注入使用自定义成员资格提供 [英] Dependency Injection with Custom Membership Provider

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

问题描述

我有一个实现一个自定义的成员提供一个ASP.NET MVC的Web应用程序。自定义成员提供程序需要一个 UserRepository 给它的构造,提供了成员资格提供程序和NHibernate之间的接口。在 UserRepository 由Ninject IoC容器提供的。

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.

很明显,但是,这并不时提供由.NET运行实例:参数的构造函数没有一个UserRepository,不能创建一个(UserRepository需要NHibernate的会议上传递给它的构造函数),然后手段提供程序不能访问其数据存储。我怎样才能解决我的对象依赖?

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?

这也许值得注意的是,这是已经加装Ninject现有的应用程序。 previously我使用了能够与参数化构造函数结合使用,创建自己需要的依赖,以协助单元测试参数构造函数。

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?

推荐答案

您可能想保持参数的构造函数,它初始化使用Ninject所需要的资料库。您可能需要使用公共服务定位​​,所以你不会需要既没有参考到Ninject也不是你的自定义提供容器内。这似乎Ninject没有一个正式的CSL适配器实现,但写作时不应该硬(检查其他的实现,如温莎的),我敢肯定有一个非官方的实现的某个地方。

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天全站免登陆