小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

NET領(lǐng)域最為流行的IOC框架之一Autofac

 ThinkTank_引擎 2016-11-21

一、前言

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í)下源碼:附上下載地址

http://www./

http://www./

和其他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í)體類

復(fù)制代碼
復(fù)制代碼
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
    }
復(fù)制代碼
復(fù)制代碼

新建一個(gè)person倉(cāng)儲(chǔ)接口

復(fù)制代碼
復(fù)制代碼
    public interface IPersonRepository
    {
        IEnumerable<Person> GetAll();
        Person Get(int id);
        Person Add(Person item);
        bool Update(Person item);
        bool Delete(int id);
    }
復(fù)制代碼
復(fù)制代碼

新建實(shí)現(xiàn)

復(fù)制代碼
復(fù)制代碼
 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;
        }
    }
復(fù)制代碼
復(fù)制代碼

Global屬性注入

復(fù)制代碼
復(fù)制代碼
 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);
        }
    }
復(fù)制代碼
復(fù)制代碼

最好獲取數(shù)據(jù)結(jié)果;

三、總結(jié)

文中只是給出了一個(gè)簡(jiǎn)單的注入實(shí)現(xiàn),剩下的可以自己去研究下,構(gòu)造函數(shù)注入,方法注入

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

也可以把文章開頭的兩個(gè)開源的項(xiàng)目下載下來(lái)研究里面的Autofac注入方式。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多