EsoxSolutions.ObjectPool 1.1.2

Additional Details

Please use the latest version and report any bugs to me.

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EsoxSolutions.ObjectPool" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EsoxSolutions.ObjectPool --version 1.1.2                
#r "nuget: EsoxSolutions.ObjectPool, 1.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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:

  1. The PoolModel class which holds an object in a pool.
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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