问题描述
Asp.Net MVC 1.0项目模板包括的AccountController 类,它支持构造器注入:
Asp.Net MVC 1.0 project templates include an AccountController class, which supports constructor injection:
public AccountController(IFormsAuthentication formsAuth,
IMembershipService service)
{
FormsAuth = formsAuth ?? new FormsAuthenticationService();
MembershipService = service ?? new AccountMembershipService();
}
在 AccountMembershipService 还包括类,它也支持构造器注入:
An AccountMembershipService class is also included, and it too supports constructor injection:
public AccountMembershipService(MembershipProvider provider)
{
_provider = provider ?? Membership.Provider;
}
我敢肯定,你们中许多人已经使用了这些单元测试,但我的目标是要注入的的SqlMembershipProvider 使用温莎,因而使用温莎XML文件,而不是web.config文件在运行时配置。换句话说,我确实希望使用构造函数注入的AccountMembershipService类,然后我想继续使用内置在ASP.Net 2.0会员制度。我只想会员系统的配置要经过国际奥委会温莎
I'm sure many of you have used these for unit testing, but my goal is to inject a SqlMembershipProvider using Windsor, and thus configure it at runtime using Windsor xml files rather than web.config. In other words, I want to actually use constructor injection for the AccountMembershipService class, and I want to continue using the built in ASP.Net 2.0 Membership system. I just want the configuration of the Membership system to go through Windsor IoC.
这是可能没有写我自己的MembershipProvider,还是不SqlMembershipProvider的与IOC发挥好?
Is this possible without writing my own MembershipProvider, or does SqlMembershipProvider not play well with IoC?
:。该SqlMembershipProvider的构造是由ASP.NET称为在应用程序的配置中指定创建的SqlMembershipProvider类的实例,此构造方法的不打算从code使用的。
我相信,但我没有足够的经验来神交 他收到的答案。
I believe Phil asked a very similar question, but I'm not experienced enough to grok the answers he received.
感谢您的帮助。
更新:
只是要清楚,我之所以suppling通过DI应用程序的MembershipProvider是支持多个租户。每个租户有的。 DI让我在运行时切换连接字符串,从而避免了核心应用无关约被用于为每个租户的数据库。温莎控制DI和它知道该租户通过URL发出请求:
Thanks for your help.
UPDATE:Just to be clear, my reason for suppling the application's MembershipProvider via DI is to support multiple tenants. Each tenant has an isolated database with ASP membership tables. DI allows me to switch connection strings at runtime and thus keep the core application agnostic about which database is being used for each tenant. Windsor is controlling the DI and it "knows" which tenant makes the request via url:
var url = HttpContext.Current.Request.ServerVariables["HTTP_HOST"]
迈克哈德洛我只是想SqlMembershipProvider的融入他使用这种IoC的设计。
Mike Hadlow talks about this technique here. I am just trying to integrate SqlMembershipProvider into his use of this IoC design.
再次感谢。
推荐答案
假设你有你的会员资格供应商配置是这样的:
Assuming you have your membership providers configured something like this:
<membership>
<providers>
<clear/>
<add name="www.tenant1.com"
type="System.Web.Security.SqlMembershipProvider, ..."
.../>
<add name="www.tenant2.com"
type="System.Web.Security.SqlMembershipProvider, ..."
.../>
</providers>
</membership>
您可以拥有温莎选择合适的供应商是这样的:
you can have Windsor select the appropriate provider like this:
var container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<MembershipProvider>()
.LifeStyle.Transient
.UsingFactoryMethod(() => Membership.Providers[HttpContext.Current.Request.Url.Host]));
... (your controller registrations, etc)
这篇关于注入的MembershipProvider到ASP.Net MVC的AccountController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!