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

 

 

 

转载于:https://www.cnblogs.com/kikyoqiang/p/10822650.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值