Skip to content

Support for pooled instance lifetime scopes in Autofac dependency injection.

License

Notifications You must be signed in to change notification settings

autofac/Autofac.Pooling

Repository files navigation

Autofac.Pooling

Build status Open in Visual Studio Code

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.

Getting Started

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.

Get Help

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.

About

Support for pooled instance lifetime scopes in Autofac dependency injection.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published