zoukankan      html  css  js  c++  java
  • Autofac 控制反转

        class Program
        {
            static void Main(string[] args)
            {
                IContainer container = Init();
                Go(container);
    
                Console.WriteLine("========================");
    
                ISchool a = container.Resolve<ISchool>();
                a.FangXue();
    
                Console.ReadKey();
            }
            static IContainer Init()
            {
                ContainerBuilder builder = new ContainerBuilder();
                Assembly asm = Assembly.Load(Assembly.GetExecutingAssembly().GetName().Name);
                builder.RegisterAssemblyTypes(asm).AsImplementedInterfaces().PropertiesAutowired().SingleInstance();
                //SingleInstance()单例,共享同一个对象,PropertiesAutowired()属性自动注入
                IContainer container = builder.Build();
                return container;
            }
            static void Go(IContainer container)
            {
                IDogBLL schoolBll = container.Resolve<IDogBLL>();
                schoolBll.Bark();
                IDogBLL schoolBll2 = container.Resolve<IDogBLL>();
                Console.WriteLine(schoolBll.Equals(schoolBll2));
            }
        }
        public interface IDogBLL
        {
            void Bark();
        }
        public class DogBLL : IDogBLL
        {
            public void Bark()
            {
                Console.WriteLine("汪汪汪");
            }
        }
        public interface ISchool
        {
            void FangXue();
        }
        public class School : ISchool
        {
            public IDogBLL dogBll { get; set; }
            public void FangXue()
            {
                dogBll.Bark();
                Console.WriteLine("放学了");
            }
        }

     Autofac.Mvc

            private void AutoFacMvc()
            {
                //using Autofac.Integration.Mvc;
                ContainerBuilder builder = new ContainerBuilder();
                // 把当前的 程序集中的 Controller 都注册
                builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
                // 不要忘了.PropertiesAutowired()
                Assembly asm = Assembly.Load("TestService"); //获取所有相关类库的程序集
                builder.RegisterAssemblyTypes(asm).Where(e => !e.IsAbstract).AsImplementedInterfaces().PropertiesAutowired();
    
                // 把当前的 程序集中的 所有类 都注册
                builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).PropertiesAutowired();
    
                Autofac.IContainer container = builder.Build();
                //注册系统级别的 DependencyResolver,
                //这样当 MVC框架创建Controller等对象的时候都是管Autofac要对象。 
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));   //!!!
            }

      Autofac.Mvc单独注册

                var test = DependencyResolver.Current.GetService<TestHelper>();
                var res = test.GetDateTime();

    不在Mvc线程里单独注册

                    var container = AutofacDependencyResolver.Current.ApplicationContainer;
                    using (container.BeginLifetimeScope())
                    {
                        var citySvc = container.Resolve<ICityService>();
                    }

     

  • 相关阅读:
    [转载]VC补遗之Profile篇
    [原创]百度之星2009初赛第二场第四题解答
    [总结]QT在CODE:BLOCKS中的配置
    [原创]自己写的一个简单的程序日志记录类
    [原创]QT动态加载UI文件注意事项
    window版本信息资源格式
    [原创]滚动条滚动范围的问题总结
    ofstream奇怪问题解决方法
    [转载]最小矩形(rec1)的解题报告
    oracle数据库用户之间授权
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/10822650.html
Copyright © 2011-2022 走看看