.NET IoC - .NET Unity 的等效代码

标签 .net configuration inversion-of-control unity-container ioc-container

在此 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/

相关文章:

facebook - 如何为子域配置 Facebook 应用程序

java - 启动过滤器 struts2 时出现异常 无法加载配置。文档根元素 "package",必须与 DOCTYPE 根 "struts"匹配

c# - Autofac 运行时参数

dependency-injection - 统一 IoC : "Operation could destabilize the runtime"

.net - 启动编译为目标 'Any CPU' 的 .NET 应用程序时出错

.net - 如何将 linq 查询转换为人类可读的字符串

c# - 在下拉列表控件中以编程方式设置默认文本

c# - 为什么 StringComparison.InvariantCultureIgnoreCase 在 where 子句中抛出异常

hadoop - 由于hbase.cluster.distributed为false,因此未启动独特的区域服务器

c# - 在转换到新供应商期间,如何有效地将一个应用程序映射到两个数据库后端?