Castle Windsor,如何以编程方式模仿参数xml语法

时间:2010-10-05 13:34:03

标签: castle-windsor ioc-container

鉴于Castle Windsor的这一遗留XML配置:

 <parameters>
    <AdditionalMessage>#{message}</AdditionalMessage>
    <Files>#{files}</Files>
    <downloaders>
      <array>
        <item>${HttpFileDownloader}</item>
        <item>${HttpsFileDownloader}</item>
        <item>${FtpFileDownloader}</item>
        <item>${FileSystemFileDownloader}</item>
      </array>
    </downloaders>
    <?if DEBUG?>
    <scraper>${BenchmarkingTitleScraperDecorator}</scraper>
    <?else?>
    <scraper>${RegexTitleScraper}</scraper>
    <?end?>   </parameters>

如何使用IWindsorInstaller执行此操作?到目前为止,我有这个,不知道我是否在正确的轨道上:

   container.Register(Component
                .For<HtmlTitleRetriever>()
                .Named("HtmlTitleRetriever")
                .DependsOn(Property.ForKey("AdditionalMessage").Eq("#{message}"))
                .DependsOn(Property.ForKey("Files").Eq("#{files}"))
                .DependsOn(Property.ForKey("Files").Eq("#{files}"))
                 .DependsOn(Property.ForKey("downloaders").Is<IFileDownloader>())
                );

1 个答案:

答案 0 :(得分:2)

    public class ContainerInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {

            //scrapers
            container.Register(Component.For<ITitleScraper>().ImplementedBy<StringParsingTitleScraper>().Named("StringParsingTitleScraper"));
            container.Register(Component.For<ITitleScraper>().ImplementedBy<RegexTitleScraper>().Named("RegexTitleScraper"));


            //list of downloaders
            container.Kernel.Resolver.AddSubResolver(new ListResolver(container.Kernel));
            container.Register(Component.For<IFileDownloader>().ImplementedBy<HttpFileDownloader>().Named("HttpFileDownloader"));
            container.Register(Component.For<IFileDownloader>().ImplementedBy<FtpFileDownloader>().Named("FtpFileDownloader"));
            container.Register(Component.For<IFileDownloader>().ImplementedBy<FileSystemFileDownloader>().Named("FileSystemFileDownloader"));

            //register concrete management class
            container.Register(Component
                .For<HtmlTitleRetriever>()
                .Named("HtmlTitleRetriever")
                .DependsOn(Property.ForKey("AdditionalMessage").Eq("message"))
#if (DEBUG)
                .DependsOn(Property.ForKey("scraper").Is("BenchmarkingTitleScraperDecorator"))
#else
                .DependsOn(Property.ForKey("scraper").Is("StringParsingTitleScraper"))
#endif
            );

            //now load the stuff that we want to configure from xml
            container.Install(Configuration.FromAppConfig());