/// <summary> /// Get or add a DataLoader instance for caching data fetching operations. /// </summary> /// <typeparam name="T">The type of data to be loaded</typeparam> /// <param name="context">The <seealso cref="DataLoaderContext"/> to get or add a DataLoader to</param> /// <param name="loaderKey">A unique key to identify the DataLoader instance</param> /// <param name="fetchFunc">A delegate to fetch data asynchronously</param> /// <returns>A new or existing DataLoader instance</returns> public static IDataLoader <T> GetOrAddLoader <T>(this DataLoaderContext context, string loaderKey, Func <Task <T> > fetchFunc) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (fetchFunc == null) { throw new ArgumentNullException(nameof(fetchFunc)); } return(context.GetOrAdd(loaderKey, () => new SimpleDataLoader <T>(WrapNonCancellableFunc(fetchFunc)))); }
/// <summary> /// Get or add a DataLoader instance for batching data fetching operations. /// </summary> /// <typeparam name="TKey">The type of key used to load data</typeparam> /// <typeparam name="T">The type of data to be loaded</typeparam> /// <param name="context">The <seealso cref="DataLoaderContext"/> to get or add a DataLoader to</param> /// <param name="loaderKey">A unique key to identify the DataLoader instance</param> /// <param name="fetchFunc">A cancellable delegate to fetch data for some keys asynchronously</param> /// <param name="keyComparer">An <seealso cref="IEqualityComparer{T}"/> to compare keys.</param> /// <param name="defaultValue">The value returned when no match is found in the dictionary, or default(T) if unspecified</param> /// <param name="maxBatchSize">The maximum number of keys passed to the fetch delegate at a time</param> /// <returns>A new or existing DataLoader instance</returns> public static IDataLoader <TKey, T> GetOrAddBatchLoader <TKey, T>(this DataLoaderContext context, string loaderKey, Func <IEnumerable <TKey>, CancellationToken, Task <IDictionary <TKey, T> > > fetchFunc, IEqualityComparer <TKey>?keyComparer = null, T defaultValue = default !, int maxBatchSize = int.MaxValue) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (fetchFunc == null) { throw new ArgumentNullException(nameof(fetchFunc)); } return(context.GetOrAdd(loaderKey, () => new BatchDataLoader <TKey, T>(fetchFunc, keyComparer, defaultValue, maxBatchSize))); }
/// <summary> /// Get or add a DataLoader instance for batching data fetching operations. /// </summary> /// <typeparam name="TKey">The type of key used to load data</typeparam> /// <typeparam name="T">The type of data to be loaded</typeparam> /// <param name="context">The <seealso cref="DataLoaderContext"/> to get or add a DataLoader to</param> /// <param name="loaderKey">A unique key to identify the DataLoader instance</param> /// <param name="fetchFunc">A delegate to fetch data for some keys asynchronously</param> /// <param name="keyComparer">An <seealso cref="IEqualityComparer{T}"/> to compare keys.</param> /// <param name="maxBatchSize">The maximum number of keys passed to the fetch delegate at a time</param> /// <returns>A new or existing DataLoader instance</returns> public static IDataLoader <TKey, IEnumerable <T> > GetOrAddCollectionBatchLoader <TKey, T>(this DataLoaderContext context, string loaderKey, Func <IEnumerable <TKey>, Task <ILookup <TKey, T> > > fetchFunc, IEqualityComparer <TKey>?keyComparer = null, int maxBatchSize = int.MaxValue) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (fetchFunc == null) { throw new ArgumentNullException(nameof(fetchFunc)); } return(context.GetOrAdd(loaderKey, () => new CollectionBatchDataLoader <TKey, T>(WrapNonCancellableFunc(fetchFunc), keyComparer, maxBatchSize))); }