DLL 内で、インターフェイス ("IInterface") と基本クラス ("BaseClass") から継承する 2 つのクラス ("Class1" と "Class2") を定義しました。
Castle Windsor の Fluent Registration API ( http://using.castleproject.org/display/IoC/Fluent+Registration+API ) を使用して、「BaseClass」(その DLL 内) から継承するすべてのクラスをそれぞれのクラスに自動的に登録します。インターフェイス。
特定のパーソナライゼーションのために、(今日から) 「castle.xml」ファイルを使用しました。このファイルは、インターフェースと具象クラス (Fluent Registration Api によって登録される) の間の関連付けを (「component」タグで) オーバーライドします。その xml ファイルを WindsorContainer のコンストラクター内にロードします。
コードは次のようなものです。
//container's initialization:
var resource = new FileResource("Castle.xml");
var interpreter = new XmlInterpreter(resource);
var container = new WindsorContainer(interpreter);
container.AddFacility<TypedFactoryFacility>();
//...
//automatic type registration:
container.Register(
AllTypes
.FromAssemblyContaining<BaseClass>()
.BasedOn<BaseClass>()
.WithService.Select(
(t1, t2) => t1.GetInterfaces()
.Except(new[] {typeof (IDisposable)})
.Union(new[] {t1}))
.Configure(a => a.Named(a.ServiceType.Name)
.LifeStyle.Transient)
.AllowMultipleMatches()
);
デフォルトでは、Castle に IInterface オブジェクトを要求すると、「Class1」が取得されます。「Class2」を取得するには、「Castle.xml」ファイル内で指定する必要があります。
今日、流暢な構成内の「コンポーネント」ディレクティブを指定して、castle.xml を削除しようとしました (「AllTypes」ディレクティブの前):
container.Register(
Component
.For<IInterface>()
.ImplementedBy<Class2>()
.LifeStyle.Transient);
...しかし、「AllTypes」流暢なディレクティブが「Component」ディレクティブをオーバーライドしたかのように、まだ Class1 オブジェクトを取得します (奇妙なことに、xml ファイル内の「component」ディレクティブが機能します)。
私は何を間違っていますか?
編集:私はキー名でコンポーネントにアクセスしていました.「.Named()」は問題を解決しました(Krzysztofに感謝):
container.Register(
Component
.For<IInterface>()
.ImplementedBy<Class2>()
.Named(typeof(IInterface).Name)
.LifeStyle.Transient);