### Register IDataLoaderBuilderFactory with Dependency Injection Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This snippet demonstrates how to register an IDataLoaderBuilderFactory with Microsoft.Extensions.DependencyInjection for use with ViewModels in Chinook.DynamicMvvm. It shows the setup within a HostBuilder. ```csharp var serviceProvider = new HostBuilder() .ConfigureServices(serviceCollection => serviceCollection .AddSingleton() ) .Build() .Services; ``` -------------------------------- ### Load Data on a Background Thread Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This example shows how to configure an IDataLoader to execute its load function on a background thread using IDataLoaderBuilder.OnBackgroundThread. This is beneficial for preventing UI thread blocking during data loading. ```csharp public IDataLoader Stuff => this.GetDataLoader(LoadStuff, b => b // Invoke the load from a new task. .OnBackgroundThread() ); ``` -------------------------------- ### Dispose Previous Data in DataLoader Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This example shows how to configure an IDataLoader to automatically dispose of its previous data when new data is loaded. This is useful when the data itself implements IDisposable or is an IEnumerable of IDisposable items. ```csharp public IDataLoader Jokes => this.GetDataLoader(LoadJokes, b => b // Dispose the previous ItemViewModels when Jokes produces new values. .DisposePreviousData() ); ``` -------------------------------- ### Mermaid: Initial Data Loading State Flow Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Illustrates the state transitions for initial data loading, from an idle state to loading, and finally to displaying data. ```mermaid stateDiagram-v2 direction LR s1: IsLoading(false)
Data(null)
Error(null) s2: IsLoading(true)
Data(null)
Error(null) s3: IsLoading(false)
Data(myData)
Error(null) s1 --> s2 : Start loading s2 --> s3 : Receive data ``` -------------------------------- ### Load Data with Request and Context (C#) Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Demonstrates how to access the source trigger and context from an IDataLoaderRequest within a data loading function. It shows how to read and write to the context, and includes a placeholder for the asynchronous data loading operation. ```C# Task LoadData(CancellationToken ct, IDataLoaderRequest request) { // This is the trigger that caused that loading operation. var sourceTrigger = request.SourceTrigger; // This is the context that was passed to the loading operation from the trigger. var context = request.Context; // You can read and write using the context. context["myMessage"] = "Hello!"; // Your async operation here. } ``` -------------------------------- ### Add DataLoader Factory with Default Behaviors (C#) Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Demonstrates how to create an IDataLoaderBuilderFactory with default behaviors like running on a background thread and providing an empty selector using Microsoft.Extensions.DependencyInjection. The factory is registered as a singleton in the service collection. ```csharp private static IServiceCollection AddDataLoaderFactory(this IServiceCollection services) { return services.AddSingleton(s => { return new DataLoaderBuilderFactory(b => b .OnBackgroundThread() .WithEmptySelector(GetIsEmpty) ); bool GetIsEmpty(IDataLoaderState state) { return state.Data == null || (state.Data is IEnumerable enumerable && !enumerable.Cast().Any()); } }); } ``` -------------------------------- ### C#: Configure DataLoader Empty State Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Shows how to configure a DataLoader to determine if the content is empty using a custom selector based on the state's data. ```csharp MyAsyncList = DataLoaderBuilder> .Empty .WithName("MyAsyncList") .WithLoadMethod(LoadMyAsyncList) .WithEmptySelector(state => state.Data is null || state.Data.Count == 0) .Build(); ``` -------------------------------- ### Initialize DataLoader in ViewModel (C#) Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Demonstrates how to initialize an IDataLoader in a ViewModel using DataLoaderBuilder. It includes setting a name, defining a load method, and building the data loader instance. The load method is an async task that simulates data fetching. ```csharp public MainPageViewModel() { MyAsyncValue = DataLoaderBuilder .Empty .WithName("MyAsyncValue") .WithLoadMethod(LoadMyAsyncValue) .Build(); } public IDataLoader MyAsyncValue { get; } private async Task LoadMyAsyncValue(CancellationToken ct, IDataLoaderRequest request) { // Your async operation here. await Task.Delay(1000, ct); return default(int); } ``` -------------------------------- ### Implementing Async Data Loading Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This C# code shows the required signature for the Load function within an IDataLoader. It should return a Task of the data type and accept a CancellationToken for cancellation. ```C# async Task LoadData(CancellationToken ct, IDataLoaderRequest request) { // Put your async operation here. return default(MyData); } ``` -------------------------------- ### Using DataLoaderView in XAML Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This XAML snippet demonstrates how to integrate the DataLoaderView into your application. It binds to an asynchronous data source and includes a DataTemplate for displaying the loaded data. ```XAML ``` -------------------------------- ### Mermaid: Data Refresh with Network Error State Flow Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Demonstrates state changes during a pull-to-refresh action when a network error occurs, preserving previous data. ```mermaid stateDiagram-v2 direction LR s3: IsLoading(false)
Data(myData)
Error(null) s4: IsLoading(true)
Data(myData)
Error(null) s5: IsLoading(false)
Data(myData)
Error(WebException) s3 --> s4 : Start refresh s4 --> s5 : Receive error
because not network ``` -------------------------------- ### DataLoaderView with Throttling Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md This XML snippet demonstrates how to configure the DataLoaderView with properties to manage UI flickering. StateMinimumDuration sets the minimum time a visual state is displayed, while StateChangingThrottleDelay controls the delay before updating to a new state, preventing unnecessary loading indicators. ```XML ``` -------------------------------- ### DataLoaderView XAML Templates and Style (XML) Source: https://github.com/nventive/chinook.dataloader/blob/main/README.md Provides XAML code for defining templates and a default style for DataLoaderView. This includes templates for empty states, error notifications, and error states, along with a comprehensive style that configures visual state management and template assignments. ```xml