AutoPatcher 1.0.2
Use Mapster's or AutoMapper's conditional mapping with Optionals
dotnet add package AutoPatcher --version 1.0.2
NuGet\Install-Package AutoPatcher -Version 1.0.2
<PackageReference Include="AutoPatcher" Version="1.0.2" />
paket add AutoPatcher --version 1.0.2
#r "nuget: AutoPatcher, 1.0.2"
// Install AutoPatcher as a Cake Addin
#addin nuget:?package=AutoPatcher&version=1.0.2
// Install AutoPatcher as a Cake Tool
#tool nuget:?package=AutoPatcher&version=1.0.2
AutoPatcher
Lightning fast automatic model patching using Optional
types and source generation. Compatible with System.Text.Json
.
Installation
- For the source generator, Optional types and its
System.Text.Json
converter:
dotnet add package AutoPatcher
The source generator is not included in the other packages, so if you need it, you have to install this package first.
- For ASP.NET Core:
dotnet add package AutoPatcher.AspNetCore
- Data annotations for
Optional<T>
(already included if you're using the ASP.NET Core package):
dotnet add package AutoPatcher.DataAnnotations
- For just the
Optional<T>
type and itsSystem.Text.Json
converter (already included in every other package):
dotnet add package AutoPatcher.Core
Basic usage
Given the following models
using AutoPatcher;
namespace People;
public class Person
{
public int Id { get; init; }
public string Name { get; set; }
public int Age { get; set; }
}
public class UpdatePersonModel
{
public Optional<string> Name { get; set; }
public Optional<int> Age { get; set; }
}
You can patch Person
with
var person = ...; // Get person from somewhere
var model = new UpdatePersonModel { Name = "Jake" };
// Do some manual mapping
if (model.Name.HasValue) person.Name = model.Name.Value;
if (model.Age.HasValue) person.Age = model.Age.Value;
Automatic patching
Writing a lot of if
s can be cumbersome and tricky to mantain. This is where automatic patching comes in handy.
To get started, annotate the model with optional properties with [PatchModel(typeof(...))]
where typeof(...)
points to the target type.
using AutoPatcher;
[PatchModel(typeof(Person))] // <--
public class UpdatePersonModel
{
public Optional<string> Name { get; set; }
public Optional<int> Age { get; set; }
}
AutoPatcher will then generate the following Patch
extension method for you
public static void Patch(this UpdatePersonModel model, Person target)
{
if (model.Name.HasValue) target.Name = model.Name.Value;
if (model.Age.HasValue) target.Age = model.Age.Value;
}
Now let's update our code to use our new extension method
var person = ...; // Get person from somewhere
var model = new UpdatePersonModel { Name = "Jake" };
model.Patch(person);
AutoPatcher also handles nested PatchModel
s and lists (and lists of PatchModel
s!).
Usage with ASP.NET Core
Configure AutoPatcher on your Startup
or WebApplicationBuilder
// MVC, Controllers, Razor Pages
services.AddControllers().AddAutoPatcher();
// Minimal APIs
services.AddAutoPatcherJsonOptions();
You can now use Optional
s on your ASP.NET Core application.
Data annotations
The default data annotations, like [StringLength(...)]
or [Email]
, do not work for Optional
s.
Don't worry though! AutoPatcher.DataAnnotations
adds Optional
-friendly alternatives for the annotations you know and love.
using AutoPatcher;
using AutoPatcher.DataAnnotations;
[PatchModel(typeof(Person))]
public class UpdatePersonModel
{
[OptionalStringLength(250)]
public Optional<string> Name { get; set; }
[Range(18, 200)]
public Optional<int> Age { get; set; }
}
A note on Source generators
Source generators can be tricky sometimes, in case your IDE is not picking up a generated Patch
method, a quick IDE restart (or refresh on VS Code) should do the trick.
Usually, even if you're getting squigglies, dotnet build
or your IDE's Run
command should still work normally.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- AutoPatcher.Core (>= 1.0.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|
Fix: Added null check before assigning list elements