虽然我通常认为 Autofac 文档(在 wiki 上)很有帮助,但关于 XML 配置和模块的部分对我来说有点不清楚。现在,我有一个示例工作(我在下面介绍),但我不确定它是否代表了 Autofac 上下文中的一种 SCSS 配置方法。特别是,我不确定配置文件和代码文件中是否有更多或更少的我真正需要的内容。
代码如下:
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 ( );
}
}
}
这是一个关联的配置文件:
<?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>
这一切都运行良好。应用程序将“Meow”输出到文本文件。如果我注释掉组件元素,应用程序会输出“Arf!”到控制台。
那么,这里一切都好吗?或者有更好的方法来解决这个问题吗?
我有点不确定基于模块的配置背后的想法:
我是否正确地认为,在实际实践中,模块应该与应用程序的其余部分位于单独的程序集中?
我是否正确理解模块的主要功能之一是为 DI 容器提供默认配置设置集?
理想情况下,我的配置文件应该有多广泛?换句话说,在使用 Autofac 时,我需要注意哪些配置文件反模式?
提前感谢(我认为)您的回复。
音乐学家
最佳答案
我个人的建议是谨慎使用 XML 配置。我只会将它用于您知道需要重新配置而无需重新编译的部分。例如,如果您正在构建一个可重用的库,这可能比您正在构建一个整体的 Web 应用程序要多。我尝试做的另一件事是使用以下代码使我的大多数类型可自动注册:
public static void AutoRegister(ContainerBuilder builder, params Assembly[] assemblies)
{
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.GetCustomAttributes(typeof (RegisterServiceAttribute), false).Any())
.AsSelf()
.AsImplementedInterfaces();
}
其中 RegisterServiceAttribute
是我的根项目中的一个属性:
[AttributeUsage(AttributeTargets.Class)]
[MeansImplicitUse]
public class RegisterServiceAttribute : Attribute
{
}
注意:MeansImplicitUse
来自 Resharper。
然后,我将 [RegisterService]
放在我想要自动注册的任何类上。我至少95%的注册都是这样处理的。剩余的注册发生在调用 AutoRegister()
之后。
关于autofac - 在 autofac 中使用模块和配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735992/