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
TKey
A key type.
TValue
A 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
batchScheduler
IBatchSchedulerA scheduler to tell the
DataLoader
when to dispatch buffered batches.options
DataLoaderOptionsAn options object to configure the behavior of this particular DataLoaderBase<TKey, TValue>.
Exceptions
- ArgumentNullException
Throws if
options
isnull
.
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
disposing
bool
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
keys
IReadOnlyList<TKey>A list of keys.
results
Memory<Result<TValue>>The resolved values which need to be in the exact same order as the keys were provided.
cancellationToken
CancellationTokenA 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
type
TypeThe 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
TDataLoader
The DataLoader type.
LoadAsync(IReadOnlyCollection<TKey>, CancellationToken)
public Task<IReadOnlyList<TValue>> LoadAsync(IReadOnlyCollection<TKey> keys, CancellationToken cancellationToken = default)
Parameters
keys
IReadOnlyCollection<TKey>cancellationToken
CancellationToken
Returns
- Task<IReadOnlyList<TValue>>
LoadAsync(TKey, CancellationToken)
public Task<TValue> LoadAsync(TKey key, CancellationToken cancellationToken = default)
Parameters
key
TKeycancellationToken
CancellationToken
Returns
- Task<TValue>
Remove(TKey)
public void Remove(TKey key)
Parameters
key
TKey
Set(TKey, Task<TValue>)
public void Set(TKey key, Task<TValue> value)
Parameters
key
TKeyvalue
Task<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
cacheKeyType
stringThe cache key type that shall be used to refer to the entity.
key
TKThe key.
value
TVThe value.
Type Parameters
TK
The key type.
TV
The 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
cacheKeyType
stringThe cache key type that shall be used to refer to the entity.
items
IEnumerable<TItem>The items that shall be associated with other cache keys.
key
Func<TItem, TK>A delegate to create the key part.
value
Func<TItem, TV>A delegate to create the value that shall be associated.
Type Parameters
TItem
The item type.
TK
The key type.
TV
The value type.