欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-07 23:48:25
原文地址:https://www.cnblogs.com/yinrq/p/5381492.html 一、前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程序也是用的Autofac。 Orchad和Nopcomm ......

一、前言

autofac是.net领域最为流行的ioc框架之一,微软的orchad开源程序使用的就是autofac,nopcommerce开源程序也是用的autofac。

orchad和nopcommerce在用autofac的时候进行封装,看过源码的都知道autafac使用简单,功能强大。

建议下载orchad和nopcommerce学习下源码:附上下载地址

和其他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 

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

安装完成后引用里面就多了autofac.dll和autofac.intergration.mvc,如果是在webapi里使用autofac需要安装autofac asp.net web api2.2 intergration 才可以。

新建一个person实体类

.NET领域最为流行的IOC框架之一Autofac
    public class person
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string address { get; set; }
    }
.NET领域最为流行的IOC框架之一Autofac

新建一个person仓储接口

.NET领域最为流行的IOC框架之一Autofac
    public interface ipersonrepository
    {
        ienumerable<person> getall();
        person get(int id);
        person add(person item);
        bool update(person item);
        bool delete(int id);
    }
.NET领域最为流行的IOC框架之一Autofac

新建实现

.NET领域最为流行的IOC框架之一Autofac
 public class personrepository : ipersonrepository
    {
        list<person> person = new list<person>();

        public personrepository()
        {
            add(new person { id = 1, name = "joye.net1", age = 18, address = "中国上海" });
            add(new person { id = 2, name = "joye.net2", age = 18, address = "中国上海" });
            add(new person { id = 3, name = "joye.net3", age = 18, address = "中国上海" });
        }
        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;
        }
    }
.NET领域最为流行的IOC框架之一Autofac

global属性注入

.NET领域最为流行的IOC框架之一Autofac
 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);
        }
    }
.NET领域最为流行的IOC框架之一Autofac

最好获取数据结果;

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

三、总结

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

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

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