This class is used to create instances that are unique per key in a multi-threaded environment. It uses a ReaderWriterLockSlim for read and write access to the Instances. Instance creations are synchronized with a Monitor on an object that is unique per key. This approach is designed to minimize the impact of long running instance creations on other threads. More...
Public Member Functions | |
SynchronizedSingletonFactory (CreateMethodDelegate< TKey, TValue > defaultCreateMethod, int lockTimeout) | |
Initializes a new instance of the SynchronizedSingletonFactory<TKey,TValue> class. More... | |
virtual bool | Add (TKey key, TValue value) |
Adds a value if the key has not been added before. More... | |
void | ForAll (Action< TValue > action) |
This method iterates over a copy of Instances and executes the action on each item. More... | |
TResult | ForAll< TResult > (Func< TValue, TResult > selector, Func< TResult, TResult, TResult > aggregateFunction, TResult seed) |
This method iterates over a copy of Instances. The selector parameter selects a value of each instance. These values are combined with the aggregateFunction . More... | |
virtual TValue | Get (TKey key, CreateMethodDelegate< TKey, TValue > createMethod) |
Gets an existing value for a key or creates a new one. More... | |
TValue | Get (TKey key) |
Gets an existing value for a key or creates a new one with the default CreateMethod. More... | |
TValue | GetBlockingInstance (TKey key, CreateMethodDelegate< TKey, TValue > createMethod) |
Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast. More... | |
TValue | GetBlockingInstance (TKey key) |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast. More... | |
TValue | GetBlockingOverall (TKey key, CreateMethodDelegate< TKey, TValue > createMethod) |
Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast. More... | |
TValue | GetBlockingOverall (TKey key) |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast. More... | |
TValue | GetNonBlocking (TKey key, CreateMethodDelegate< TKey, TValue > createMethod) |
Gets an existing value for a key or creates a new one. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces. More... | |
TValue | GetNonBlocking (TKey key) |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces. More... | |
virtual bool | Remove (TKey key) |
Removes a value from the Instances. More... | |
virtual bool | TryGet (TKey key, out TValue value) |
Tries to get an existing value for the key. More... | |
Protected Member Functions | |
virtual void | DoAdd (TKey key, TValue value) |
Adds a value to the Instances. Calling methods need to guard the Instances with a WriterLock. Calling methods are: Add, GetBlockingInstance, GetNonBlocking and GetBlockingOverall. More... | |
IDisposable | ReaderLock () |
Enters a critical read section. Exit the critical section by disposing the return value. More... | |
IDisposable | WriterLock () |
Enters a critical write section. Exit the critical section by disposing the return value. More... | |
Properties | |
int | Count [get] |
Gets the number of added values. More... | |
CreateMethodDelegate< TKey, TValue > | CreateMethod [get, set] |
Gets or sets the default creation method for values. More... | |
Dictionary< TKey, TValue > | Instances [get] |
Gets a reference to the underlying dictionary that contains all existing instances. More... | |
int | LockTimeout [get] |
Gets the maxium timeout for critical sections. More... | |
This class is used to create instances that are unique per key in a multi-threaded environment. It uses a ReaderWriterLockSlim for read and write access to the Instances. Instance creations are synchronized with a Monitor on an object that is unique per key. This approach is designed to minimize the impact of long running instance creations on other threads.
TKey | The type of key. |
TValue | The type of value. |
Instance members are thread safe unless specified otherwise.
|
inline |
Initializes a new instance of the SynchronizedSingletonFactory<TKey,TValue> class.
defaultCreateMethod | The default create method. |
lockTimeout | The max timeout to wait to enter a critical section. |
|
inlinevirtual |
Adds a value if the key has not been added before.
key | The key. |
value | The value. |
|
inlineprotectedvirtual |
Adds a value to the Instances. Calling methods need to guard the Instances with a WriterLock. Calling methods are: Add, GetBlockingInstance, GetNonBlocking and GetBlockingOverall.
key | The key. |
value | The value. |
|
inline |
This method iterates over a copy of Instances and executes the action on each item.
action | The action. |
|
inline |
This method iterates over a copy of Instances. The selector parameter selects a value of each instance. These values are combined with the aggregateFunction .
selector | The action that maps a value to each instance. |
aggregateFunction | The function that combines all selector results. |
seed | The result value to start with. |
TResult | The type of the result value. |
|
inline |
Gets an existing value for a key or creates a new one with the default CreateMethod.
key | The key. |
|
inlinevirtual |
Gets an existing value for a key or creates a new one.
key | The key. |
createMethod | The creation method. |
The default implementation uses the GetBlockingInstance algorithm. Override to change behavior to GetNonBlocking or GetBlockingOverall
|
inline |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast.
key | The key. |
|
inline |
Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast.
key | The key. |
createMethod | The creation method. |
|
inline |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast.
key | The key. |
|
inline |
Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast.
key | The key. |
createMethod | The creation method. |
|
inline |
Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces.
key | The key. |
|
inline |
Gets an existing value for a key or creates a new one. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces.
key | The key. |
createMethod | The creation method. |
|
inlineprotected |
Enters a critical read section. Exit the critical section by disposing the return value.
LockTimeoutException | A read lock could not be obtained within the LockTimeout. |
|
inlinevirtual |
Removes a value from the Instances.
key | The key. |
|
inlinevirtual |
Tries to get an existing value for the key.
key | The key. |
value | The value. |
|
inlineprotected |
Enters a critical write section. Exit the critical section by disposing the return value.
LockTimeoutException | A write lock could not be obtained within the LockTimeout. |
|
get |
Gets the number of added values.
|
getset |
Gets or sets the default creation method for values.
|
getprotected |
|
getprotected |
Gets the maxium timeout for critical sections.