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();
        }

posted @ 2019-04-19 16:16  健忘就忘吧  阅读(237)  评论(0编辑  收藏  举报