EsoxSolutions.ObjectPool
1.1.2
Please use the latest version and report any bugs to me.
See the version list below for details.
dotnet add package EsoxSolutions.ObjectPool --version 1.1.2
NuGet\Install-Package EsoxSolutions.ObjectPool -Version 1.1.2
<PackageReference Include="EsoxSolutions.ObjectPool" Version="1.1.2" />
paket add EsoxSolutions.ObjectPool --version 1.1.2
#r "nuget: EsoxSolutions.ObjectPool, 1.1.2"
// Install EsoxSolutions.ObjectPool as a Cake Addin #addin nuget:?package=EsoxSolutions.ObjectPool&version=1.1.2 // Install EsoxSolutions.ObjectPool as a Cake Tool #tool nuget:?package=EsoxSolutions.ObjectPool&version=1.1.2
EsoxSolutions.ObjectPool
Overview
Sometimes, for reasons of efficiency, it can be quite handy to keep a pool, that is non-empty set, of initialized objects ready. This can be the case in for instance when you have database connections, which are expensive to create both in time and resources.
What you do is that make a pool of objects, usually an array, and you have one object managing that array, giving out objects, and returning, basically doing the bookkeeping.
This is a very simple implementation of such a pool, which is thread safe, and can be used in a multi-threaded environment.
Caveat: I consider this version to be still in beta. If you find any bugs, please report them to me, or mail me at info@esoxsolutions.nl
Usage
There are two main classes:
- The PoolModel class which holds an object in a pool.
- The ObjectPool which releases and returns the PoolModel objects.
PoolModel
The PoolModel is a generic class. It takes a type parameter, which is the type of the object you want to pool. To get the value stored in the PoolModel, use the Unwrap method.
An example use:
var initialObjects = new List<int> { 1, 2, 3 };
var objectPool = new ObjectPool<int>(initialObjects);
using (var model = objectPool.GetObject())
{
var value = model.Unwrap();
Console.WriteLine(value);
}
ObjectPool
The objectpool as you can see administers the object. In its constructor its gets a list of pre-initialized objects. The length of this list does not change. In case there are no more objects in the pool, an exception is raised.
QueryableObjectPool
The QueryableObjectPool is a special type of ObjectPool. It has a Query method, which takes a predicate, and returns the first object in the pool that matches the predicate. If no object matches the predicate, an exception is raised.
An example use:
var initialObjects = new List<int> { 1, 2, 3 };
var objectPool = new QueryableObjectPool<int>(initialObjects);
using (var model = objectPool.GetObject(x => x == 2))
{
var value = model.Unwrap();
Console.WriteLine(value);
}
Future work
One possible extension would be to have a timeout on the objects in the pool. If an object is not used for a certain amount of time, it is disposed. This would be useful in case you have a pool of database connections, and you want to make sure that the connections are not kept open for too long.
Version history:
* 1.1.1: Added QueryableObjectPool
* 1.1.2: Improved threadsafety
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
version 1.0: Beta