在開始autofac時,有必要先瞭解兩個關鍵詞:“控制反轉(IoC/Inverse Of Control)”與“依賴註入(DI/Dependence injection)”。 控制反轉(IoC):它把傳統上由程式代碼直接操控的對象的調用權交給容器,通過容器來實現對象組件的裝配和管理。 依賴註入(DI ...
在開始autofac時,有必要先瞭解兩個關鍵詞:“控制反轉(IoC/Inverse Of Control)”與“依賴註入(DI/Dependence injection)”。
控制反轉(IoC):它把傳統上由程式代碼直接操控的對象的調用權交給容器,通過容器來實現對象組件的裝配和管理。
依賴註入(DI):就是由IoC容器在運行期間,動態地將某種依賴關係註入到對象之中。
public class BMW { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶馬" + series); } } public class Porsche { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶時捷" + series); } }
上面定義了寶馬與保時捷兩個類,它們都有Create方法,參數車系,傳統的調用方式如下:
BMW bmw = new BMW(); bmw.Create("730Li"); Porsche porsche = new Porsche(); porsche.Create("卡宴");
這裡我們看到,需要使用new創建實例並調用方法
改進,使用工廠方式
public interface ICarCreator { void Create(string series); } public class BMW : ICarCreator { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶馬" + series); } } public class Porsche : ICarCreator { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶時捷" + series); } }
調用方式,這裡使用了反射的形式,在配置文件里配置(類似支持多個資料庫的操作工廠)
ICarCreator car = (ICarCreator)Assembly.GetExecutingAssembly().CreateInstance(ConfigurationManager.AppSettings["Brand"]); //使用了反射方式 car.Create("suv");
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Brand" value="AutofacDemo1.BMW"/> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
進一步改進,使用autofac,將創建實例交給autofac
var builder = new ContainerBuilder(); builder.RegisterType<ICarCreator>(); builder.RegisterType<BMW>().As<ICarCreator>(); //builder.RegisterType<Porsche>().As<ICarCreator>(); Container = builder.Build(); using (var scope = Container.BeginLifetimeScope()) { var writer = scope.Resolve<ICarCreator>(); writer.Create("轎車"); }
完整的代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Configuration; using Autofac; namespace AutofacDemo1 { class Program { private static IContainer Container { get; set; } static void Main(string[] args) { #region 普通的調用 //BMW bmw = new BMW(); //bmw.Create("730Li"); //Porsche porsche = new Porsche(); //porsche.Create("卡宴"); #endregion #region 工廠的方式 //ICarCreator car = (ICarCreator)Assembly.GetExecutingAssembly().CreateInstance(ConfigurationManager.AppSettings["Brand"]); //使用了反射方式 //car.Create("suv"); #endregion #region 使用Ioc[Autofac] var builder = new ContainerBuilder(); builder.RegisterType<ICarCreator>(); builder.RegisterType<BMW>().As<ICarCreator>(); //builder.RegisterType<Porsche>().As<ICarCreator>(); Container = builder.Build(); using (var scope = Container.BeginLifetimeScope()) { var writer = scope.Resolve<ICarCreator>(); writer.Create("轎車"); } #endregion Console.ReadKey(); } } public interface ICarCreator { void Create(string series); } public class BMW : ICarCreator { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶馬" + series); } } public class Porsche : ICarCreator { /// <summary> /// /// </summary> /// <param name="series">車系</param> public void Create(string series) { Console.WriteLine("生產了寶時捷" + series); } } }View Code