c# MVC5(二) MVC与IOC结合
今天主要来讲解使用Unity来自动注入。Unity前面我们已经详细的介绍过了,如有需要请自行前往去看,今天我们的重点是说MVC与IOC的结合。
IOC:控制反转,控制反转的工具是DI(依赖注入:构造函数注入--属性注入--方法注入(按时间顺序)),DI使用的工具是Unity容器。
一:新增mvc项目,然后nuget添加引用
1 <package id="Unity" version="5.10.3" targetFramework="net45" /> 2 <package id="Unity.Abstractions" version="4.1.3" targetFramework="net45" /> 3 <package id="Unity.Configuration" version="5.10.0" targetFramework="net45" /> 4 <package id="Unity.Container" version="5.10.3" targetFramework="net45" /> 5 <package id="Unity.Interception" version="5.10.1" targetFramework="net45" /> 6 <package id="Unity.Interception.Configuration" version="5.10.0" targetFramework="net45" />
二:新增文件夹CfgFiles新增一个unity.config文件,
具体代码和注释如下:

<configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/> </configSections> <unity> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/> <containers> <container name="ruanmouContainer"> <extension type="Interception"/> <!--<register type="命名空间.接口类型2,程序集" mapTo="命名空间.实现类型2,程序集" />--> <register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Ruanmou.EF.Model.JDDbContext, Ruanmou.EF.Model"/> <register type="Ruanmou.Bussiness.Interface.IUserCompanyService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.UserCompanyService, Ruanmou.Bussiness.Service"> <interceptor type="InterfaceInterceptor"/> </register> <register type="Ruanmou.Bussiness.Interface.IUserService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.UserService, Ruanmou.Bussiness.Service"> <interceptor type="InterfaceInterceptor"/> <interceptionBehavior type="Ruanmou.Project.AOP.LogBeforeBehavior, Ruanmou.MVC5"/> <interceptionBehavior type="Ruanmou.Project.AOP.LogAfterBehavior, Ruanmou.MVC5"/> </register> <register type="Ruanmou.Bussiness.Interface.ICompanyService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CompanyService, Ruanmou.Bussiness.Service"> <interceptor type="InterfaceInterceptor"/> </register> </container> </containers> </unity> </configuration>
需要注意的是:
1:一定在unity.config的属性中设置:复制到输出目录一定要设置:始终复制,这样做的目的是每次重新生成,会把这个untiy.config文件copy到bin目录下面。保证每次都是使用的是最新的。
2:下面这两个类是AOP拦截器需要的,如果需要的话则新增这些实体类,不需要的则直接删除掉。
<interceptionBehavior type="Ruanmou.Project.AOP.LogBeforeBehavior, Ruanmou.MVC5"/>
<interceptionBehavior type="Ruanmou.Project.AOP.LogAfterBehavior, Ruanmou.MVC5"/>
三:新增读取unity.config配置文件类DIFactory,
代码如下:
public class DIFactory { private static IUnityContainer container = null; static DIFactory() { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config"); Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); container = new UnityContainer(); section.Configure(container, "ruanmouContainer"); } public static IUnityContainer GetContainer() { return container; } }
四:新增defaultController类的子类DIControllerFactory来进行重新,
具体代码如下:
public class DIControllerFactory : DefaultControllerFactory { private Logger logger = new Logger(typeof(DIControllerFactory)); protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { this.logger.Warn($"{controllerType.Name}被构造..."); IUnityContainer container = DIFactory.GetContainer(); //return base.GetControllerInstance(requestContext, controllerType); return (IController)container.Resolve(controllerType); } }
五:在Global.asax中指定控制器为当前新增的控制器DIControllerFactory,
代码如下:
public class MvcApplication : System.Web.HttpApplication { private Logger logger = new Logger(typeof(MvcApplication)); protected void Application_Start() { AreaRegistration.RegisterAllAreas();//注册区域 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);//注册全局的Filter RouteConfig.RegisterRoutes(RouteTable.Routes);//注册路由 BundleConfig.RegisterBundles(BundleTable.Bundles);//合并压缩 ,打包工具 Combres ControllerBuilder.Current.SetControllerFactory(new DIControllerFactory()); this.logger.Info("网站启动了。。。"); } }
六:上面的准备工作已经做好了,下面来测试使用:
public class ThirdController : Controller { private IUserService _iUserService = null; private ICompanyService _iCompanyService = null; private IUserCompanyService _iUserCompanyService = null; /// <summary> /// 构造函数注入---控制器得是由容器来实例化 /// </summary> /// <param name="userService"></param> /// <param name="companyService"></param> /// <param name="userCompanyService"></param> public ThirdController(IUserService userService, ICompanyService companyService, IUserCompanyService userCompanyService) { this._iUserService = userService; this._iCompanyService = companyService; this._iUserCompanyService = userCompanyService; } // GET: Third public ActionResult Index() { IUserService service = this._iUserService; User user = service.Find<User>(2); return View(); } }
通过以上6个步骤则完成了IOC与MVC的完美结合,以后不用每次在new 一个业务类了。可以在初始化控制器的时候进行初始化那些业务类。
付费内容,请联系本人QQ:1002453261
本文来自博客园,作者:明志德道,转载请注明原文链接:https://www.cnblogs.com/for-easy-fast/articles/12489831.html
分类:
02 ASP.Net MVC
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析