Autofac

网友投稿 861 2022-12-02

Autofac

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, ICatchResultRepository { public CatchResultRepository(ISession session) : base(session) { } } public class CatchResultContentRepository : RepositoryBase, ICatchResultContentRepository { public CatchResultContentRepository(ISession session) : base(session) { } } //吧啦吧啦。。。

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

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

看上去,autofac与asp- 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().As().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().SingleInstance(); builder.Register(c => c.Resolve().OpenSession()).InstancePerLifetimeScope(); return builder.Build();//构建一个Container返回

看看怎么调用:

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:VMware一些使用心得
下一篇:mysql允许远程访问
相关文章

 发表评论

暂时没有评论,来抢沙发吧~