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(),对,就是酱紫。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值