What is InstancePerLifetimeScope in Autofac?

我的未来我决定 提交于 2019-12-10 13:38:03

问题


Can someone please explain in plain English what the lines of code where I put the question marks do? Or maybe point me to an article that puts light on this. This code is for registering dependencies in an autofac container

var builder = new Autofac.ContainerBuilder();


builder.Register<NHibernateInstance>(c => 
    new NHibernateInstance(ConnString, false))
       .InstancePerDependency();//?????

builder.Register(c => c.Resolve<NHibernateInstance>()
    .GetFactory().OpenSession())
    .As<ISession>()
    .InstancePerLifetimeScope(); //-----?????

回答1:


This is a dependency injection container. The Autofac.ContainerBuilder gets a new container, or registrar you might say.

The builder.Register<NHibernateInstance> is stating that when constructing an NHibernateInstance during the recovery phase (i.e. getting an instance out of the container) this is how it should be built.

The last line is indicating that when resolving an NHibernateInstance the OpenSession method should be called once per the lifetime of the object.



来源:https://stackoverflow.com/questions/16946706/what-is-instanceperlifetimescope-in-autofac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!