AutoPatcher 1.0.2

Suggested Alternatives

Mapster

Additional Details

Use Mapster's or AutoMapper's conditional mapping with Optionals

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package AutoPatcher --version 1.0.2
NuGet\Install-Package AutoPatcher -Version 1.0.2
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="AutoPatcher" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoPatcher --version 1.0.2
#r "nuget: AutoPatcher, 1.0.2"
#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 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

AutoPatcherLogo

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 its System.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 ifs 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 PatchModels and lists (and lists of PatchModels!).

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 Optionals on your ASP.NET Core application.

Data annotations

The default data annotations, like [StringLength(...)] or [Email], do not work for Optionals. 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.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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