如何将对象列表注入Castle Windsor的构造函数中

时间:2013-08-24 16:47:19

标签: list dependency-injection castle-windsor

如何将此转换为Castle Windsor中的Constructor Injection(它似乎与此问题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));
        ...
    }
}

这并不像传递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);

1 个答案:

答案 0 :(得分:3)

我已经解决了,如果有人对这是否是一个好的/最好的解决方案有任何反馈我会非常感兴趣

        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);

然后出于测试目的,使用一个使用IImageUpdaters列表的类

    public class TestImageUpdaterClass
    {
        public IList<IImageUpdater> ImageUpdaters { get; set; }

        public TestImageUpdaterClass(IList<IImageUpdater> imageUpdaters)
        {
            ImageUpdaters = imageUpdaters;
        }
    }