本文参考资料:
1、https://www.cnblogs.com/RayWang/p/11128554.html。
2、https://www.cnblogs.com/eedc/p/6127181.html
3、https://www.cnblogs.com/ancupofcoffee/p/5007649.html#top
前言
关于IoC和DI(依赖注入)的概念网上一搜一大把。简单来说,IoC即“控制反转”,是一种设计原则,一个抽象的概念。而依赖注入是实现IoC的一种设计。DI容器有很多,比如Unity、Autofac等。利用DI容器,可以将代码解耦,高分离度的代码将更有利于维护。利用上述参考资料,文本将依样画葫芦,实现一个简单的DI案例。
项目结构
项目 | 名称 | 类型 | 框架 |
Wangxc.AutoFac.Infrasturacute.Ioc | DI Framework 容器 | 库类 | .NET FrameWork 4.6 |
Wangxc.AutoFac.Model | 实体层 | 库类 | .NET FrameWork 4.6 |
Wangxc.AutoFac.Repository | 仓储层 | 库类 | .NET FrameWork 4.6 |
Wangxc.AutoFac.Service | 业务逻辑层 | 库类 | .NET FrameWork 4.6 |
Wangxc.AtuoFac.MvcApp | .NET Framework MVC主程序 | ASP.NET MVC 项目 | .NET FrameWork 4.6 |
名称 | 职责 | 举例 |
界面层 | 负责展示数据 | StudentController |
业务逻辑层 | 负责业务逻辑 | StudentService |
数据访问层 | 负责数据访问 | StudentRepository |
名称 | 职责 | 举例 |
界面层UI | 负责展示数据 | StudentController |
业务逻辑抽象层 (Ineterface BLL) | 业务逻辑运算抽象接口 | IStudentService |
业务逻辑层 (BLL) | 业务逻辑运算 | StudentServcie |
数据访问抽象层 (InterfaceDAL) | 数据访问抽象接口 | IStudentRepository |
数据访问层 (DAL) | 负责提供数据 | StudentRepository |
项目具体实现
实体层:
namespace Wangxc.AutoFac.Model
{public class StudentEntity{public int Id { get; set; }public string Name { get; set; }public string Grade { get; set; }}
}
仓储层:
namespace Wangxc.AutoFac.Repository.IRepository
{public interface IStudentRepository{string GetName(int id);}
}
using Wangxc.AutoFac.Repository.IRepository;namespace Wangxc.AutoFac.Repository.Repository
{public class StudentRepository : IStudentRepository{public string GetName(int id){switch (id){case 3: return "张三";case 4: return "李四";case 5: return "王五";default:return "赵六";}; }}
}
逻辑层:
namespace Wangxc.AutoFac.Service.IService
{public interface IStudentService{string GetName(int id);}
}
using Wangxc.AutoFac.Service.IService;
using Wangxc.AutoFac.Repository.IRepository;namespace Wangxc.AutoFac.Service.Service
{public class StudentService : IStudentService{private readonly IStudentRepository _studentRepository;public StudentService(IStudentRepository studentRepository){this._studentRepository = studentRepository;}public string GetName(int id){return this._studentRepository.GetName(id);}}
}
AutoFac IoC容器层:
通过NuGet程序包引入Autofac包:
通过NuGet程序包引入Autofac mvc5 程序包:
新建类文件MvcContainer.cs用于注册批量对象:
using System;
using System.Linq;
using Autofac;
using Autofac.Integration.Mvc;
using System.Reflection;namespace Wangxc.AutoFac.Infrasturcture.Ioc
{public static class MvcContainer{public static IContainer Instance;public static System.Web.Mvc.IDependencyResolver Init(Func<ContainerBuilder, ContainerBuilder> func = null){var builder = new ContainerBuilder();//新建容器用于注册组件MyBuild(builder);//注册组件func?.Invoke(builder);Instance = builder.Build();//利用构建器创建容器return new AutofacDependencyResolver(Instance);//返回针对MVC的解析器}public static void MyBuild(ContainerBuilder builder){//注册仓储层Assembly repositoryAssembly = Assembly.Load("Wangxc.AutoFac.Repository");builder.RegisterAssemblyTypes(repositoryAssembly).PublicOnly() //只要public访问权限的.Where(cc => cc.IsClass) //只要class类型的(排除值和interface类型).AsImplementedInterfaces(); //自动以其实现的接口暴露(包括Dispose接口)//注册逻辑层Assembly serviceAssembly = Assembly.Load("Wangxc.AutoFac.Service");builder.RegisterAssemblyTypes(serviceAssembly).PublicOnly().Where(cc => cc.IsClass).AsImplementedInterfaces();Assembly MvcAssembly = Assembly.Load("Wangxc.AutoFac.MvcApp");builder.RegisterControllers(MvcAssembly);//注册MVC项目中的Controller}}
}
MVC项目UI层:
通过NuGet程序包引入Autofac包:
在Global.asax.cs中添加如下代码:
新增StudentController.cs控制器用于测试Autofac是否搭建成功,如下图所示为构造函数注入:
using System.Web.Mvc;
using Wangxc.AutoFac.Service.IService;namespace Wangxc.AutoFac.MvcApp.Controllers
{public class StudentController : Controller{private readonly IStudentService _studentService;public StudentController(IStudentService studentService){this._studentService = studentService;}[HttpGet]public string GetNameById(int id){return this._studentService.GetName(id);}}
}
试运行
运行程序,并导航到Student/GetNameById,如下图所示: