Class PoolService<T>
IObjectPool<T> manager which simplifies pool usage, adding methods for automatic objects creation and cleaning up when returned to pool.
When PoolService<T> created you should provide Eco.Shared.Pools.PoolService`1.factory method for new object instantiation (if pool is empty) and optionally
Eco.Shared.Pools.PoolService`1.onReturn method which will be called on an object when it returned (added) to pool.
Inheritance
System.Object
PoolService<T>
Assembly: Eco.Shared.dll
Syntax
public class PoolService<T> : Object where T : class
Type Parameters
Constructors
PoolService(IObjectPool<T>, Func<T>, Action<T>)
Declaration
public PoolService(IObjectPool<T> pool, Func<T> factory, Action<T> onReturn = null)
Parameters
Type |
Name |
Description |
IObjectPool<T> |
pool |
|
System.Func<T> |
factory |
|
System.Action<T> |
onReturn |
|
Methods
Rent()
Rents object from pool or creates new instance using Eco.Shared.Pools.PoolService`1.factory.
Declaration
Returns
RentAndPromiseToReturn(out T)
using
operator friendly Rent().
One of common cases is when you need to rent an object only for scope of the function and then return it by the end of the function like:
void MyFunc()
{
var obj = poolService.Rent();
try
{
DoSomethingWithObject(obj);
}
finally
{
poolService.Return(obj);
}
}
This method let you optimize a layout and don't care about Return
by the end of function like:
void MyFunc()
{
using var promise = poolService.RentAndPromiseToReturn(out var obj);
DoSomethingWithObject(obj);
}
or
void MyFunc()
{
using (poolService.RentAndPromiseToReturn(out var obj))
DoSomethingWithObject(obj);
}
Declaration
public PoolService<T>.ReturnPromise RentAndPromiseToReturn(out T value)
Parameters
Type |
Name |
Description |
T |
value |
|
Returns
Return(T)
Returns (add) value
to the pool. If Eco.Shared.Pools.PoolService`1.onReturn method was provided then it will be called on value
before adding to pool.
Also it may fail to add value
to Eco.Shared.Pools.PoolService`1.pool (in example if Eco.Shared.Pools.PoolService`1.pool is fixed size and already full).
In this case Eco.Shared.Pools.PoolService`1.onReturn will be called anyway, but method return false
. It returns true
if object was actually added to pool, you can check return value for final object cleanup.
Declaration
public bool Return(T value)
Parameters
Type |
Name |
Description |
T |
value |
|
Returns
Type |
Description |
System.Boolean |
|
Extension Methods