MyFunc
を受け取る関数 がありIDoer
ます。さまざまな実装とさまざまな初期化を渡したい:
var types = new IDoer[]{typeof(Walker),typeof(Runner),typeof(Sweamer)};
var strings = new[]{"abc","xyz","zoo","cat","dog"};
foreach(var type in types) {
foreach(var str in strings) {
IDoer doer = container.ResolveWithParams(type, str, RandomizeInteger());
MyFunc(doer, str);
}
}
またはさらに良い:
var strings = new[]{"abc","xyz","zoo","cat","dog"};
foreach(var type in types) {
foreach(var str in strings) {
IDoer doer = container.ResolveWithParams<Walker>(type, str, RandomizeInteger());
MyFunc(doer, str);
doer = container.ResolveWithParams<Runner>(type, str, RandomizeInteger());
MyFunc(doer, str);
doer = container.ResolveWithParams<Sweamer>(type, str, RandomizeInteger());
MyFunc(doer, str);
}
}
たとえば、Walker
のコンストラクタは次のとおりです。
public Walker(/*lots of params...*/,
string importantString, /*other params...*/,
int importantInteger,/*even more params...*/) {/*...*/}
Runner
は次のとおりです。
public Runner(string importantString, /*some params...*/,
int importantInteger,/*additional different set of data...*/) {/*...*/}
およびSweamer
:
public Sweamer(string importantString, int importantInteger) {/*...*/}
私の質問は、container
コード (XML なし) を使用してこれを構成する方法ですか?
コンテナーの種類は気にしません。これは、IoC コンテナーを使用する最初のステップにすぎません。一般的に、それがどのように行われているかを知りたいと思っています。