• Autofac在mvc 3中的使用


    Autofac在mvc 3中的使用

    1、直接集成

    引入Autofac.dll,Autofac.Configuration.dll

            private void SetupResolveRules(ContainerBuilder builder)
            {
                //Components are wired to services using the As() methods on ContainerBuilder
                builder.RegisterType<SysUserMgrImp>().As<ISysUserMgr>();
                builder.RegisterType<FormsAuthServiceImp>().As<IFormsAuthService>();
            }
    
    
           protected void Application_Start()
            {
                // 依赖注入
                var builder = new ContainerBuilder();
                SetupResolveRules(builder);
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                var container = builder.Build();
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
    
                AreaRegistration.RegisterAllAreas();
    
                RegisterGlobalFilters(GlobalFilters.Filters);
                RegisterRoutes(RouteTable.Routes);
    
            }

    使用构造函数注入

        public class SysUserController : AdminBaseController
        {
            private ISysUserMgr sysUserMgr;// = new SysUserMgrImp();
    
            public SysUserController(ISysUserMgr mgr)
            {
                sysUserMgr = mgr;
            }
    ............................


    2、配置文件

    引入Autofac.dll,Autofac.Configuration.dll,Autofac.Configuration.dll

    修改web.config

        <configSections>
            <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
        </configSections>
    
        <autofac defaultAssembly="GPS.Domain">
            <components>
                <component type="GPS.Domain.Implements.SysUserMgrImp,GPS.Domain.Implements" service="GPS.Domain.Interfaces.ISysUserMgr" />
                <component type="GPS.Domain.Implements.FormsAuthServiceImp,GPS.Domain.Implements" service="GPS.Domain.Interfaces.IFormsAuthService" >
                </component>
            </components>
        </autofac>

    修改Global.asax

            protected void Application_Start()
            {
                // 依赖注入
                var builder = new ContainerBuilder();
                builder.RegisterModule(new ConfigurationSettingsReader("autofac")); 
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                var container = builder.Build();
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
    
                AreaRegistration.RegisterAllAreas();
    
                RegisterGlobalFilters(GlobalFilters.Filters);
                RegisterRoutes(RouteTable.Routes);
            }

    使用构造函数注入

        public class AccountController : Controller
        {
    
            private ISysUserMgr sysUserMgr;// = new SysUserMgrImp();
            private IFormsAuthService formAuthService;// = new FormsAuthServiceImp();
    
            public AccountController(ISysUserMgr userMgr, IFormsAuthService formAuth)
            {
                sysUserMgr = userMgr;
                formAuthService = formAuth;
            }
    ........................


    参考
    http://code.google.com/p/autofac/wiki/Mvc3Integration
    http://code.google.com/p/autofac/wiki/XmlConfiguration

  • 相关阅读:
    素数筛法
    UVA, 967 Circular
    软件开发一些常用工具
    iOS 一些常用方法笔记
    iOS 项目调试
    UINavigation的基本使用
    Tomcat多实例部署
    五联疫苗介绍
    互联网公司架构
    分布式事务
  • 原文地址:https://www.cnblogs.com/dbasys/p/2598691.html
Copyright © 2020-2023  润新知