.NET领域最为流行的IOC框架之一Autofac(转)


 

一、前言

Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程序也是用的Autofac。

Orchad和Nopcommerce在用Autofac的时候进行封装,看过源码的都知道Autafac使用简单,功能强大。

建议下载Orchad和Nopcommerce学习下源码:附上下载地址

http://www.orchardproject.net/

http://www.nopcommerce.com/

和其他IOC对比:

Unity:微软patterns&practicest团队开发的IOC依赖注入框架,支持AOP横切关注点。

MEF(Managed Extensibility Framework):是一个用来扩展.NET应用程序的框架,可开发插件系统。

Spring.NET:依赖注入、面向方面编程(AOP)、数据访问抽象,、以及ASP.NET集成。

PostSharp:实现静态AOP横切关注点,使用简单,功能强大,对目标拦截的方法无需任何改动。

Autofac:最流行的依赖注入和IOC框架,轻量且高性能,对项目代码几乎无任何侵入性。

下面介绍Autofac的使用

二、Autofac使用

新建一个mvc的项目,使用nuget安装Autofac,需要安装Autofac和Autofac ASP.NET MVC5 Intergration 

安装完成后引用里面就多了Autofac.dll和Autofac.Intergration.MVC,如果是在webApi里使用Autofac需要安装Autofac ASP.NET Web API2.2 Intergration 才可以。

新建一个person实体类

  1.   public class Person
  2.   {
  3.   public int Id { get; set; }
  4.   public string Name { get; set; }
  5.   public int Age { get; set; }
  6.   public string Address { get; set; }
  7.   }

新建一个person仓储接口

  1.   public interface IPersonRepository
  2.   {
  3.   IEnumerable GetAll();
  4.   Person Get(int id);
  5.   Person Add(Person item);
  6.   bool Update(Person item);
  7.   bool Delete(int id);
  8.   }
新建实现
  1.   public class PersonRepository : IPersonRepository
  2.   {
  3.   List person = new List();
  4.    
  5.   public PersonRepository()
  6.   {
  7.   Add(new Person { Id = 1, Name = "joye.net1", Age = 18, Address = "中国上海" });
  8.   Add(new Person { Id = 2, Name = "joye.net2", Age = 18, Address = "中国上海" });
  9.   Add(new Person { Id = 3, Name = "joye.net3", Age = 18, Address = "中国上海" });
  10.   }
  11.   public IEnumerable GetAll()
  12.   {
  13.   return person;
  14.   }
  15.   public Person Get(int id)
  16.   {
  17.   return person.Find(p => p.Id == id);
  18.   }
  19.   public Person Add(Person item)
  20.   {
  21.   if (item == null)
  22.   {
  23.   throw new ArgumentNullException("item");
  24.   }
  25.   person.Add(item);
  26.   return item;
  27.   }
  28.   public bool Update(Person item)
  29.   {
  30.   if (item == null)
  31.   {
  32.   throw new ArgumentNullException("item");
  33.   }
  34.    
  35.   int index = person.FindIndex(p => p.Id == item.Id);
  36.   if (index == -1)
  37.   {
  38.   return false;
  39.   }
  40.   person.RemoveAt(index);
  41.   person.Add(item);
  42.   return true;
  43.   }
  44.   public bool Delete(int id)
  45.   {
  46.   person.RemoveAll(p => p.Id == id);
  47.   return true;
  48.   }
  49.   }

Global属性注入

  1.   public class MvcApplication : System.Web.HttpApplication
  2.   {
  3.   private void SetupResolveRules(ContainerBuilder builder)
  4.   {
  5.   builder.RegisterType().As();
  6.   }
  7.   protected void Application_Start()
  8.   {
  9.   var builder = new ContainerBuilder();
  10.   SetupResolveRules(builder);
  11.   builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  12.   var container = builder.Build();
  13.   DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  14.    
  15.   AreaRegistration.RegisterAllAreas();
  16.   FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  17.   RouteConfig.RegisterRoutes(RouteTable.Routes);
  18.   BundleConfig.RegisterBundles(BundleTable.Bundles);
  19.   }
  20.   }

最好获取数据结果;

三、总结

文中只是给出了一个简单的注入实现,剩下的可以自己去研究下,构造函数注入,方法注入

泛型注入,所有程序集注入,都可以看下,

也可以把文章开头的两个开源的项目下载下来研究里面的Autofac注入方式。

原文地址: