AutoFac的介紹
Bryan Lin
2013/10/18
Agenda


AutoFac 是什麼?



Why AutoFac ?



How to use AutoFac ?



Q&A
AutoFac 是什麼?


一種可以讓你的專案使用 Inversion of Controll (IoC)的Framework



使用簡單的方式來實作 IoC,讓程式碼看起來更簡潔



有關dependency injection/inversion of control的說明,可至下列網站參考:


http://martinfowler.com/articles/injection.html
Why AutoFac ?


讓程式的測試較為容易進行



保留程式的彈性



在眾多 IoC Framework 中,AutoFac是文件較多,比較容易學習的
How to use AutoFac ?


已經有寫好的程式碼如下:
How to use AutoFac ?


安裝AutoFac的NuGet包
How to use AutoFac ?


使用方式:


要定義類別的 Interface

public class SomeType : IService
{
}
How to use AutoFac ?


建立 ContainerBuilder 來註冊 IoC 的類別

// Create your builder.
var builder = new ContainerBuilder();
// Usually you're only interested in exposing the type
// via its interface:
builder.RegisterType<SomeType>().As<IService>();
// However, if you want BOTH services (not as common)
// you can say so:
builder.RegisterType<SomeType>().AsSelf().As<IService>();
How to use AutoFac ?


以我們剛才的程式碼,我們可以這樣使用:
How to use AutoFac ?


接著實作WriteDate這個方法
Q&A

Auto fac的介紹 20131018