Class SharedResource<T>
Manages a shared resource of type T
, providing controlled access and ensuring efficient reuse.
This class is designed to handle scenarios where a resource is expensive to create and should be reused
for as long as it is viable. The resource is created upon first request and subsequently "rented" out
for use. The class ensures that the same resource instance is reused across multiple requests, maximizing efficiency.
Inherited Members
Namespace: PoeShared.Services
Assembly: PoeShared.dll
Syntax
public sealed class SharedResource<T> : DisposableReactiveObject, IDisposable, INotifyPropertyChanged where T : SharedResourceBase
Type Parameters
Name | Description |
---|---|
T | The type of the shared resource. Must be a subclass of SharedResourceBase. |
Remarks
The SharedResource<T> class is especially useful in situations where resource creation is costly, such as network connections, database connections, or large data structures. It ensures that the overhead of creating such resources is incurred only once and the resource is reused until it is no longer valid or the SharedResource<T> itself is disposed.
The class also manages the lifecycle of the resource. The resource is expected to remain alive and usable until the SharedResource<T> class is disposed. Upon disposal, the SharedResource<T> class disposes of the managed resource as well, ensuring proper cleanup. If the resource disposes itself or becomes unusable (e.g., a network connection is lost), the class will automatically create a new instance of the resource upon the next request.
The class employs thread-safe mechanisms to ensure that the resource can be accessed concurrently from multiple threads without race conditions. This makes it suitable for use in multi-threaded environments.
Constructors
Name | Description |
---|---|
SharedResource(Func<T>) | Initializes a new instance of the SharedResource<T> class. |
Properties
Name | Description |
---|---|
IsRented | Gets a value indicating whether the current instance of the shared resource is rented. |
Methods
Name | Description |
---|---|
RentOrCreate() | Rents the current instance of the shared resource or creates a new one if necessary. |