asp.net mvc 使用Autofac控制反转

首先需要在项目使用Nuget安装Autofac和Autofac.Integration.Mvc,

注入有两种方式,一种是构造函数注入,另一种是属性注入,哪种方式比较好就仁者见仁,智者见智。

在程序的入口点Application_Start,

第一种:构造函数注入

/// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="builder"></param>
        private void SetupResolveRules(ContainerBuilder builder)
        {
            //告诉Autofac框架,将来要创建的控制器类存放在哪个程序集 (WebApplication1)
            Assembly assembly = Assembly.Load("WebApplication1");
            builder.RegisterControllers(assembly);

            //告诉autofac框架注册数据仓储层所在程序集中的所有类的对象实例
            Assembly assRepositories = Assembly.Load("DemoRepository");
            //创建respAss中的所有类的instance以此类的实现接口存储
            builder.RegisterTypes(assRepositories.GetTypes()).AsImplementedInterfaces();

            //告诉autofac框架注册业务逻辑层所在程序集中的所有类的对象实例
            Assembly assIRepository = Assembly.Load("DemoDomain");
            builder.RegisterTypes(assIRepository.GetTypes()).AsImplementedInterfaces();
        }

第二种:属性注入

/// <summary>
        /// 属性注入
        /// </summary>
        /// <param name="builder">autofac容器</param>
        private void SetupResolveRulesByProperty(ContainerBuilder builder)
        {
            //告诉Autofac框架,将来要创建的控制器类存放在哪个程序集 (WebApplication1)
            Assembly assembly = Assembly.Load("WebApplication1");
            builder.RegisterControllers(assembly).PropertiesAutowired(); 这样支持属性注入

            //告诉autofac框架注册数据仓储层所在程序集中的所有类的对象实例
            Assembly assRepositories = Assembly.Load("DemoRepository");
            //创建respAss中的所有类的instance以此类的实现接口存储
            builder.RegisterTypes(assRepositories.GetTypes()).AsImplementedInterfaces();

            //告诉autofac框架注册业务逻辑层所在程序集中的所有类的对象实例
            Assembly assIRepository = Assembly.Load("DemoDomain");
            builder.RegisterTypes(assIRepository.GetTypes()).AsImplementedInterfaces();
        }

执行:

protected void Application_Start()
        {
            //实例化一个autofac的创建容器
            ContainerBuilder builder = new ContainerBuilder();

            //构造函数注入
            //SetupResolveRules(builder);

            //属性注入
            SetupResolveRulesByProperty(builder);

            //创建一个Autofac的容器
            IContainer container = builder.Build();
            //将MVC的控制器对象实例 交由autofac来创建
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

 

在控制器那边调用:

public ITestRepository _iTest { get; set; }//属性注入这么用
        public HomeController()
        {
        }

        public ActionResult Index()
        {
            ViewBag.TestStr = _iTest.Create();
            return View();
        }

构造函数注入使用,如果要使用的接口比较多的话,就要写好多。

public readonly ITestRepository _iTest = null;
        public HomeController(ITestRepository iTest)
        {
            _iTest = iTest;
        }

        public ActionResult Index()
        {
            ViewBag.TestStr = _iTest.Create();
            return View();
        }

转载于:https://www.cnblogs.com/liaojianwang/p/10736626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值