我有一个函数,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 容器的第一步,我想大致了解它是如何完成的。