当前位置: 移动技术网 > IT编程>开发语言>.net > IOC容器-Autofac在MVC中实现json方式注入使用

IOC容器-Autofac在MVC中实现json方式注入使用

2019年07月31日  | 移动技术网IT编程  | 我要评论

热血英豪双子星,见习期工作小结,印度超人国语版

在你阅读时,默认已经了解ioc和autofac的基本用法,

我在最近的我的博客项目中运用了ioc autofac 实现了依赖注入

由于我的项目时asp.net mvc所以我目前向大家展示mvc中如何使用autofac

首先在app_start中新建一个类包含以下方法

using system.reflection;
using autofac;
using autofac.integration.mvc;
using system.web.mvc;
using microsoft.extensions.configuration;
using autofac.configuration;
using microsoft.extensions.configuration.json;

namespace singleblog.ui.app_start
{
    public class autofacconfig
    {       
        public static void register()
        {
            //创建容器
            var builder = new containerbuilder();

            iconfigurationbuilder config = new configurationbuilder();
            //使用json注册文件
            iconfigurationsource autofacjsonconfigsource = new jsonconfigurationsource()
            {
                path = "autofac.json",
                optional = false,//boolean,默认就是false,可不写
                reloadonchange = false,//同上
            };
            //配置添加到autofac的configuration内中
            config.add(autofacjsonconfigsource);

            //将json的文件配置注册到autofac的容器内
            var module = new configurationmodule(config.build());
            builder.registermodule(module);

            //使用autofac提供的registercontrollers扩展方法来对程序集中所有的controller一次性的完成注册
            builder.registercontrollers(assembly.getexecutingassembly());

            //创建一个autofac的容器
            var container = builder.build();

            //下面就是使用mvc的扩展 更改了mvc中的注入方式.
            dependencyresolver.setresolver(new autofacdependencyresolver(container));
        }        
    }
}

这段代码的意思,就是从json文件中读取相应的注入内容!

但是注意!程序集中该引用的必须要引用,我贴出了引用代码为了方便大家去寻找

同时,创建一个根据autofac官网提示ioc的一个json文件,起名为autofac.json

内容如下

{
  "components": [
  {
    "type": "singleblog.dal.实现逻辑,singleblog.dal",
    "services": [{"type": "singleblog.idal.抽象的逻辑接口,singleblog.idal"}],
    "autoactivate": true,
    "injectproperties": true
  }
  后面可以接着写你的注入内容,注意每个实现的方法只能有一个对应的compoents! }

因为我是拿我的项目来介绍,这是根据dal与idal的依赖注入,同理bll和ibll都可以,你可以接着写。

这两个文件都是在mvc框架中即ui中,与ibll和idal分离为了方便我放出我的项目图

写好了之后,就是如何使用了

 但是在使用之前还必须得在你的项目启动类global.asax中引用autofacconfig类,比如我的引用

protected void application_start()
        {
            arearegistration.registerallareas();
            filterconfig.registerglobalfilters(globalfilters.filters);
            routeconfig.registerroutes(routetable.routes);
            autofacconfig.register();
        }

之后在项目的控制器中即可以使用bll和dal的方法,做到了依赖抽象而不用依赖细节

private iarticleservice _articleservice;
        private iqquserinfoservice _qquserinfoservice;
        public indexcontroller(iarticleservice articleservice,iqquserinfoservice qquserinfoservice)
        {
            this._articleservice = articleservice;
            this._qquserinfoservice = qquserinfoservice;
        }

这是一个index控制器,我申明私有的ibll逻辑,在构造函数中注入进去后使用,即可以访问到bll的实现逻辑

但是注意在bll如果要这样做的话,必须申明public的字段,不然不能使用哦!

使用这方法可以简单高效的实现ioc的依赖注入,使得你的项目更加的松耦合!

autofac的使用介绍就到这里,如果还有不明白的,请探寻官网的原理和阅读该地址

https://www.cnblogs.com/wolegequ/archive/2012/06/09/2543452.html 该地址详细介绍了关于autofac

本次实例很简单,希望某些大神可以给点关注,给点评价,菜鸟一枚,还望各位海涵,如有代码错误或者其他漏点,请与我联系,我将会及时改正,希望与博客园的各位大佬共同进步!每天学习一点点小知识!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网