一、为什么使用AutoFac?
之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌似更为普遍,于是捯饬了两天,发现这个东东确实是个高大上的IOC容器~
Autofac是.NET领域最为流行的IOC框架之一,传说是速度最快的一个:
优点:
它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件
较低的学习曲线,学习它非常的简单,只要你理解了IoC和DI的概念以及在何时需要使用它们
XML配置支持
自动装配
与Asp.Net MVC 3集成
微软的Orchad开源程序使用的就是Autofac,从该源码可以看出它的方便和强大
既然它都这么牛X了,我们用它就理所当然了,所以推荐其为IOC的终极解决方案!
二、AutoFac的使用
首先你必须获取AutoFac,这里你可以通过各种方式加载它,我这里还是通过VS中的NuGet来加载AutoFac,不论是哪种方式,最终的目的就是将 Autofac.dll,Autofac.Configuration.dll 这两个程序集引用到你的项目中。这样在你的项目中,如果想使用AutoFac,只需添加其命名空间引用即可~
1、AutoFac入门
我们先定义一个数据访问的接口:
- public interface IDAL
- {
- void Insert(string commandText);
- }
然后用Sql和Oracle两种方式分别实现上述接口,不过这里只是演示而已,所以并没有真正去实现这两个类,你懂的~
SQL方式:
- public class SqlDAL : IDAL
- {
- public void Insert(string commandText)
- {
- Console.WriteLine("使用sqlDAL添加相关信息");
- }
- }
Oracle方式:
- public class OracleDAL : IDAL
- {
- public void Insert(string commandText)
- {
- Console.WriteLine("使用OracleDAL添加相关信息");
- }
- }
然后注入实现构造函数注入:
- public class DBManager
- {
- IDAL _dal;
- public DBManager(IDAL dal)
- {
- _dal= dal;
- }
- public void Add(string commandText)
- {
- _dal.Insert(commandText);
- }
- }
最后要真正完成依赖注入就得AtuoFac登场了:
- var builder = new ContainerBuilder();
- builder.RegisterType<DBManager>();
- builder.RegisterType<SqlDAL>().As<IDAL>();
- using (var container = builder.Build())
- {
- var manager = container.Resolve<DBManager>();
- manager.Add("INSERT INTO Persons VALUES ('Man', '25', 'WangW', 'Shanghai')");
- }
从以上栗子可以看出,其实AutoFac的使用跟Unity的使用有点像,关键的东东就是这个Container容器类
2、AutoFac常用方法说明
(1)builder.RegisterType<Object>().As<Iobject>():注册类型及其实例。例如下面就是注册接口IDAL的实例SqlDAL
- ContainerBuilder builder = new ContainerBuilder();
- builder.RegisterType<SqlDAL>().As<IDAL>();
- IContainer container = builder.Build();
- SqlDAL sqlDAL = (SqlDAL)container.Resolve<IDAL>();
(2)IContainer.Resolve<IDAL>():解析某个接口的实例。例如上面的最后一行代码就是解析IDAL的实例SqlDAL
(3)builder.RegisterType<Object>().Named<Iobject>(string name):为一个接口注册不同的实例。有时候难免会碰到多个类映射同一个接口,比如SqlDAL和OracleDAL都实现了IDAL接口,为了准确获取想要的类型,就必须在注册时起名字。
- builder.RegisterType<SqlDAL>().Named<IDAL>("sql");
- builder.RegisterType<OracleDAL>().Named<IDAL>("oracle");
- IContainer container = builder.Build();
- SqlDAL sqlDAL = (SqlDAL)container.ResolveNamed<IDAL>("sql");
- OracleDAL oracleDAL = (OracleDAL)container.ResolveNamed<IDAL>("oracle");
(4)IContainer.ResolveNamed<IDAL>(string name):解析某个接口的“命名实例”。例如上面的最后一行代码就是解析IDAL的命名实例OracleDAL
(5)builder.RegisterType<Object>().Keyed<Iobject>(Enum enum):以枚举的方式为一个接口注册不同的实例。有时候我们会将某一个接口的不同实现用枚举来区分,而不是字符串,例如:
- public enum DBType{ Sql, Oracle}
- builder.RegisterType<SqlDAL>().Keyed<IDAL>(DBType.Sql);
- builder.RegisterType<OracleDAL>().Keyed<IDAL>(DBType.Oracle);
- IContainer container = builder.Build();
- SqlDAL sqlDAL = (SqlDAL)container.ResolveKeyed<IDAL>(DBType.Sql);
- OracleDAL oracleDAL = (OracleDAL)container.ResolveKeyed<IDAL>(DBType.Oracle);
(6)IContainer.ResolveKeyed<IDAL>(Enum enum):根据枚举值解析某个接口的特定实例。例如上面的最后一行代码就是解析IDAL的特定实例OracleDAL
(7)builder.RegisterType<Worker>().InstancePerDependency():用于控制对象的生命周期,每次加载实例时都是新建一个实例,默认就是这种方式
(8)builder.RegisterType<Worker>().SingleInstance():用于控制对象的生命周期,每次加载实例时都是返回同一个实例
(9)IContainer.Resolve<T>(NamedParameter namedParameter):在解析实例T时给其赋值
- DBManager manager = container.Resolve<DBManager>(new NamedParameter("name", "SQL"));
- public class DBManager
- {
- IDAL dal;
- public DBManager (string name,IDAL _dal)
- {
- Name = name;
- dal= _dal;
- }
- }
3、通过配置的方式使用AutoFac
(1)先配置好配置文件
- <?xml version="1.0"?>
- <configuration>
- <configSections>
- <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
- </configSections>
- <autofac defaultAssembly="ConsoleApplication1">
- <components>
- <component type="ConsoleApplication1.SqlDAL, ConsoleApplication1" service="ConsoleApplication1.IDAL" />
- </components>
- </autofac>
- </configuration>
(2)读取配置实现依赖注入(注意引入Autofac.Configuration.dll)
- static void Main(string[] args)
- {
- ContainerBuilder builder = new ContainerBuilder();
- builder.RegisterType<DBManager>();
- builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
- using (IContainer container = builder.Build())
- {
- DBManager manager = container.Resolve<DBManager>();
- manager.Add("INSERT INTO Persons VALUES ('Man', '25', 'WangW', 'Shanghai')");
- }
三、ASP.NET MVC与AtuoFac
终于到了ASP.NET MVC与AtuoFac双剑合璧的时候了,下面就看看AtuoFac在MVC中的应用,其实很简单,大概就几个步骤搞定:
1、首先在函数Application_Start() 注册自己的控制器类,一定要引入Autofac.Integration.Mvc.dll
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- using Autofac;
- using AtuoFacOfMVC4.Models;
- using System.Reflection;
- using Autofac.Integration.Mvc;
- namespace AtuoFacOfMVC4
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- var builder = new ContainerBuilder();
- SetupResolveRules(builder);
- builder.RegisterControllers(Assembly.GetExecutingAssembly());
- var container = builder.Build();
- DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
- AreaRegistration.RegisterAllAreas();
- WebApiConfig.Register(GlobalConfiguration.Configuration);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- AuthConfig.RegisterAuth();
- }
- private void SetupResolveRules(ContainerBuilder builder)
- {
- builder.RegisterType<StudentRepository>().As<IStudentRepository>();
- }
- }
- }
2、现在在你的MVC程序中注入依赖代码就ok了
(1)首先声明一个Student学生类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AtuoFacOfMVC4.Models
- {
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Graduation { get; set; }
- public string School { get; set; }
- public string Major { get; set; }
- }
- }
(2)然后声明仓储接口及其实现
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace AtuoFacOfMVC4.Models
- {
- public interface IStudentRepository
- {
- IEnumerable<Student> GetAll();
- Student Get(int id);
- Student Add(Student item);
- bool Update(Student item);
- bool Delete(int id);
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace AtuoFacOfMVC4.Models
- {
- public class StudentRepository : IStudentRepository
- {
- private List<Student> Articles = new List<Student>();
- public StudentRepository()
- {
- //添加演示数据
- Add(new Student { Id = 1, Name = "张三", Major = "软件工程", Graduation = "2013年", School = "西安工业大学" });
- Add(new Student { Id = 2, Name = "李四", Major = "计算机科学与技术", Graduation = "2013年", School = "西安工业大学" });
- Add(new Student { Id = 3, Name = "王五", Major = "自动化", Graduation = "2013年", School = "西安工业大学" });
- }
- /// <summary>
- /// 获取全部学生信息
- /// </summary>
- /// <returns></returns>
- public IEnumerable<Student> GetAll()
- {
- return Articles;
- }
- /// <summary>
- /// 通过ID获取学生信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public Student Get(int id)
- {
- return Articles.Find(p => p.Id == id);
- }
- /// <summary>
- /// 添加学生信息
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public Student Add(Student item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- Articles.Add(item);
- return item;
- }
- /// <summary>
- /// 更新学生信息
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- public bool Update(Student item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- int index = Articles.FindIndex(p => p.Id == item.Id);
- if (index == -1)
- {
- return false;
- }
- Articles.RemoveAt(index);
- Articles.Add(item);
- return true;
- }
- /// <summary>
- /// 删除学生信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool Delete(int id)
- {
- Articles.RemoveAll(p => p.Id == id);
- return true;
- }
- }
- }
(3)最后添加控制器StudentController,并注入依赖代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using AtuoFacOfMVC4.Models;
- namespace AtuoFacOfMVC4.Controllers
- {
- public class StudentController : Controller
- {
- readonly IStudentRepository repository;
- //构造器注入
- public StudentController(IStudentRepository repository)
- {
- this.repository = repository;
- }
- public ActionResult Index()
- {
- var data = repository.GetAll();
- return View(data);
- }
- }
- }
(4)最后为控制器StudentController的Index方法添加视图即可,这里不再详述,运行效果如下