Show / Hide Table of Contents

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>
Namespace: Eco.Shared.Pools
Assembly: Eco.Shared.dll
Syntax
public class PoolService<T> : Object where T : class
Type Parameters
Name Description
T

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
public T Rent()
Returns
Type Description
T

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
Type Description
PoolService.ReturnPromise<>

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

CommandLine.FeedFromCommandLine(Object)
CommandLine.ToCommandLineArgs(Object, Func<Object, Boolean>)
ListUtil.DepthFirstTraversal<T>(T, Func<T, IEnumerable<T>>)
EnumerableExtensions.SingleItemAsEnumerable<T>(T)
EventUtils.RaiseEvent<TEventArgs>(Object, String, TEventArgs)
PredicateUtils.MatchesAll<TEnumerable, T>(T, TEnumerable)
PredicateUtils.MatchesAll<T>(T, Func<T, Boolean>[])
PredicateUtils.MatchesAny<TEnumerable, T>(T, TEnumerable)
ReflectionUtils.PropertyValue<T>(Object, PropertyInfo)
ReflectionUtils.TryGetPropertyValueByName<T>(Object, String, out T)
ReflectionUtils.GetPropertyValueByName<T>(Object, String)
ReflectionUtils.SetPropertyByName(Object, String, Object)
ReflectionUtils.GetStructPropertyByName<T>(Object, String)
ReflectionUtils.GetStringPropertyByName(Object, String)
ReflectionUtils.ZipByProperty<T>(Object, Object, Object, Func<T, T, T>)
☀
☾
In This Article
Back to top
Copyright (c) Strange Loop Games 2021
☀
☾