C#使用AutoFac实现IOC依赖注入

1,配置web.cofig配置文件修改执行javascript
<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
2,编写Autofc.cs并且此文件放在App_Start文件目录下,在应用初始化是进行加载。
<pre code_snippet_id="272713" snippet_file_name="blog_20140403_1_3301351" name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac;
using AutofcDemo.Controllers;
using AutofcDemo.Models;

namespace AutofcDemo.App_Start
{
    public static class Autofc
    {
        public static IContainer container; 
        public static void InitAutofc() 
        {
            ContainerBuilder builder = new ContainerBuilder();
          //----------------------------------------------------------------------------------------
            //                      构造方法进行自动注入使用方式(官方推荐)
            //----------------------------------------------------------------------------------------
            //方式一
             //builder.RegisterType<ShowDao>();
            // builder.RegisterType<HomeController>();
            //方式二
            //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t=>t.Name.EndsWith("Dao")).SingleInstance();
            //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Controller")).InstancePerLifetimeScope();
            //方式三
           // builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());

            //----------------------------------------------------------------------------------------
            //                      属性自动注入使用方式
            //----------------------------------------------------------------------------------------
            //方式一
             //builder.Register(c => new HomeController { showDao = c.Resolve<ShowDao>() });
            //方式二
             //builder.RegisterType<ShowDao>();
            // builder.RegisterType<HomeController>().PropertiesAutowired();
            //方式三
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Dao")).SingleInstance();
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Controller")).InstancePerLifetimeScope().PropertiesAutowired();
            //----------------------------------------------------------------------------------------
            container = builder.Build();
            DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
          
        }
    }
}
3,在global.cs中进行配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using AutofcDemo.App_Start;
using Autofac;
using AutofcDemo.Models;
using AutofcDemo.Controllers;

namespace AutofcDemo
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            Autofc.InitAutofc();
        }
    }
}
4,在控制器中注入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutofcDemo.Models;

namespace AutofcDemo.Controllers
{
    public class HomeController : Controller
    {
        //通过属性注入
        public ShowDao showDao { set; get; }

        //通过构造方法注入
        //public HomeController(ShowDao _showDao)
        //{
        //    this.showDao = _showDao;
        //}
        public ActionResult Index()
        {
            string aaa = showDao.PrintInfo();
            ViewBag.aaa = aaa;
            return View();
        }

    }
}





 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值