1.首先需要安装 Autofac 和Autofac.Integration.Mvc

install-package Autofac -version5.2.0

install-package Autofac.Integration.Mvc -version 5.0.0

2.编写依赖注入和解析器类,并在该类中增加静态方法,实现注入和依赖解析,通过

System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

来把当前的依赖注入加入到mvc框架中。

说明:

  • DependencyResolver 是System.Web.Mvc 命名空间中的类,其方法SetResolver()接收一个IDependencyResolver接口类型的参数;
  • AutofacDependencyResolver 是Autofac.Integration.Mvc命名空间中的类,该类实现了IDependencyResolver方法,在该类中只是把IDependencyResolver接口的两个方法GetService()和GetServices()作为虚拟方法,并未真正实现。该类是通过多个重载的构造函数来实现其生命周期的控制,并且至少接收一个ILifetimeScope类型注入容器参数来实现依赖项的解析。

3.为了使依赖解析器能够在系统启动时启动,需要在Global.asax.cs 中调用依赖解析器类中的静态方法进行初始化。

4. 控制器抽象,重建一个控制器基类,并在其中增加以来注入对象获取的方法,根据传入的接口类型自动匹配注入的依赖,从而得到该接口的实例。其他控制器通过继承该类而共享该类方法来获取依赖解析对象。

 

示例正代码:

1.仓储接口

using autofacdemo2.Models;
using System.Collections.Generic;
namespace autofacdemo2.Repository
{
   public interface IStudentRepository
   {
       List<Student> GetStudents();
   }
}

2.仓储实现

using autofacdemo2.Models;
using System.Collections.Generic;

namespace autofacdemo2.Repository
{
   public class StudentRepository : IStudentRepository
   {
       private List<Student> students;
       public StudentRepository()
       {
           students = new List<Student> {
               new Student{ Id=1,Name="张三",Sex="男",Grade="二(一)班" },
               new Student{ Id=2,Name="李四",Sex="女",Grade="二(二)班" },
               new Student{ Id=3,Name="王五",Sex="男",Grade="二(一)班" }

           };
       }
       public List<Student> GetStudents()
       {
          return students;
       }
   }
}

3.依赖解析类

using Autofac;
using Autofac.Integration.Mvc;
using autofacdemo2.Repository;
using System.Web.Mvc;
namespace autofacdemo2.App_Start
{
   public class DependencyInject
   {
       private static IContainer _container;
       public static IContainer Container {
           get { return _container; }
       }
       /// <summary>
       /// 注册相关接口
       /// </summary>
       public static void Initialise()
       {
           var builder = new ContainerBuilder();
           builder.RegisterType<StudentRepository>().As<IStudentRepository>();
           _container = builder.Build();
           DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
       }

}
}

4.控制器

using autofacdemo2.Repository;
using System.Linq;
using System.Web.Mvc;
namespace autofacdemo2.Controllers
{
   public class HomeController : Controller
   {
       protected internal IStudentRepository GetStudentRepository()
       {
           return DependencyResolver.Current.GetService<IStudentRepository>();
       }

       // GET: Home
       public ActionResult Index()
       {
           var students = GetStudentRepository();
           ViewBag.Count = students.GetStudents().Count();
           return View(students.GetStudents());
       }
   }
}

5.控制器基类

 

using System.Web.Mvc;

namespace autofacdemo2.Controllers
{
   public class BasicController : Controller
   {
       // GET: Basic
       protected internal TService GetService<TService>()
       {
           return DependencyResolver.Current.GetService<TService>();
       }
   }
}

6.控制器派生类

using autofacdemo2.Repository;
using System.Linq;
using System.Web.Mvc;
namespace autofacdemo2.Controllers
{
   public class AutofacTestController : BasicController
   {
       // GET: AutofacTest
       public ActionResult Index()
       {
           var studentrepository = GetService<IStudentRepository>();
           ViewBag.Count = studentrepository.GetStudents().Count();
           return View();
       }
   }
}

 

©著作权归作者所有:来自51CTO博客作者mb6066e165689bf的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. Linux I2C内核架构分析,基于三星I2C控制
  2. Nodejs 开发CLI必备基础依赖库
  3. 一个小时学会Maven
  4. thinkphp无法加载模块解决办法
  5. 5.依赖倒转原则
  6. Apache Kafka 不需要管理员:删除 Apache ZooKeeper 的依赖
  7. Spring Cloud 升级之路 - 2020.0.x - 1. 背景知识、需求描述与公
  8. 处理mvc的控制器访问与参数解析
  9. php facade与composer的使用

随机推荐

  1. android音乐播放器常见操作
  2. android 获取 imei号码以及其他信息
  3. Android(安卓)程序中哪个 Activity 最先
  4. Android使用代码
  5. Android三种方法设置ImageView的图片
  6. Android EventBus3.0 索引
  7. Android监听应用程序安装和卸载
  8. android theme中的各个color的含义
  9. android tp多点触摸
  10. Android下rtc驱动调用流程