Phetch.Core 0.6.0

dotnet add package Phetch.Core --version 0.6.0
NuGet\Install-Package Phetch.Core -Version 0.6.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Phetch.Core" Version="0.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phetch.Core --version 0.6.0
#r "nuget: Phetch.Core, 0.6.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Phetch.Core as a Cake Addin
#addin nuget:?package=Phetch.Core&version=0.6.0

// Install Phetch.Core as a Cake Tool
#tool nuget:?package=Phetch.Core&version=0.6.0

Phetch  Nuget License: MIT codecov

Documentation | API Reference | Sample app

Phetch is a small Blazor library for handling async query state, in the style of React Query, SWR, or RTK Query.

Currently, Phetch is only designed for use with Blazor WebAssembly. However, the core package (Phetch.Core) has no dependencies on Blazor or ASP.NET Core, so in theory it can be used anywhere that supports .NET Standard 2.1.

ℹ️ Status: All core features are finished, but there may be some minor breaking changes before a v1.0.0 release.

Features

  • Automatically handles loading and error states, and updates your components whenever the state changes.
  • Automatically caches data returned by queries, and makes it easy to invalidate or update this cached data when needed.
  • Supports calling any async method as a query (not restricted just to HTTP requests).
  • Supports dependent queries, pagination, prefetching, request de-duplication, retries, CancellationTokens, and more.
  • 100% strongly typed, with nullability annotations.
  • Lightweight and easy to mix-and-match with other state management methods.
  • No Javascript needed!

Show me some code!

Click here to view the source code for the sample project, with more detailed examples.

Below is the code for a basic component that runs a query when the component is first loaded. Phetch can do a whole lot more than that though, so make sure to check out the samples project and full documentation!

Defining an endpoint:

using Phetch.Core;

// This defines an endpoint that takes an int and returns a bool.
var isEvenEndpoint = new Endpoint<int, bool>(
    // Replace this part with your own async function:
    async (value, cancellationToken) =>
    {
        var response = await httpClient.GetFromJsonAsync<dynamic>(
            $"https://api.isevenapi.xyz/api/iseven/{value}",
            cancellationToken);
        return response.IsEven;
    }
);

Using the endpoint in a component:

@using Phetch.Blazor

<UseEndpoint Endpoint="isEvenEndpoint" Arg="3" Context="query">
    @if (query.IsError) {
        <p><em>Something went wrong!</em></p>
    } else if (query.IsLoading) {
        <p><em>Loading...</em></p>
    } else if (query.HasData) {
        <b>The number is @(query.Data ? "even" : "odd")</b>
    }
</UseEndpoint>

Some notes on the example above:

  • Inside the <UseEndpoint> component, you can use query to access the current state of the query. Changing the Context parameter will rename this object.
  • By changing the Arg parameter, the query will automatically be re-fetched when needed.
  • Normally, you would share endpoints around your application using dependency injection (see Defining Query Endpoints).
  • If you need to access the query state inside the @code block of a component, you can replace <UseEndpoint/> with the pattern described in Using Query Objects Directly.

Installing

You can install Phetch via the .NET CLI with the following command:

dotnet add package Phetch.Blazor

If you're using Visual Studio, you can also install via the built-in NuGet package manager.

Contributing

Any contributions are welcome, but ideally start by creating an issue.

Comparison with other libraries

  • Fluxor, Blazor-State or Cortex.Net: These are general-purpose state management libraries, so:
    • They will give you more control over exactly how your state is updated, and will work for managing non-query state.
    • However, for managing query state, you will need much more code to achieve the same things that Phetch can do in just a couple of lines. This becomes particularly important if you need to cache data or share queries across components.
  • Fusion: This is a much larger library, focused on real-time updates. Fusion is great if your app has lots of real-time functionality, but for most applications it will probably be overkill compared to Phetch.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Phetch.Core:

Package Downloads
Phetch.Blazor

A small Blazor library for handling async query state, in the style of React Query

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 434 12/1/2023
0.5.1 320 7/14/2023
0.5.0 193 6/19/2023
0.4.0 306 3/6/2023
0.3.1 407 11/19/2022
0.3.0 419 11/13/2022
0.2.0 509 8/18/2022
0.1.2 530 8/6/2022
0.1.1 523 8/1/2022
0.1.0 522 7/26/2022
0.0.3 543 7/9/2022