autofac初识
在开始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); } } }