Class DataLoaderBase<TKey, TValue>
- Namespace
- GreenDonut
- Assembly
- GreenDonut.dll
A DataLoader creates a public API for loading data from a
particular data back-end with unique keys such as the `id` column of a
SQL table or document name in a MongoDB database, given a batch loading
function. -- facebook
Each DataLoader instance contains a unique memoized cache. Use
caution when used in long-lived applications or those which serve many
users with different access permissions and consider creating a new
instance per web request. -- facebook
This is an abstraction for all kind of DataLoaders.
public abstract class DataLoaderBase<TKey, TValue> : IDataLoader<TKey, TValue>, IDataLoader, IDisposable where TKey : notnull
Type Parameters
TKeyA key type.
TValueA value type.
- Inheritance
-
DataLoaderBase<TKey, TValue>
- Implements
-
IDataLoader<TKey, TValue>
- Derived
- Inherited Members
- Extension Methods
Constructors
DataLoaderBase(IBatchScheduler, DataLoaderOptions?)
Initializes a new instance of the DataLoaderBase<TKey, TValue> class.
protected DataLoaderBase(IBatchScheduler batchScheduler, DataLoaderOptions? options = null)
Parameters
batchSchedulerIBatchSchedulerA scheduler to tell the
DataLoaderwhen to dispatch buffered batches.optionsDataLoaderOptionsAn options object to configure the behavior of this particular DataLoaderBase<TKey, TValue>.
Exceptions
- ArgumentNullException
Throws if
optionsisnull.
Properties
Cache
Gets access to the cache of this DataLoader.
protected ITaskCache? Cache { get; }
Property Value
CacheKeyType
Gets the cache key type for this DataLoader.
protected virtual string CacheKeyType { get; }
Property Value
Methods
Clear()
public void Clear()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
FetchAsync(IReadOnlyList<TKey>, Memory<Result<TValue>>, CancellationToken)
A batch loading function which has to be implemented for each
individual DataLoader. For every provided key must be a
result returned. Also to be mentioned is, the results must be
returned in the exact same order the keys were provided.
protected abstract ValueTask FetchAsync(IReadOnlyList<TKey> keys, Memory<Result<TValue>> results, CancellationToken cancellationToken)
Parameters
keysIReadOnlyList<TKey>A list of keys.
resultsMemory<Result<TValue>>The resolved values which need to be in the exact same order as the keys were provided.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A list of results which are in the exact same order as the provided keys.
GetCacheKeyType(Type)
A helper to create a cache key type for a DataLoader.
protected static string GetCacheKeyType(Type type)
Parameters
typeTypeThe DataLoader type.
Returns
- string
Returns the DataLoader cache key.
GetCacheKeyType<TDataLoader>()
A helper to create a cache key type for a DataLoader.
protected static string GetCacheKeyType<TDataLoader>() where TDataLoader : IDataLoader
Returns
- string
Returns the DataLoader cache key.
Type Parameters
TDataLoaderThe DataLoader type.
LoadAsync(IReadOnlyCollection<TKey>, CancellationToken)
public Task<IReadOnlyList<TValue>> LoadAsync(IReadOnlyCollection<TKey> keys, CancellationToken cancellationToken = default)
Parameters
keysIReadOnlyCollection<TKey>cancellationTokenCancellationToken
Returns
- Task<IReadOnlyList<TValue>>
LoadAsync(TKey, CancellationToken)
public Task<TValue> LoadAsync(TKey key, CancellationToken cancellationToken = default)
Parameters
keyTKeycancellationTokenCancellationToken
Returns
- Task<TValue>
Remove(TKey)
public void Remove(TKey key)
Parameters
keyTKey
Set(TKey, Task<TValue>)
public void Set(TKey key, Task<TValue> value)
Parameters
keyTKeyvalueTask<TValue>
TryAddToCache<TK, TV>(string, TK, TV)
A helper to adds an additional cache lookup to a resolved entity.
protected void TryAddToCache<TK, TV>(string cacheKeyType, TK key, TV value) where TK : notnull
Parameters
cacheKeyTypestringThe cache key type that shall be used to refer to the entity.
keyTKThe key.
valueTVThe value.
Type Parameters
TKThe key type.
TVThe value type.
TryAddToCache<TItem, TK, TV>(string, IEnumerable<TItem>, Func<TItem, TK>, Func<TItem, TV>)
A helper to add additional cache lookups to a resolved entity.
protected void TryAddToCache<TItem, TK, TV>(string cacheKeyType, IEnumerable<TItem> items, Func<TItem, TK> key, Func<TItem, TV> value) where TK : notnull
Parameters
cacheKeyTypestringThe cache key type that shall be used to refer to the entity.
itemsIEnumerable<TItem>The items that shall be associated with other cache keys.
keyFunc<TItem, TK>A delegate to create the key part.
valueFunc<TItem, TV>A delegate to create the value that shall be associated.
Type Parameters
TItemThe item type.
TKThe key type.
TVThe value type.