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 に答える