ウィンザー城:コードからコンストラクターのパラメーターを指定するにはどうすればよいですか?
-
01-07-2019 - |
質問
次のクラスがあるとします
MyComponent : IMyComponent {
public MyComponent(int start_at) {...}
}
次のように XML 経由でキャッスル ウィンザーにインスタンスを登録できます。
<component id="sample" service="NS.IMyComponent, WindsorSample" type="NS.MyComponent, WindsorSample">
<parameters>
<start_at>1</start_at >
</parameters>
</component>
まったく同じことをコード内で実行するにはどうすればよいでしょうか?(コンストラクターパラメーターに注意してください)
解決
編集:Fluent インターフェイスを使用して、以下のコードの回答を使用しました:)
namespace WindsorSample
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
public class MyComponent : IMyComponent
{
public MyComponent(int start_at)
{
this.Value = start_at;
}
public int Value { get; private set; }
}
public interface IMyComponent
{
int Value { get; }
}
[TestFixture]
public class ConcreteImplFixture
{
[Test]
void ResolvingConcreteImplShouldInitialiseValue()
{
IWindsorContainer container = new WindsorContainer();
container.Register(
Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.Parameters(Parameter.ForKey("start_at").Eq("1")));
Assert.That(container.Resolve<IMyComponent>().Value, Is.EqualTo(1));
}
}
}
他のヒント
これを試して
int start_at = 1;
container.Register(Component.For().DependsOn(dependency: Dependency.OnValue(start_at)));
Binsor を使用してコンテナーを構成することを検討しましたか?冗長で不格好な XML ではなく、Boo ベースの DSL を使用して Windsor を構成できます。構成は次のようになります。
component IMyComponent, MyComponent:
start_at = 1
利点は、柔軟な構成ファイルを使用しながら、XML に関する問題を回避できることです。また、コードでコンテナーを構成する場合のように、構成を変更するために再コンパイルする必要もありません。
ゼロ フリクション構成を可能にするヘルパー メソッドも多数あります。
for type in Assembly.Load("MyApp").GetTypes():
continue unless type.NameSpace == "MyApp.Services"
continue if type.IsInterface or type.IsAbstract or type.GetInterfaces().Length == 0
component type.GetInterfaces()[0], type
すぐに始めることができます ここ.
コンテナにインスタンスを要求するときは、IDictionary を渡す必要があります。
IWindsorContainer の次の Resolve オーバーロードを使用します。
T Resolve<T>(IDictionary arguments)
または非汎用的なもの:
object Resolve(Type service, IDictionary arguments)
たとえば、次のようになります。(コンテナーが IWindsorContainer であると仮定します)
IDictionary<string, object> values = new Dictionary<string, object>();
values["start_at"] = 1;
container.Resolve<IMyComponent>(values);
辞書内のキーの値は大文字と小文字が区別されることに注意してください。
構成クラスを使用して app.config を読み取ることができます。次に、それを登録し、ウィンザーにその依存関係に使用してもらいます。理想的には、MyConfiguration はインターフェイスを使用します。
public class MyConfiguration
{
public long CacheSize { get; }
public MyConfiguration()
{
CacheSize = ConfigurationManager.AppSettings["cachesize"].ToLong();
}
}
container.Register(Component.For<MyConfiguration>().ImplementedBy<MyConfiguration>());
container.Register(Component.For<MostRecentlyUsedSet<long>>()
.ImplementedBy<MostRecentlyUsedSet<long>>().
DependsOn(Dependency.OnValue("size", container.Resolve<MyConfiguration>().CacheSize))
.LifestyleSingleton());
IWindsorContainer インターフェイスの AddComponentWithProperties メソッドを使用して、拡張プロパティを持つサービスを登録できます。
以下は、NUnit 単体テストでこれを実行する「実際の」サンプルです。
namespace WindsorSample
{
public class MyComponent : IMyComponent
{
public MyComponent(int start_at)
{
this.Value = start_at;
}
public int Value { get; private set; }
}
public interface IMyComponent
{
int Value { get; }
}
[TestFixture]
public class ConcreteImplFixture
{
[Test]
void ResolvingConcreteImplShouldInitialiseValue()
{
IWindsorContainer container = new WindsorContainer();
IDictionary parameters = new Hashtable {{"start_at", 1}};
container.AddComponentWithProperties("concrete", typeof(IMyComponent), typeof(MyComponent), parameters);
IMyComponent resolvedComp = container.Resolve<IMyComponent>();
Assert.That(resolvedComp.Value, Is.EqualTo(1));
}
}
}