Custom DataLoader

A custom DataLoader is a class that derives from DataLoaderBase<TKey, TValue> and implements FetchData. This is very useful, not just in DI (Dependency Injection) scenarios, because the data fetching logic is defined inside the custom DataLoader itself; therefore, must not be provided repeatedly when creating new instances.

Creating a custom DataLoader is not that difficult. First of all we should create a dedicated marker interface for separation purposes.

public interface IUserDataLoader
    : IDataLoader<string, User>
{ }

Although the extra interface IUserDataLoader isn't necessarily required, we strongly recommend to create an extra interface in this particular case because of several reasons. One reason is you might have a handful of DataLoader which implementing a completely different data fetching logic, but from the outside they look identic due to their identic type parameter list. That's why we should always create a separate interface for each DataLoader. We just mentioned one reason here because the explanation would go beyond the scope of custom DataLoader.

Last but not least, we have to create a new class deriving from DataLoaderBase<TKey, TValue> and implementing our marker interface.

public class UserDataLoader
    : DataLoaderBase<string, User>
    , IUserDataLoader
    protected override Task<IReadOnlyList<Result<User>>> Fetch(
        IReadOnlyList<string> keys)
        // Here goes our data fetching logic