如何将其转换为 Castle Windsor 中的构造函数注入(这似乎与Windsor Castle resolve multiple dependencies with same interface这个问题有关,但我认为我的示例有点复杂?)。
public class ImageUpdaters : List<IImageUpdater>
{
public ImageUpdaters()
{
Add(new ApplicationTileBackImageUpdater(ApplicationUnits.Imperial));
Add(new ApplicationTileBackImageUpdater(ApplicationUnits.Metric));
Add(new ApplicationTileFrontImageUpdater(ApplicationUnits.Imperial));
Add(new ApplicationTileFrontImageUpdater(ApplicationUnits.Metric));
...
}
}
它不像只传递 IImageUpdater 的所有实现的列表那么简单,因为大多数都是参数化的。我有另一个稍微复杂一点的例子,其中一些对象需要共享同一个实例
public DataProcessors()
{
var snowReportRetriever = new CachingDataRetriever(new UriRetriever("http://.."));
var notificationSender = new NotificationSender(new MessageGenerator(new SQLDataReader()), new MessageSender());
var snowDepthProcessor = new DataProcessor<SnowDepthModel>(
"SnowDepthFact"
, snowReportRetriever
, new SnowDepthParser()
, new SnowDepthModelPersistor()
, new SnowDepthModelNotifier(notificationSender)
, new SQLDataReader());
var runsOfTheDayProcessor = new DataProcessor<ModelList<RunsOfTheDayModel>>
(
"RunsOfTheDayFact"
, snowReportRetriever
, new RunsOfTheDayParser()
, new RunsOfTheDayModelPersistor()
, new SQLDataReader());
...
编辑:我到了某个地方,我可以注册个人 ImageUpdaters 和 List 但列表是空的,所以我的断言失败了?
container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));
container.Register(
Component.For<IList<IImageUpdater>>()
.ImplementedBy<List<IImageUpdater>>()
);
container.Register(
Component.For<IImageUpdater>()
.ImplementedBy<ApplicationTileFrontImageUpdater>()
.DependsOn(Property.ForKey<ApplicationUnits>().Eq(ApplicationUnits.Metric))
.Named("ApplicationTileFrontImageUpdaterMetric")
);
...
var imageUpdaters = container.Resolve<IList<IImageUpdater>>();
Assert.AreEqual(13,imageUpdaters.Count);