码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC中简单使用Autofac

时间:2014-06-18 13:07:58      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

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

  示例代码:

  IProvinceRepository.cs

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Libing.Portal.Web.Models
{
    public interface IProvinceRepository
    {
        List<Libing.Portal.Web.Models.Province> GetProvinceList();
    }
}
View Code

  ProvinceRepository.cs

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

  ProvinceController.cs

bubuko.com,布布扣
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

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Libing.Portal.Web.Models
{
    public interface ICityRepository
    {
        List<Libing.Portal.Web.Models.City> GetCityList();
    }
}
View Code

  CityRepository.cs

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

  CityController.cs

bubuko.com,布布扣
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

bubuko.com,布布扣
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<Libing.Portal.Web.Models.ProvinceRepository>().As<Libing.Portal.Web.Models.IProvinceRepository>();
            builder.RegisterType<Libing.Portal.Web.Models.CityRepository>().As<Libing.Portal.Web.Models.ICityRepository>();
        }
    }
}
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”结尾的类,并自动找出这些类所实现的接口,然后设定与其对应。

ASP.NET MVC中简单使用Autofac,布布扣,bubuko.com

ASP.NET MVC中简单使用Autofac

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/libingql/p/3791938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!