-
-
Notifications
You must be signed in to change notification settings - Fork 846
Closed
Description
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
ServicePoolthat 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 aRentandReturnmethod, 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?
Reactions are currently unavailable