Class DerivedReferencedResource<TKey, TValue, TSourceValue>
- Namespace
- Acuit.Pinpoint.ResourceManagement
- Assembly
- Acuit.Pinpoint.ResourceManagement.Abstractions.dll
A base class for implementing IReferencedResource<TKey, TValue> for resources that derive their values from other resource values.
public abstract class DerivedReferencedResource<TKey, TValue, TSourceValue> : ReferencedResource<TKey, TValue>, IReferencedResource<TKey, TValue>, IDisposable where TKey : notnull
Type Parameters
TKeyThe type of the key used to identify different resources, which must be a non-nullable type.
TValueThe resource value type.
TSourceValueThe source resource value type.
- Inheritance
-
ReferencedResource<TKey, TValue>DerivedReferencedResource<TKey, TValue, TSourceValue>
- Implements
-
IReferencedResource<TKey, TValue>
- Inherited Members
Remarks
Implementations deriving from this class must do the following:
- Provide a constructor that calls the base class constructor with the necessary arguments.
- Provide an override for GetDerivedValueAsync(TSourceValue, CancellationToken) that produces a derived value from a source value.
Note that the change token exposed by this class simply reflects the change token of the source resource. This means that:
- This assumes that the deriviation of a source value to a derived value is deterministic based only on the source value. If this is not the case, this class would not be appropriate to use.
- If a source value changes, but the derived value ends up being the same as the previously-derived value, references to this resource will still be notified of a change.
Constructors
DerivedReferencedResource(TKey, Func<IResourceReference<TSourceValue>>)
Initializes a new instance of the DerivedReferencedResource<TKey, TValue, TSourceValue> class.
protected DerivedReferencedResource(TKey key, Func<IResourceReference<TSourceValue>> sourceResourceReferenceFactory)
Parameters
keyTKeyThe resource key.
sourceResourceReferenceFactoryFunc<IResourceReference<TSourceValue>>A factory that will create a reference to the source resource.
Exceptions
- ArgumentNullException
keyis null.- ArgumentNullException
sourceResourceReferenceFactoryis null.
Methods
Dispose(bool)
Closes and releases all resources used by the ReferencedResource<TKey, TValue>.
protected override void Dispose(bool disposing)
Parameters
Remarks
Derived classes should override this when they have any resources that should be disposed.
GetChangeToken()
Returns a IChangeToken that can be used to observe when this resource value changes.
protected override IChangeToken GetChangeToken()
Returns
GetDerivedValueAsync(TSourceValue, CancellationToken)
Produces the derived resource value from the source resource value.
protected abstract Task<TValue> GetDerivedValueAsync(TSourceValue sourceValue, CancellationToken cancellationToken)
Parameters
sourceValueTSourceValueThe source value.
cancellationTokenCancellationTokenA cancellation token that can be used to request cancellation.
Returns
- Task<TValue>
The task object representing the asynchronous operation.
Remarks
Once the operation completes, the Result property on the returned task object contains the derived value.
This will only be called once with each new source value. If this throws an exception, then it will propagate to resource references.
Note that when this method throws an exception, all subsequent attempts to retrieve the resource value by references will cause this method to be called again, until it eventually returns a derived value that can be cached. If a derived value cannot be produced from a given source value, and if it is costly to determine this, then the implementation may choose to cache the derivation error so that it can immediately throw an exception rather than performing the costly derivation process again that will produce the same error.
Exceptions
- Exception
The resource value could not be derived. Specific exceptions depend on the implementation.
GetValueAsync(CancellationToken)
Gets the resource value.
protected override Task<TValue> GetValueAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA cancellation token that can be used to request canceling retrieving the resource.
Returns
- Task<TValue>
The task object representing the asynchronous operation.
Remarks
Once the operation completes, the Result property on the returned task object contains the resource value.
This method will be called on behalf of resource references, ensuring it is never called more than once at a time. The returned value will be cached and returned to all references until the change token (retrieved via GetChangeToken() before calling GetValueAsync(CancellationToken)) signals a change.
If this method throws an exception, it will be propagated to resource references (via their call to GetValueAsync(CancellationToken)), and subsequent calls to GetValueAsync(CancellationToken) by resource references will cause this method to be called again.
Exceptions
- Exception
The resource could not be retrieved. Specific exceptions depend on the implementation.