物业注射自定义成员资格提供使用城堡 [英] Property injection in custom membership provider using Castle

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

问题描述

因此​​,在阅读有关自定义成员资格提供注射的可能性远,我发现了两种可能的方式做到这一点:

So far in reading about possibilities of injection to custom membership providers, I've found two possible ways to do it:

是下列其中一项:的 http://bugsquash.blogspot.com/2010/11/windsor-managed-membershipproviders。 HTML

下面笔者基本上建议注册您的自定义提供程序,然后有一个相当可疑的温莎适配器会员(我真的不喜欢的方式它采用集装箱从的HttpApplication 变,它最终与温莎适配器包),它的实例提供商。

Here the author basically suggests to register your custom provider and then have a rather questionable windsor adapter for membership (I don't really like the way in which it instances your provider using a container it gets from HttpApplication, which it ultimately wraps with the windsor adapter).

这是另一个类似的选项:的 http://code.google.com/p/webdotnet/source/browse/trunk/Steeg.Framework/Web/Security/MemberShipProvider.cs?r=2

This is another, similar option: http://code.google.com/p/webdotnet/source/browse/trunk/Steeg.Framework/Web/Security/MemberShipProvider.cs?r=2

,你只覆盖初始化()并手动实例的依赖关系在那里。至少在第一个,你不用手(不是提供者本身除外)需要实例的依赖关系。

Where you just override Initialize() and manually instance the dependencies there. At least in the first one, you don't need to instance dependencies by hand (other than the provider itself).

有那么几个也建议使用一个某种类型的服务定位器(MVC的或其他方式)

There are then a few which also suggest using a service locator of some kind (MVC's or otherwise)

然后我遇到ninject上 Membership.Provider 而轻松的东西,如 _kernel.Inject(Membership.Provider)。这更接近我想要的东西,保持它吸引我首先使用DI组成根的概念。

Then I've come across ninject injecting dependencies to properties on Membership.Provider rather easily with something like _kernel.Inject(Membership.Provider). This is much closer to what I want, keeping the composition root concept which drew me to using DI in the first place.

我如何使用城堡达到类似的结果呢?

How can I achieve a similar result using castle?

更新:显然这与生命周期管理的问题。
注入库与Ninject 定义成员资格提供

Update: apparently this has issues with lifecycle management. Inject repository to custom membership provider with Ninject

我应该去选项#1呢?至少在我的实例提供者自己。所以生命周期管理不应该是一个问题。

Should I go for option #1 then? At least with that I instance the provider myself. So lifecycle management shouldn't be an issue.

推荐答案

我不会隐式属性注射去,因为容器将跳过在财产,如果类型不是代替快速失败(详细信息的这里)。相反,我会用服务定位器,但写不包含任何逻辑的包装成员提供,而只是从服务定位器请求成员资格提供程序,并调用执行。 乔纳斯Gauffin 写的为MVC 这样的事情(使用 DependencyResolver 服务定位器),这是相当不错的(也是<一个HREF =htt​​p://nuget.org/packages/griffin.mvccontrib相对=nofollow>可用的NuGet ),但它是容易做到的自己。

I wouldn't go with implicit property injection, since the container will skip over the property if the type is not (correctly) registered, instead of failing fast (more info here). Instead I would use the Service Locator, but write a wrapper membership provider that does not contain any logic, but simply requests a membership provider from the service locator and calls that implementation. Jonas Gauffin wrote such a thing for MVC (using the DependencyResolver service locator), which is quite nice (also available on NuGet), but it is easy to do yourself.

虽然使用的服务定位被认为是反模式,请记住,会员制模式必须通过web.config文件进行配置,该系统不使用的这部分 DependencyResolver .Current 本身。还写这样的 DependencyResolverMembershipProvider 是只是有点力学的,可以被认为不应用程序的根组成的一部分。致电组合物根内的容器是没有问题的。

Although the use of the service locator is considered an anti-pattern, please remember that the membership model must be configured through the web.config and this part of the system does not use the DependencyResolver.Current itself. Also writing such a DependencyResolverMembershipProvider is just a bit of mechanics and can be considered part of the composition root, not of the application. Calling the container inside your composition root is not a problem.

这篇关于物业注射自定义成员资格提供使用城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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