作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将其转换为 CaSTLe Windsor 中的构造函数注入(inject)(它似乎与这个问题有关 Windsor Castle resolving 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));
...
}
}
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());
...
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);
最佳答案
我已经解决了,如果有人对这是否是一个好的/最佳解决方案有任何反馈,我会非常感兴趣
container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));
container.Register(
Component.For<IImageUpdater>()
.ImplementedBy<ApplicationTileFrontImageUpdater>()
.DependsOn(Property.ForKey<ApplicationUnits>().Eq(ApplicationUnits.Metric))
.Named("ApplicationTileFrontImageUpdaterMetric")
);
container.Register(
Component.For<IImageUpdater>()
.ImplementedBy<ApplicationTileFrontImageUpdater>()
.DependsOn(Property.ForKey<ApplicationUnits>().Eq(ApplicationUnits.Imperial))
.Named("ApplicationTileFrontImageUpdaterImperial")
);
....
container.Register(
Component.For<TestImageUpdaterClass>()
.ImplementedBy<TestImageUpdaterClass>()
);
var tiuc = container.Resolve<TestImageUpdaterClass>();
Assert.AreEqual(13, tiuc.ImageUpdaters.Count);
public class TestImageUpdaterClass
{
public IList<IImageUpdater> ImageUpdaters { get; set; }
public TestImageUpdaterClass(IList<IImageUpdater> imageUpdaters)
{
ImageUpdaters = imageUpdaters;
}
}
关于list - 如何将对象列表注入(inject)温莎城堡的构造函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420842/
我是一名优秀的程序员,十分优秀!