Skip to content

Patchable

CloudFy edited this page Jan 15, 2025 · 3 revisions

About

Arcturus Patchable is a lightweight partial update model. Patchable holds a reference to the target type, and a dictionary of string-value properties. When used via ASP.NET Core, the dictionary is populated with the body properties.

Call .UsePatchRequestValidation() to validate the body against the target type. A HTTP 400 (bad request) is returned if properties are found which does not match, or null-values are provided for properties that does not support null.

Lightweight

Arcturus.Patchable is a low layer artifact which allow Entity Framework Core entities, domain objects and primitives to include the patch option.

FAQ

  • Only support for minimal API right now.

Usage

Use Arcturus.Extensions.Patchable.AspNetCore.

using Arcturus.Extensions.Patchable.AspNetCore;

// minimal api
app.MapPatch("/sample", (PatchRequest<SampleItem> patched, BusinessLogic businessLogic) =>
{
    return Results.Ok(
        businessLogic.UpdateSampleData(patched));
}).UsePatchRequestValidation();

...

public class BusinessLogic
{
    public SampleData UpdateSampleData(Patchable<SampleItem> patchedItem)
    {
        SampleData data = new ..;

        var o = new PatchHandleOptions
        {
            OnValidateProperty = (p, d) => p.Name != "Key"
        };

        var patchHandler = new PatchHandler();
        return patchHandler .Apply(patchedItem, data, o);
    }
}

Send a patch request

HTTP PATCH /sample

{
"name" : "Some name"
, ...
}

Dependency injection

You can easily inject the patch handler as a singleton service.

builder.Services.AddSingleton<IPatchHandler, PatchHandler>();

Implementations

Clone this wiki locally