Can I create implicit Lifetime coverage in a factory?
Does Autofac support implicit life scopes for using 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 every time _sessionFactory is called, Autofac automatically creates a nested lifetime scope?
+3
source to share
1 answer
Yes. Let the factory return Owned<Session>
. Native Instances indicates that the calling code is responsible for removing the service. In fact, if you allow Func<Owned<Session>>
from a container, Autofac will provide you with an automatic factory that creates such instances.
+5
source to share