2

如何将其转换为 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);
4

1 回答 1

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;
        }
    }
于 2013-08-24T21:16:28.970 回答