Skip to content

Feature Idea: Pooled Instances #1162

@alistairjevans

Description

@alistairjevans

An idea came to me about a potentially useful new feature we could add in to Autofac; pooled instances.

Basically, when you resolve a registration configured as pooled, you get an instance from a pool of objects, not a new instance.

In more detail:

  • When you configure a registration, you specify PooledInstances() as the lifetime option instead of SingleInstance() or InstancePerLifetimeScope(). You could probably set an optional max capacity or something.
  • Behind the scenes, we register a ServicePool that contains pooled instances of the service.
  • When a user resolves the service, we give them (Rent) an instance from the pool, which is scoped to the LifetimeScope; when the scope ends, we hand the instance back (Return) to the pool (but don't dispose of the instance).
  • Services could implement a provided interface, INotifyPoolEvents, which has a Rent and Return method, called when we retrieve from the pool or put back, to do any necessary resetting. We could conceivably also store additional event handlers somehow to serve a similar purpose without modifying the type.
  • When the container (or whatever scope the pool was registered against) is disposed, all the items in the pool are disposed.

Any thoughts? Could give people a nice easy route to use a set of pooled 'something' in their DI?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions