-
Notifications
You must be signed in to change notification settings - Fork 1
Patchable
CloudFy edited this page Jan 15, 2025
·
3 revisions
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.
Arcturus.Patchable is a low layer artifact which allow Entity Framework Core entities, domain objects and primitives to include the patch option.
- Only support for minimal API right now.
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"
, ...
}You can easily inject the patch handler as a singleton service.
builder.Services.AddSingleton<IPatchHandler, PatchHandler>();