在此 Autofac IoC article它们展示了一个使用参数将接口(interface)映射到实现的示例。您会在文章的中间找到它。
XML 中的 Unity 等效项是什么?无法使用流畅的语法来完成我正在做的事情。需要是外部配置文件。
更新:
这是我想知道如何在 Unity 中执行的具体代码 -
<component id="DataStoreProvider"
service="Company.Server.IDataStoreProvider,Company.Server.Interface"
type="Company.Server.DataStoreProvider,Company.Server.Core">
<parameters>
<connectionString>My Connection String</connectionString>
</parameters>
</component>
也许不是以这种方式传递连接字符串的最好的例子......但你明白了。我想知道如何在Unity中使用XML进行参数。
最佳答案
你可以这样做。引用这个MSDN文章
<configuration>
<configSections>
...
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
...
</configSections>
...
<unity>
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="ILoginService" type="Company.Shared.ILoginService,Company.Shared.Interface" />
<typeAlias alias="LoginService" type="Company.Server.LoginService,Company.Server.Core" />
<typeAlias alias="INavigationService" type="Company.Shared.INavigationService,Company.Shared.Interface" />
<typeAlias alias="NavigationService" type="Company.Server.NavigationService,Company.Server.Core" />
</typeAliases>
<containers>
<container name="Services">
<types>
<type type="ILoginService" mapTo="LoginService" />
<type type="INavigationService" mapTo="NavigationService" />
</types>
</container>
</containers>
</unity>
....
更新:如果您查看 MSDN 文章,其中有一个部分描述了我认为符合您要求的内容。
<type type="IMyService" mapTo="MyDataService" name="DataService">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration">
<constructor>
<param name="connectionString" parameterType="string">
<value value="AdventureWorks"/>
</param>
<param name="logger" parameterType="ILogger">
<dependency />
</param>
</constructor>
</typeConfig>
</type>
关于.NET IoC - .NET Unity 的等效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1246588/