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

效果图

最新文章

  1. Linux 中优秀的文本化编辑思想大碰撞(Markdown、LaTeX、MathJax)
  2. jsrender for array 和for object语法
  3. Java NIO之选择器Selector
  4. java8 引进lamda
  5. Charles的使用
  6. Qt 与 JavaScript 通信
  7. AVAudioPlayer的锁屏播放控制和锁屏播放信息显示
  8. PHP入门小练习
  9. database锁实现
  10. vue-cli2 构建速度优化
  11. CentOS7使用firewalld打开关闭防火墙与端口[转]
  12. JavaScript 正则表达式 通俗解释 快速记忆
  13. Python 技术点
  14. Dance GAN 迁移不同视频中人物动作的方法
  15. linux使用mail命令发送邮件
  16. linux 常用awk命令
  17. Unity3d之Perfab
  18. String调用Array相关方法——有点古怪
  19. Address already in use: JVM_Bind,tomcat启动异常
  20. 转python调用Go代码

热门文章

  1. 图解Windows下安装WebLogic
  2. 镜像二叉树——剑指Offer
  3. webpack-入口篇
  4. VC,VB程序button、图标样式美化
  5. JPA測试实例
  6. HTTP要点概述:五,HTTP的无状态性,持久连接,Cookie
  7. GeHost powershell
  8. Ubuntu 16.04下安装MacBuntu 16.04 TP 变身Mac OS X主题风格
  9. openstack dnsmasq彭祖
  10. Runtime ----- 带你上道