在autofac中使用模块和配置文件 [英] using modules and config files in autofac

查看:201
本文介绍了在autofac中使用模块和配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我通常在Wiki上发现Autofac文档是有帮助的,但我对XML配置和模块的部分还是不太清楚.现在,我有一个示例工作(在下面介绍),但是我不确定在Autofac的上下文中它是否代表一种卑鄙的配置方法.特别是,我不确定在配置文件和代码文件中是否真正满足了我的需求.

While I'm generally finding the Autofac documentation (on the wiki) to be helpful, the sections on XML configuration and modules is a bit unclear to me. Now, I have a sample working (which I present below), but I'm unsure whether it represents a sort of bastardized approach to configuration within the context of Autofac. In particular, I'm not sure if I have more or less of what I really need in the config files and the code files.

代码如下:

using System;
using System.IO;
using Autofac;
using Autofac.Configuration;

namespace AutofacTest.Animals
{
    interface IAnimal
    {
        void Speak ( );
    }

    abstract class Animal : IAnimal
    {
        protected TextWriter Writer
        {
            get;
            private set;
        }

        protected Animal ( TextWriter writer )
        {
            this.Writer = writer;
        }

        public abstract void Speak ( );

    }

    class Dog : Animal
    {

        public Dog ( TextWriter writer )
            : base ( writer )
        {

        }

        public override void Speak ( )
        {
            this.Writer.WriteLine ( "Arf!" );
        }
    }

    class Cat : Animal
    {
        public Cat ( TextWriter writer )
            : base ( writer )
        {

        }

        public override void Speak ( )
        {
            this.Writer.WriteLine ( "Meow" );
        }
    }

    // In actual practice, this would be in a separate assembly, right?
    class AnimalModule : Module
    {
        protected override void Load ( ContainerBuilder builder )
        {
            builder.RegisterInstance ( Console.Out ).As<TextWriter> ( ).SingleInstance ( );
            builder.Register ( d => new Dog ( d.Resolve<TextWriter> ( ) ) ).As<IAnimal> ( );
        }
    }

    class Program
    {
        static void Main ( )
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            ContainerBuilder builder = new ContainerBuilder ( );
            ConfigurationSettingsReader reader = new ConfigurationSettingsReader(); 
            builder.RegisterModule ( reader );
            //builder.RegisterModule ( new AnimalModule ( ) );
            builder.Build ( ).Resolve<IAnimal> ( ).Speak ( );
            Console.ReadKey ( );
        }
    }
}

这是一个关联的配置文件:

And here's an associated config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
    </configSections>
    <autofac defaultAssembly="AutofacTest">
        <components>
            <component
                type="AutofacTest.Animals.Cat"
                service="AutofacTest.Animals.IAnimal" />
            <component type="System.IO.StreamWriter" service="System.IO.TextWriter">
                <parameters>
                    <parameter name="path" value="C:\AutofacTest.txt"/>
                    <parameter name="append" value="false" />
                </parameters>
                <properties>
                    <property name="AutoFlush" value="true" />
                </properties>
            </component>
        </components>
        <modules>
            <module type="AutofacTest.Animals.AnimalModule, AutofacTest"/>
        </modules>
    </autofac>
</configuration>

一切正常.应用程序将喵"输出到文本文件.如果我注释掉了组成元素,则应用程序将输出"Arf!".到控制台.

This all works fine. The application outputs "Meow" to a text file. If I comment out the component elements, the application outputs "Arf!" to the console.

那么,这里一切都还好吗?还是有更好的方法来解决这个问题?

So, is everything all right here? Or is there a better way of going about this?

我不确定基于模块的配置背后的想法:

And I'm a little unsure about the thinking behind module-based configuration:

我是否纠正说,实际上,模块应该与应用程序的其余部分放在单独的程序集中吗?

Am I correct that, in actual practice, modules should be in separate assemblies from the rest of the app?

我是否正确理解模块的主要功能之一是为DI容器提供一组默认配置设置?

Do I understand correctly that one of the chief functions of modules are to provide sets of default configuration settings for DI containers?

理想情况下,我的配置文件应该有多大?换句话说,当使用Autofac时,需要在监视中使用哪些配置文件反模式?

Ideally, how extensive should my config files be? In other words, when using Autofac, what are some config file anti-patterns for which I need to be on the lookout?

(我想)先感谢您的答复.

Thanks (I think) in advance for your responses.

音乐人

推荐答案

我无法从您的示例中100%确定预期的行为-如果您多次注册同一组组件,似乎那是您的意图,请忽略这些建议.

I'm not 100% sure from your example what the expected behaviour is - it seems like you're registering the same set of components many times over, if that's your intention please ignore these suggestions.

  • 如果您使用XML注册模块,则也不需要在模块中注册组件.
  • 同样,如果您使用XML注册模块,则无需同时在代码中注册模块.

关于最佳实践",我想说Jim推荐的少量使用XML的建议是一个很好的建议.就个人而言,我倾向于在模块内部进行所有繁重的工作,然后通过XML注册模块以利用可以在那里应用的配置.

Regarding 'best practices' I'd say Jim's recommendation to use XML sparingly is a good one. Personally, I tend to do all of the heavy lifting inside modules, and then register the modules via XML to take advantage of the configuration that can be applied there.

我要提出的另一项建议是仅使用XML配置模块.在您的示例中,您正在组件上设置配置.如果将参数应用到模块,然后在模块内部将参数根据需要传递给组件,则配置的脆弱性将降低.模块不会流失太多,而组件则需要能够在不破坏配置的情况下进行更改.

Another recommendation I'd make is to use XML to configure modules only. In your example you're setting configuration on a component; configuration is less fragile if you apply the parameters to a module, and then internal to that module pass them on to components as needed. Modules don't tend to churn very much, while components need to be able to be changed without breaking configuration.

HTH,

尼克

这篇关于在autofac中使用模块和配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆