Support for pooled instance lifetime scopes in Autofac dependency injection.
Autofac can help you implement a pool of components in your application without you having to write your own pooling implementation, and making these pooled components feel more natural in the world of DI.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
Once you've added a reference to the Autofac.Pooling
package, you can start using
the new PooledInstancePerLifetimeScope
and PooledInstancePerMatchingLifetimeScope
methods:
var builder = new ContainerBuilder();
builder.RegisterType<MyCustomConnection>()
.As<ICustomConnection>()
.PooledInstancePerLifetimeScope();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
// Creates a new instance of MyCustomConnection
var instance = scope.Resolve<ICustomConnection>();
instance.DoSomething();
}
// When the scope ends, the instance of MyCustomConnection
// is returned to the pool, rather than being disposed.
using (var scope2 = container.BeginLifetimeScope())
{
// Does **not** create a new instance, but instead gets the
// previous instance from the pool.
var instance = scope.Resolve<ICustomConnection>();
instance.DoSomething();
}
// Instance gets returned back to the pool again at the
// end of the lifetime scope.
Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.