儘管我通常發現Autofac文檔(在wiki上)很有幫助,但關於XML配置和模塊的章節有點不清楚。現在,我有一個示例工作(我在下面介紹),但我不確定它是否代表了Autofac環境中的一種混合方法。特別是,我不確定在配置文件和代碼文件中是否真的需要我或多或少的東西。在autofac中使用模塊和配置文件
下面的代碼:
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時,我需要了解哪些配置文件反模式?
謝謝(我認爲)提前爲您的答覆。
musicologyman
只是一個提醒,可節省其他人的時間和精力 - 在AutoFac 4.0,這是*不再*有效。有關如何使用配置,請參閱版本4.0的文檔。 – codeputer 2016-09-07 23:03:29