博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC中简单使用Autofac
阅读量:5836 次
发布时间:2019-06-18

本文共 4518 字,大约阅读时间需要 15 分钟。

  项目中引入Autofac的目的是为了实现控制反转,即IoC,Inversion of Control。控制反转可以有效的降低类之间的相互依赖关系,增加架构的弹性,降低软件复杂度。

  示例代码:

  IProvinceRepository.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Libing.Portal.Web.Models{    public interface IProvinceRepository    {        List
GetProvinceList(); }}
View Code

  ProvinceRepository.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Libing.Portal.Web.Models{    public class ProvinceRepository : IProvinceRepository    {        public List
GetProvinceList() { using (var ctx = new PortalContext()) { return ctx.Provinces.ToList(); } } }}
View Code

  ProvinceController.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Libing.Portal.Web.Controllers{    public class ProvinceController : Controller    {        private Libing.Portal.Web.Models.IProvinceRepository _provinceRepository;        public ProvinceController(Libing.Portal.Web.Models.IProvinceRepository provinceRepository)        {            _provinceRepository = provinceRepository;        }        public ActionResult Index()        {            return View(_provinceRepository.GetProvinceList());        }    }}
View Code

  ICityRepository.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Libing.Portal.Web.Models{    public interface ICityRepository    {        List
GetCityList(); }}
View Code

  CityRepository.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Libing.Portal.Web.Models{    public class CityRepository : ICityRepository    {        public List
GetCityList() { using (var ctx = new PortalContext()) { return ctx.Cities.ToList(); } } }}
View Code

  CityController.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Libing.Portal.Web.Controllers{    public class CityController : Controller    {        private Libing.Portal.Web.Models.IProvinceRepository _provinceRepository;        private Libing.Portal.Web.Models.ICityRepository _cityRepository;        public CityController(Libing.Portal.Web.Models.IProvinceRepository provinceRepository, Libing.Portal.Web.Models.ICityRepository cityRepository)        {            _provinceRepository = provinceRepository;            _cityRepository = cityRepository;        }        public ActionResult Index()        {            return View(_cityRepository.GetCityList());        }    }}
View Code

  Global.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Http;using System.Web.Mvc;using System.Web.Routing;using System.Reflection;using Autofac;using Autofac.Integration.Mvc;namespace Libing.Portal.Web{    // Note: For instructions on enabling IIS6 or IIS7 classic mode,     // visit http://go.microsoft.com/?LinkId=9394801    public class MvcApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            #region Autofac注入依赖            var builder = new ContainerBuilder();            SetupResolveRules(builder);            builder.RegisterControllers(Assembly.GetExecutingAssembly());            var container = builder.Build();            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));            #endregion            WebApiConfig.Register(GlobalConfiguration.Configuration);            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);        }        private static void SetupResolveRules(ContainerBuilder builder)        {            builder.RegisterType
().As
(); builder.RegisterType
().As
(); } }}
View Code

  在Global.cs中的SetupResolveRules可以使用下面的方式来注册:

private static void SetupResolveRules(ContainerBuilder builder){    builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())        .Where(t => t.Name.EndsWith("Repository"))        .AsImplementedInterfaces();}

  利用ContainerBuilder的RegisterAssemblyTypes()方法来注册组件中的所有类,通过Where()筛选出全部以“Repository”结尾的类,并自动找出这些类所实现的接口,然后设定与其对应。

转载地址:http://mhccx.baihongyu.com/

你可能感兴趣的文章
[ Office 365 开发系列 ] 开发模式分析
查看>>
3月13日 函数
查看>>
acm algorithm practice 2010 Dec. 26
查看>>
UVa11136 Hoax or what
查看>>
ceph理论及部署配置实践
查看>>
Spring事务管理的xml方式
查看>>
2. Add Two Numbers
查看>>
4-管理分支
查看>>
Java正则
查看>>
CryptoAPI的几个用处
查看>>
python - web自动化测试 - 元素操作 - 窗口切换
查看>>
shell编程系列1--shell脚本中的变量替换
查看>>
从零开始学习OpenGL ES之一 – 基本概念
查看>>
Android应用的缓冲界面启动界面
查看>>
【C#】线程之Parallel
查看>>
Linux 字符设备驱动模型
查看>>
nginx——location匹配流程图
查看>>
mysql利用navicat导出表结构和表中数据
查看>>
跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp...
查看>>
Cucumber 入门【转】
查看>>