發表文章

目前顯示的是有「開發技術類-IoC-基本概念」標籤的文章

自救必看三大準則

IoC基本概念介紹

在實際開始進入Asp .net Mvc之前,我們需要先來看一下一個很重要的概念,那就是IoC。 可以說IoC是框架的核心,基本上只要具備一定規模的框架,通常都會使用IoC和DI的搭配,因此我們需要先瞭解它的概念才能夠實際開始我們的框架開發。 同步發表於我的部落格: http://alantsai2007.blogspot.tw/2014/09/BuildYourOwnApplicationFrameworkOnMvc-02-ioc-intro.html 什麼是IoC? IoC是Inversion Of Control(控制翻轉)的縮寫。 在介紹IoC之前,我們先來一段歷史回顧,看看彈性的程式是如何演進。 傳統New Class的方法 我們寫了一支方法,而這一支方法(Write(string text))的功能非常簡單,就是把傳入的參數輸出到console: class Program { static void Main ( string [] args) { ConsoleOutput output = new ConsoleOutput(); output.Write( "Hello World" ); } } class ConsoleOutput { public void Write ( string text) { Console.WriteLine(text); } } 開發出來了,一切都很美好,但是有一天有個使用者和你反應:「這個輸出能不能輸出到Text檔案裡面啊?」 身為一個厲害的工程師,這個當然沒有問題,馬上幫他寫出另外一個版本是輸出到Text檔案: class Program { static void Main ( string [] args) { // ConsoleOutput output = new ConsoleOutput(); TextOutput output = new TextOutput(); output.Write( "Hello Worl...