2009-06-29 3 views
1

コードを使用してUnity Containerにタイプを登録および解決する際には、「登録名」を使用して、インターフェースまたは基本クラス階層から派生した参照を明確にすることができます。統一構成セクションのUnity '登録名'に相当するものは何ですか?

「登録名」テキストをレジスタにパラメータとして提供され、解決されるだろう方法:

myContainer.RegisterType<IMyService, CustomerService>("Customers"); 

MyServiceBase result = myContainer.Resolve<MyServiceBase>("Customers"); 

は、しかし、私は私にはない設定ファイルの種類を登録するとき「登録名」を割り当てることができる場所を確認してください

私はインターフェイスを登録します:

<typeAlias alias="IEnlistmentNotification" type="System.Transactions.IEnlistmentNotification, System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 

は、その後、私はそのインターフェイスを実装知ることが起こる二つのタイプ:このコードに対応しているようだ

<type type="IEnlistmentNotification" mapTo="FlexAleManager"><lifetime type="singleton"/></type> 

<type type="IEnlistmentNotification" mapTo="PlaylistManager"><lifetime type="singleton"/></type> 

<typeAlias alias="PlaylistManager" type="Sample.Dailies.Grid.Workers.PlaylistManager, Sample.Dailies.Grid.Workers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 

<typeAlias alias="FlexAleManager" type="Sample.Dailies.Grid.Workers.FlexAleManager, Sample.Dailies.Grid.Workers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 

それから私はインターフェイスと二つのタイプの間のマッピングを提供します:

​​

明らかに私が必要とするのは、明確な設定エントリです。 tがこのコードに対応:

myContainer.RegisterType<IEnlistmentNotification, FlexAleManager>("Flex"); 
myContainer.RegisterType<IEnlistmentNotification, PlaylistManager>("Play"); 

私は自分のコードに入るときに、私はこれを行うことができます:

IEnlistmentNotification flex = myContainer.Resolve<IEnlistmentNotification>("Flex"); 
IEnlistmentNotification play = myContainer.Resolve<IEnlistmentNotification>("Play"); 

は、私が何を意味するかを参照してください?自分の質問に答えるために

おかげで、

キンボール

答えて

1

は、私はCodePlexに/ユニティ自宅でこのスクラップを見つけました:型要素が追加のフィールドを持っていることを

<type type="IFoo" mapTo="ServerFoo" name="server" /> 
<type type="IFoo" mapTo="ClientFoo" name="client"> 
    <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration"> 
     <constructor> 
     <param name="server" parameterType="IFoo"> 
      <dependency name="server" /> 
     </param> 
     </constructor> 
    </typeConfig></type> 

お知らせ:「名前私は気づいていなかった。これは、私が探していた「登録名」の値を提供します。

また、param要素には、同じ機能を実行するname属性を持つdependencyという名前のサブ要素もあります。

だから...コードプレックスに感謝!

キンボール

+0

;-)将来的に出てのもので、「行方不明」を避けるために設定するXSDを使用します。http://unitycontributions.codeplex.com/WorkItem/View.aspx?WorkItemId=2309 –

+0

してください、ミスター、IEnlistmentNotificationを使用して任意のサンプル(すべての完全なソースとテスト) "??ありがとう – Kiquenet

関連する問題