【问题标题】:.NET IoC - Equivalent Code for .NET Unity.NET IoC - .NET Unity 的等效代码
【发布时间】:2010-11-17 19:26:47
【问题描述】:

在这个 Autofac IoC article 中,他们展示了一个将接口映射到带有参数的实现的示例。你会在文章的中间找到它。

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做参数。

【问题讨论】:

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


    【解决方案1】:

    你可以这样做。参考这篇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>
    

    【讨论】:

    • 查看更新。你能告诉我如何在你发布的 XML 中传递参数吗?
    • 谢谢,瓦苏。在第一次扫描时错过了。 +1 和胜利。
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 2015-07-11
    • 2021-08-14
    • 2017-06-17
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多