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

posted @   深潭  阅读(5224)  评论(4编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!
点击右上角即可分享
微信分享提示