1、使用 NUGET 安装 Autofac

2、需要引用Autofac ASP.NET MVC 5 Integration  这个扩展包。

但有时候在NuGet中找不到 该包 需要使用“程序要控制器控制台”安装

输入  安装解决

 Install-Package Autofac.Mvc5 -Version 4.0.2

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

install-package autofac.webapi2

  

3、使用方法,以下方式引用  https://www.cnblogs.com/zjoch/p/6485170.html 博客

新建一个person实体类

public class Person{public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }public string Address { get; set; }}

新建一个person仓储接口

public interface IPersonRepository{IEnumerable<Person> GetAll();Person Get(int id);Person Add(Person item);bool Update(Person item);bool Delete(int id);}

新建一个person仓储

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;}}

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);}}

效果图

转载于:https://www.cnblogs.com/jerrywublogs/p/10621379.html

关于使用 autoFac 的注入方法相关推荐

  1. efcore mysql autofac_Asp.NetCore3.1版本的CodeFirst与经典的三层架构与AutoFac批量注入

    Core3.1 CodeFirst与AutoFac批量注入(最下面附GitHub完整 Demo,由于上传网速较慢,这里就直接压缩打包上传了) ===Core3.1 CodeFirst 数据库为远程阿里 ...

  2. Autofac 依赖注入框架

    Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上非常高. 官方网站http://autofac.org/ 优点:依 ...

  3. Autofac 批量注入

    如果有很多实现接口需要用到autofac进行注入,是不是需要调用ContainerBuilder.RegisterType()这个方法很多次?那有没有一个偷懒的方法呢?只需要三四行代码就可以实现所有接 ...

  4. python ioc di_Spring介绍,IOC(控制反转),DI(依赖注入)介绍及两种注入方法

    Spring介绍,IOC(控制反转),DI(依赖注入)介绍及两种注入方法 第一中方法:在xml文件中注入: (1)开源的轻量级的应用开发框架 特点:a.简化开发:b.解耦:c.集成: 原理对象与对象之 ...

  5. 【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )

    文章目录 一.动态注入方法 二.完整代码示例 一.动态注入方法 调用 Student 类不存在的方法 , 如果该类重写了 def methodMissing(String name, def args ...

  6. 【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )

    文章目录 一.Groovy 类内部和外部分别获取 metaClass 二.分析 Groovy 类内部和外部获取 metaClass 操作的字节码 三.使用 HandleMetaClass 注入方法 一 ...

  7. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )

    文章目录 一.使用 @Category 注解进行方法注入 二.分类注入方法查找优先级 三.完整代码示例 一.使用 @Category 注解进行方法注入 @Category 注解原型如下 : @Docu ...

  8. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类注入方法 )

    文章目录 一.方法注入 二.使用 Category 分类注入方法 三.完整代码示例 一.方法注入 在之前的博客中 , 主要是使用 Groovy 元编程 拦截方法 , 改变方法的实现 ; 使用元编程还可 ...

  9. 2020-11-23(dll注入方法)

    一般情况下有如下dll注入方法: 1.修改注册表来注入dll: 2.使用CreateRemoteThread函数对运行中的进程注入dll: 3.使用SetWindowsHookEx函数对应用程序挂钩( ...

  10. 原理+实战掌握SQL注入方法

    本文首发于先知社区 原理+实战掌握SQL注入方法 前言: SQL注入是web安全中最常见的攻击方式,SQL注入有很多方法,但如果只知道payload,不知道原理,感觉也很难掌握,这次就总结一下我所遇到 ...

最新文章

  1. 网站的 计算机主机作用是什么意思,网关是什么意思?网关的作用是什么?
  2. Storm构建分布式实时处理应用初探(转)
  3. vs widows服务的发布
  4. TX2 安装QT Creator
  5. 单应性矩阵和仿射变换_单应矩阵 基本矩阵 本质矩阵的区别与联系
  6. CodeForces - 1267A Apprentice Learning Trajectory(贪心)
  7. vue js 工具方法封装js 文件
  8. 仿58 php框架源码,转转最新源码
  9. Java反编译工具 luyten 0.5.3
  10. 同济大学计算机学硕无人录取,2019年双非上岸同济大学计算机考研初复试经验分享,超详细!...
  11. win8 性能测试软件,win8系统优化软件评测
  12. H264格式 I帧 P帧 B帧 基础知识
  13. 【R】ggplot2_堆积图
  14. Linux 10GE端口,CR19000关于10GE端口切换GE口
  15. EXCEPTION_ACCESS_VIOLATION异常
  16. 想开咖啡店要怎么入手?
  17. WordSequence API
  18. cortana小娜不能文字搜索,只能用语音搜索【修复教程】
  19. Neos - 漂亮的JABBER客户端
  20. Edge浏览器开启下载提速

热门文章

  1. Ubuntu系统多屏幕时 触摸屏如何分屏定位
  2. 机器学习之amp;amp;Andrew Ng课程复习--- 聚类——Clustering
  3. ASP.NET MVC+EF框架+EasyUI实现权限管理系列
  4. Android 编程下 ListView 和 CheckBox 混合使用时的常见问题
  5. 抓取xen-tools生成的信息
  6. 写一个自己的QQ签名
  7. 【老孙随笔】求职,不要无的放矢
  8. [备忘]java读取与写入文件的五种方式
  9. Spring Boot 定时任务,怎么实现任务动态增删启停?
  10. API 的5 大身份验证安全隐患