2014-10-27 45 views
2

我目前正在重构我们的后端,并且在此过程中,我删除了ServiceLocator上的依赖关系。 但是!现在这已经给我搞清楚怎么做依赖注入,当需要注射的种类,从Web.config中 目前,具体的情况下被拉的头痛是这样的:在type =“”属性上使用依赖注入

<system.identityModel> 
    <identityConfiguration> 
     <claimsAuthenticationManager type="MyNamespace.MyClaimsAuthenticationManager, MyAssembly, Version=1.0.0.0, Culture=neutral" /> 
     <claimsAuthorizationManager type="MyNamespace.MyClaimsAuthorizationManager, MyAssembly, Version=1.0.0.0, Culture=neutral" /> 
     <securityTokenHandlers> 
     <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler,System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler,System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     <securityTokenHandlerConfiguration maximumClockSkew="5"></securityTokenHandlerConfiguration> 
     </securityTokenHandlers> 
    </identityConfiguration> 
</system.identityModel> 

这里的类型MyClaimsAuthenticationManager和MyClaimsAuthorizationManager是需要注入它们的依赖项的。

该项目在.NET 4.5下运行,目前使用Ninject作为依赖注入部分。

回答

4

.NET中的can't do DI with the Provider mechanisms,主要是因为它依赖于Constrained Construction反模式。

你最好的选择是找到一个不依赖.NET提供者机制的更好的设计。

如果这是不可能的,你的下一个最好的选择是对待供应商(在这种情况下MyClaimsAuthenticationManagerMyClaimsAuthorizationManager)作为Humble Objects各自含有Composition Root对构成实际的对象图,做的工作。换句话说,您可以将这些供应商视为Adapters,然后在需要启动的任何对象图上进行实际工作。

my book中,我将在第7.5节中详细介绍这种方法。

+0

感谢您的回答 - 我会马上购买您的书籍,但它不能作为Kindle电子图书提供:o必须等待蜗牛邮件发送:-) – 2014-10-27 09:43:43

+0

报废,找到电子书在万宁自己的网站上。谢谢,周一愉快 – 2014-10-27 09:51:54