| 一、前言 Autofac是.NET領(lǐng)域最為流行的IOC框架之一,微軟的Orchad開源程序使用的就是Autofac,Nopcommerce開源程序也是用的Autofac。 Orchad和Nopcommerce在用Autofac的時(shí)候進(jìn)行封裝,看過(guò)源碼的都知道Autafac使用簡(jiǎn)單,功能強(qiáng)大。 建議下載Orchad和Nopcommerce學(xué)習(xí)下源碼:附上下載地址 和其他IOC對(duì)比: Unity:微軟patterns&practicest團(tuán)隊(duì)開發(fā)的IOC依賴注入框架,支持AOP橫切關(guān)注點(diǎn)。 MEF(Managed Extensibility Framework):是一個(gè)用來(lái)擴(kuò)展.NET應(yīng)用程序的框架,可開發(fā)插件系統(tǒng)。 Spring.NET:依賴注入、面向方面編程(AOP)、數(shù)據(jù)訪問(wèn)抽象,、以及ASP.NET集成。 PostSharp:實(shí)現(xiàn)靜態(tài)AOP橫切關(guān)注點(diǎn),使用簡(jiǎn)單,功能強(qiáng)大,對(duì)目標(biāo)攔截的方法無(wú)需任何改動(dòng)。 Autofac:最流行的依賴注入和IOC框架,輕量且高性能,對(duì)項(xiàng)目代碼幾乎無(wú)任何侵入性。 下面介紹Autofac的使用 二、Autofac使用 新建一個(gè)mvc的項(xiàng)目,使用nuget安裝Autofac,需要安裝Autofac和Autofac ASP.NET MVC5 Intergration 
 安裝完成后引用里面就多了Autofac.dll和Autofac.Intergration.MVC,如果是在webApi里使用Autofac需要安裝Autofac ASP.NET Web API2.2 Intergration 才可以。 新建一個(gè)person實(shí)體類     public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
    }新建一個(gè)person倉(cāng)儲(chǔ)接口     public interface IPersonRepository
    {
        IEnumerable<Person> GetAll();
        Person Get(int id);
        Person Add(Person item);
        bool Update(Person item);
        bool Delete(int id);
    }新建實(shí)現(xiàn)  public class PersonRepository : IPersonRepository
    {
        List<Person> person = new List<Person>();
        public PersonRepository()
        {
            Add(new Person { Id = 1, Name = "joye.net1", Age = 18, Address = "中國(guó)上海" });
            Add(new Person { Id = 2, Name = "joye.net2", Age = 18, Address = "中國(guó)上海" });
            Add(new Person { Id = 3, Name = "joye.net3", Age = 18, Address = "中國(guó)上海" });
        }
        public IEnumerable<Person> GetAll()
        {
            return person;
        }
        public Person Get(int id)
        {
            return person.Find(p => p.Id == id);
        }
        public Person Add(Person item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            person.Add(item);
            return item;
        }
        public bool Update(Person item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            int index = person.FindIndex(p => p.Id == item.Id);
            if (index == -1)
            {
                return false;
            }
            person.RemoveAt(index);
            person.Add(item);
            return true;
        }
        public bool Delete(int id)
        {
            person.RemoveAll(p => p.Id == id);
            return true;
        }
    }Global屬性注入  public class MvcApplication : System.Web.HttpApplication
    {
        private void SetupResolveRules(ContainerBuilder builder)
        {
            builder.RegisterType<PersonRepository>().As<IPersonRepository>();
        }
        protected void Application_Start()
        {
            var builder = new ContainerBuilder();
            SetupResolveRules(builder);
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }最好獲取數(shù)據(jù)結(jié)果; 
 三、總結(jié) 文中只是給出了一個(gè)簡(jiǎn)單的注入實(shí)現(xiàn),剩下的可以自己去研究下,構(gòu)造函數(shù)注入,方法注入 泛型注入,所有程序集注入,都可以看下, 也可以把文章開頭的兩個(gè)開源的項(xiàng)目下載下來(lái)研究里面的Autofac注入方式。 | 
|  | 
來(lái)自: ThinkTank_引擎 > 《Autofac》