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

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

问题描述

我有一个实现自定义成员资格提供程序的 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 进行改造的现有应用程序.以前,我使用无参数构造函数,这些构造函数能够与带参数的构造函数一起创建所需的依赖项,以协助单元测试.

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 初始化所需存储库的无参数构造函数.您可能想要使用 Common Service Locator,这样您就不需要引用 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.

这篇关于使用自定义成员资格提供程序进行依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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