Skip to content
jayoungers edited this page Oct 18, 2018 · 3 revisions

PatchMap is a .Net library intended to simplify the process of creating and updating entities in your REST based applications.

After defining a map for an entity (a Blog, for example), you can Insert, Update, and Patch that entity using the same code:

[RoutePrefix("api/blogs")]
public class BlogsController : BaseController
{
    public BlogsController(ExampleContext dbContext) : base(dbContext) { }

    [HttpPost, Route("")]
    public PatchCommandResult<BlogViewModel> Insert(BlogViewModel blog)
    {
        return new BlogPatchCommand(DbContext).Execute(null, blog.ToPatchOperations());
    }

    [HttpPut, Route("{id}")]
    public PatchCommandResult<BlogViewModel> Update(int id, BlogViewModel blog)
    {
        return new BlogPatchCommand(DbContext).Execute(id, blog.ToPatchOperations());
    }

    [HttpPatch, Route("{id}")]
    public PatchCommandResult<BlogViewModel> Patch(int id, List<JsonPatch> patches)
    {
        return new BlogPatchCommand(DbContext).Execute(id, patches.ToPatchOperations<BlogViewModel>());
    }
}

The library consists of three parts:

  1. A method to convert HTTP Patch messages into a list of PatchOperation.
  2. A method to convert any object into a list of PatchOperation.
  3. A Mapper, which can take some sort of Entity and a list of PatchOperation, and given a set of defined Maps, apply the PatchOperations to the target Entity.
Clone this wiki locally