Autofac是一个.net平台下发性能还不错的IoC框架,利用它可以实现依赖注入和控制反转,使自己的软件模块之间的耦合性大大降低,让软件扩展、维护更加容易。控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。下面我就用Autofac实现ASP.NET mvc5.0的IOC控制反转的方法。这里用到是vs2013,Autofac ASP.NET MVC 5 Integration和mvc 5.0。Autofac ASP.NET MVC 5 Integration是Autofac的MVC封装库,我们只需要很少的代码就可以实现依赖注入。下面请看详细实例。

1、下载安装相关代码

在你的web项目右键选择“管理nuget程序包”

选中左边的联机,然后在搜索的输入框输入“autofac”

选中Autofac ASP.NET MVC 5 Integration,点击安装,这样你的项目就下载Autofac ASP.NET MVC 5 Integration了这个相关的dll的引用,并自动给添加了相关引用。

2、案例展示

定义一个业务接口IProductRepository,用来获取所有产品 
public interface IProductRepository
{
IQueryable<Product> Products { get; }
}
定义一个业务具体类,实现IProductRepository接口
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Product> Products
{
get { return context.Products; }
}
EFDbContext定义的EF类,我这里是用EF框架来实现读取数据库记录,当然你可以用纯的ADO、或者其它框架如ibatis.Net、Nhibernate、dapper等。

2.1、定义一个Controller依赖IProductRepository接口

public class BuyController : Controller
{
private IProductRepository repository;
public BuyController(IProductRepository repo)
{
repository = repo;
} //此处省略其它代码
}

2.2、绑定IProductRepository接口依赖的具体实现的类

using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.linq;
using System.Web;
using System.Web.Mvc;
using WenMarine.BLL.Abstract;
using WenMarine.BLL.Concrete;
using WenMarine.Web.Infrastructure.Abstract;
using WenMarine.Web.Infrastructure.Concrete;
namespace WenMarine.Web.Infrastructure
{
public class IocConfig
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<EFProductRepository>().As<IProductRepository>(); //autofac 注册依赖
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}

2.3、调用IocConfig使依赖生效

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
IocConfig.RegisterDependencies();
}
 

3、总结

通过上面可以看到,BuyController(高层模块)中是引用一个IProductRepository抽象接口而不是具体的实现EFProductRepository(具体模块)类。这样做到依赖倒置,也就是控制反转。这里高层模块(BuyController)不依赖于底层模块(EFProductRepository里面的读取数据库操作的低层代码),二者都同依赖于抽象(IProductRepository);抽象不依赖于具体(EFProductRepository),具体依赖于抽象。
这里BuyController中的IProductRepository到底是哪个实现,是通过IocConfig类使用autofac的来注册绑定的。这样如果以后要更改数据库读取方式,考虑到性能问题,比如不用EF了框架了,要用Dapper,直接新建一个类DapperProductRepository继承IProductRepository接口,实现Products 属性。
最后把IocConfig中builder.RegisterType<EFProductRepository>().As<IProductRepository>(); 
改为
builder.RegisterType<DapperProductRepository >().As<IProductRepository>(); 

就可以了。这样BuyController高层的代码根本不用改,这样做到了对扩展开发,对修改关闭(OCP-面向对象的开放封闭原则),这个是面向对象程序设计的核心。很好的做到的松耦合,模块之间的耦合性大大降低,让软件扩展、维护更加容易。

下载

最新文章

  1. C# 中使用Word文档对图像进行操作
  2. Win7系统中提示:本地无法启动MySQL服务,报的错误:1067,进程意外终止的解决方法。
  3. 【24】若所有参数皆需类型转换,请为此采用non-members函数
  4. DiscreteSeekBar----&gt;SeekBar的使用
  5. Array.Add () and += in PowerShell
  6. ASP.Net TextBox控件只允许输入数字
  7. 简单探讨 javascript 闭包
  8. 定点分析: MySQL InnoDB是如何保证系统异常断电情况下的数据可靠性?
  9. VUE 参数共享问题
  10. Python函数学习
  11. P1006 传纸条-洛谷luogu-dp动态规划
  12. itcast-spring
  13. bzoj 2351 [BeiJing2011]Matrix——二维哈希
  14. C#知识点提炼期末复习专用
  15. JS性能优化 之 FOR循环
  16. STM32 CAN总线标识符过滤器难点解析
  17. 【技巧】如何清空SQLServer的日志文件
  18. Eclipse 中 program arguments 与 VM arguments 的区别
  19. tp5自动生成目录
  20. C#比较时分秒大小,终止分钟默认加十分钟,解决跨天、跨月、跨年的情况

热门文章

  1. SDK,monkey 浅谈
  2. RGBA 与opacity
  3. echo 换行不换行
  4. easyui combotree 默认 初始化时就选中
  5. poj 1080 (LCS变形)
  6. HTML之表单元素
  7. MongoDB 3.2 在windows上的安装
  8. android studio中xml没有提示
  9. 20145301&amp;20145321&amp;20145335实验一
  10. 【原创】-- uboot,kennel,fs,rootfs 编译制作