2

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

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

4

1 回答 1

5

你可以这样做。请参阅此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>
于 2009-08-10T16:12:42.267 回答