AlirezaMahDev.Extensions.ParameterInstance
0.0.1-alpha7
dotnet add package AlirezaMahDev.Extensions.ParameterInstance --version 0.0.1-alpha7
NuGet\Install-Package AlirezaMahDev.Extensions.ParameterInstance -Version 0.0.1-alpha7
<PackageReference Include="AlirezaMahDev.Extensions.ParameterInstance" Version="0.0.1-alpha7" />
<PackageVersion Include="AlirezaMahDev.Extensions.ParameterInstance" Version="0.0.1-alpha7" />
<PackageReference Include="AlirezaMahDev.Extensions.ParameterInstance" />
paket add AlirezaMahDev.Extensions.ParameterInstance --version 0.0.1-alpha7
#r "nuget: AlirezaMahDev.Extensions.ParameterInstance, 0.0.1-alpha7"
#:package AlirezaMahDev.Extensions.ParameterInstance@0.0.1-alpha7
#addin nuget:?package=AlirezaMahDev.Extensions.ParameterInstance&version=0.0.1-alpha7&prerelease
#tool nuget:?package=AlirezaMahDev.Extensions.ParameterInstance&version=0.0.1-alpha7&prerelease
AlirezaMahDev.Extensions.ParameterInstance
Project Description
This project provides a high-performance, thread-safe implementation of parameter-based instance factories. It enables efficient creation, caching, and management of object instances based on parameter values, significantly improving performance in scenarios where object creation is expensive or where instance reuse is desired.
Key Features
- Thread-Safe Caching: Implements a concurrent dictionary for safe multi-threaded access
- Lazy Initialization: Creates instances only when first requested
- Resource Management: Automatic cleanup of disposable resources
- Dependency Injection: Seamless integration with .NET's DI container
- Flexible Configuration: Customizable caching and instance creation behaviors
- High Performance: Optimized for low-latency and high-throughput scenarios
Core Components
ParameterInstanceFactory<TInstance, TParameter>
The main factory class that implements IParameterInstanceFactory<TInstance, TParameter>:
- Thread-Safe Operations: Uses
ConcurrentDictionaryfor thread-safe instance caching - Lazy Initialization: Implements lazy instantiation with
Lazy<T> - Resource Management: Automatically disposes
IDisposableandIAsyncDisposableinstances - Dependency Injection: Uses
ActivatorUtilitiesfor service resolution - Instance Management: Methods to remove or clear cached instances
ParameterInstanceFactoryExtensions
Extension methods that simplify working with the factory in a DI container:
AddParameterInstanceFactory(): Registers the factory with the service collectionGetOrAddParameterInstance(): Helper method to get or create instancesTryRemoveParameterInstance(): Helper method to remove instances from cache
Usage Example
Registration
// In Startup.cs or Program.cs
services.AddParameterInstanceFactory<UserContext, UserSession>(
options =>
{
// Configure factory options
options.CacheSize = 1000;
options.EnableAutoCleanup = true;
},
(provider, context) =>
{
// Create and configure the instance
var logger = provider.GetRequiredService<ILogger<UserSession>>();
return new UserSession(context.UserId, context.TenantId, logger);
});
Usage in Application
public class UserRequestProcessor
{
private readonly IParameterInstanceFactory<UserSession, UserContext> _sessionFactory;
public UserRequestProcessor(
IParameterInstanceFactory<UserSession, UserContext> sessionFactory)
{
_sessionFactory = sessionFactory;
}
public async Task ProcessRequest(UserContext context)
{
// Get or create a session for the user context
// This will create the session only if it doesn't exist in the cache
var session = _sessionFactory.GetOrCreate(context);
try
{
// Use the session...
await session.ProcessRequestAsync();
}
finally
{
// Optionally remove the session if it's no longer needed
_sessionFactory.TryRemove(context);
}
}
}
Dependencies
Project Dependencies
- AlirezaMahDev.Extensions.ParameterInstance.Abstractions: Defines the core interfaces
- AlirezaMahDev.Extensions: Provides base builder patterns
Why These Dependencies?
- ParameterInstance.Abstractions: Provides the contract interfaces that this project implements
- Extensions: Supplies the builder infrastructure for DI integration
Performance Considerations
- Memory Usage: The factory maintains a cache of instances, so be mindful of memory consumption
- Thread Safety: All operations are thread-safe, but consider the thread safety of the instances being created
- Lifetime Management: Instances are kept in memory until explicitly removed or the factory is disposed
Best Practices
- Use for Expensive Objects: Best suited for objects that are expensive to create
- Monitor Cache Size: Keep an eye on the cache size to prevent memory leaks
- Clean Up: Explicitly remove instances when they're no longer needed
- Thread Safety: Ensure that the instances you create are thread-safe if they'll be accessed from multiple threads
- Disposable Instances: Always implement
IDisposableorIAsyncDisposablefor resources that need cleanup
Implementation Details
The factory uses a combination of ConcurrentDictionary and Lazy<T> to ensure thread-safe lazy initialization. When an instance is requested:
- The factory checks if an instance exists for the given parameters
- If not, it creates a new
Lazy<T>instance that will create the actual object when needed - The
Lazy<T>ensures that the object is created only once, even if multiple threads request it simultaneously - The instance is stored in the cache for future use
- When the factory is disposed, it disposes of all disposable instances in the cache
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AlirezaMahDev.Extensions (>= 0.0.1-alpha7)
- AlirezaMahDev.Extensions.ParameterInstance.Abstractions (>= 0.0.1-alpha7)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AlirezaMahDev.Extensions.ParameterInstance:
| Package | Downloads |
|---|---|
|
AlirezaMahDev.Extensions.File
Package Description |
|
|
AlirezaMahDev.Extensions.Brain
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-alpha7 | 409 | 12/8/2025 |
| 0.0.1-alpha | 276 | 12/7/2025 |