Tutorial

10.2 The supplier

The supplier delivers stock for the products it is asked about. Ours is a fake that a developer can take down and bring back.

Suppliers/SupplierUnavailableException.cs:

namespace Shopfront.Suppliers;
 
public class SupplierUnavailableException(string message) : Exception(message);

Suppliers/ISupplier.cs:

namespace Shopfront.Suppliers;
 
public interface ISupplier
{
    // Delivers what the supplier can of each SKU: a list of (SKU, units) actually sent.
    Task<IReadOnlyList<(string Sku, int Units)>> Deliver(
        IReadOnlyList<string> skus,
        CancellationToken cancellationToken
    );
}

Suppliers/FakeSupplier.cs:

namespace Shopfront.Suppliers;
 
// Sends a carton of five of anything asked for, unless it has been taken down.
public class FakeSupplier : ISupplier
{
    public const int Carton = 5;
 
    public bool IsDown { get; set; }
 
    public Task<IReadOnlyList<(string Sku, int Units)>> Deliver(
        IReadOnlyList<string> skus,
        CancellationToken cancellationToken
    )
    {
        if (IsDown)
            throw new SupplierUnavailableException("The supplier's feed did not answer.");
        return Task.FromResult<IReadOnlyList<(string Sku, int Units)>>(
            [.. skus.Select(sku => (sku, Carton))]
        );
    }
}

SDK Reference

Schedule | AddScheduler | How retries work | Resolve a dead letter | AddLifecycleHook


Next: 10.3 The restock