zoukankan      html  css  js  c++  java
  • Autofac

    Autofac真是个好东西啊。

    自动注入。即可以替我们构造实例,使得我们能很方便的面向接口编程。

    面向接口编程的最大意义,就是解耦:定义和实现分离。调用的时候,将不同的实例赋给接口对象,就能实现所谓的多态。窃以为,面向接口编程是设计模式的基础和精髓。

    然后,autofac是面向接口的得力助手。

    为什么这么说呢?你看看:

        public class NormalSpider
        {
            ISpiderKernelService service;
            public NormalSpider(ISpiderKernelService service)
            {//构造函数有1个参数
                this.service = service;
            }
        }
        public class SpiderKernelService : ISpiderKernelService
        {
            ICatchResultRepository catchResultRepository;
            ICatchResultContentRepository catchResultContentRepository;
            IUnitOfWorkCycle unitOfWork;
            public SpiderKernelService(ICatchResultRepository catchResultRepository,
                ICatchResultContentRepository catchResultContentRepository,
                IUnitOfWorkCycle unitOfWork)
            {//构造函数有4个参数
                this.catchResultRepository = catchResultRepository;
                this.catchResultContentRepository = catchResultContentRepository;
                this.unitOfWork = unitOfWork;
            }
       }
       //然后,每个仓库类又各有参数若干。。。
       public class CatchResultRepository : RepositoryBase<CatchResult>, ICatchResultRepository
        {
            public CatchResultRepository(ISession session)
                : base(session)
            {
            }
        }
        public class CatchResultContentRepository : RepositoryBase<CatchResultContent>, ICatchResultContentRepository
        {
            public CatchResultContentRepository(ISession session)
                : base(session)
            {
            }
        }
       //吧啦吧啦。。。

    如果这个世界上没有autofac这类的工具,那我应该如何new一个对象来使用呢?上面语句中,统统将赋值语句的右边,改为new。而一旦new,后面的对象类型就必须要指定。既然在代码中已经写死,那就不是面向接口编程。

    autofac的好处就是,你可以进行所谓的注册。将指定的DLL,通过反射,进行注册,那么autofac就能够将里面的类,在系统启动之初,自动构造并赋给接口。更换DLL,就能对应不同的实现,而调用方,一点都不用修改。这种方式,对团队开发是最适合不过的了。比如说,应用程序开发人员和数据库开发人员的进度不一致,那么大家制定一套接口,都面向这套接口编程。应用程序开发人员可以自己提供假数据,而不必等待数据库开发人员弄好才能进一步工作。等到数据库弄好,再将DLL替换,神不知鬼不觉,无缝切换吗,此为“打桩”。

    看上去,autofac与asp.net mvc结合得比较好,自动就替我们处理了控制器中的实例构造。如果是win form,应用autofac,还做不到这么便利:

    namespace FormSpider
    {
        public class AutofacConfig
        {
            static IContainer container = null;
            public static IContainer Container
            {//勉强算是单例模式
                get
                {
                    if(container == null)
                    {
                        container = BuildContainer();
                    }
                    return container;
                }
            }
            static IContainer BuildContainer()
            {//autofac本质上,是要返回一个 IContainer,容器。利用这个容器,我们可以得到注册在里面的各种实例。
                var builder = new ContainerBuilder();
    
    //各种注册。。。
                builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
                //GZFBC.Services
                builder.RegisterAssemblyTypes(typeof(ISpiderKernelService).Assembly)
                    .Where(t => t.IsClass && t.Name.EndsWith("Service"))
                    .As(t => t.GetInterfaces().Single(i => i.Name.EndsWith(t.Name)))
                    .InstancePerLifetimeScope();
    
                //BaseLT.Data
                builder.RegisterAssemblyTypes(typeof(ISysFieldRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
                //GZFBC.Data
                builder.RegisterAssemblyTypes(typeof(IGZFBC_Data_Repository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();
    
                //using System.Configuration;
                string conn = ConfigurationManager.AppSettings["conn"];
                builder.Register(c => GZFBC.Data.Infrastructure.ConnectionHelper.BuildSessionFactory(conn)).As<ISessionFactory>().SingleInstance();
                builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).InstancePerLifetimeScope();
    
                return builder.Build();//构建一个Container返回
            }
        }
    }

    看看怎么调用:

        public class NormalSpider
        {
            ISpiderKernelService service;
            public NormalSpider(ISpiderKernelService service)
            {//构造函数有1个参数
                this.service = service;
            }
        }
    
    NormalSpider spider = new NormalSpider(
                                service: AutofacConfig.Container.Resolve<ISpiderKernelService>());

    AutofacConfig.Container.Resolve(),对,就是酱紫。

  • 相关阅读:
    luogu P2827 蚯蚓
    CHOI1001/1002 火车进出栈问题
    hdoj4699 Editor
    反弹shell监控
    AppScan 9.0.3.6 crack
    Spectre & Meltdown Checker – CPU芯片漏洞检查脚本Linux版
    Microsoft IIS WebDav 'ScStoragePathFromUrl' Remote Buffer Overflow (CVE-2017-7269)
    Shodan新手使用指南
    The Art of Subdomain Enumeration (转)
    DDOS攻击方式总结 (转)
  • 原文地址:https://www.cnblogs.com/leftfist/p/6808703.html
Copyright © 2011-2022 走看看