Can I create an implicit Lifetime scope within a factory?

不打扰是莪最后的温柔 提交于 2019-12-10 21:07:16

问题


Does Autofac support implicit lifetime scopes for the use of a factory? For example, I would like to be able to do the following

public class Session : IDisposable
{
    public Session(A a, B b, C c)
    {
        ...
    }
} 

...

using (var session = _sessionFactory())
{
    ...
}

and then upon each call to the _sessionFactory have Autofac automatically create a nested lifetime scope?


回答1:


Yes. Let the factory return Owned<Session>. Owned instances indicates that the calling code is responsible for disposing the service. Actually, if you resolve a Func<Owned<Session>> from the container, Autofac will give you an automatic factory that produces such instances.



来源:https://stackoverflow.com/questions/14544952/can-i-create-an-implicit-lifetime-scope-within-a-factory

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