PartialUpdater 1.0.0-beta
dotnet add package PartialUpdater --version 1.0.0-beta
NuGet\Install-Package PartialUpdater -Version 1.0.0-beta
<PackageReference Include="PartialUpdater" Version="1.0.0-beta" />
paket add PartialUpdater --version 1.0.0-beta
#r "nuget: PartialUpdater, 1.0.0-beta"
// Install PartialUpdater as a Cake Addin #addin nuget:?package=PartialUpdater&version=1.0.0-beta&prerelease // Install PartialUpdater as a Cake Tool #tool nuget:?package=PartialUpdater&version=1.0.0-beta&prerelease
Partial Updater
Partial Updater is a library for .NET developers that enables patching entities by providing the minimal JSON payload, containing only the fields that need to be updated, thus reducing the network traffic. It converts JSON payload to an expression tree, which is then used to create update function.
Installation
Install-Package PartialUpdater -Version 1.0.0-beta
Usage
Say you have some blog post entity
class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime Published { get; set; }
public DateTime LastUpdated { get; set; }
public string Content { get; set; }
public Author Author { get; set; }
}
that needs to be updated over network. Post
's Author
property is of type Author
class Author
{
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ContactInfo ContactInfo { get; set; }
}
which, among others, has a ContactInfo
property of type ContactInfo
class ContactInfo
{
public string Email { get; set; }
public string Website { get; set; }
}
Now imagine a user edits the post title and author's email in the browser (or some other remote app) and wants to save it. They could send the JSON representation of the updated post over network and then you would replace the original post with the updated one. That way, they could end up sending a large amount of unnecessary data over network (including post content which can be large) along with the post title and author's email that need to be updated. With Partial Updater, a client can send JSON that only contains the fields that need to be updated:
{
"author": {
"contactInfo": {
"email": "newemail@example.com"
}
},
"title": "New title"
}
and you can update the original entity the following way:
PartialUpdate partialUpdate = JsonConvert.DeserializeObject<PartialUpdate<Post>>(jsonString);
partialUpdate.Apply(originalPost);
where jsonString
is a variable of type string
that contains the above JSON content and originalPost
is an object of type Post
that represents the original blog post entity that will be updated.
Apply
method will only update the fields specified in the JSON payload (even if they are nested) and all the other fields in the original post will remain unchanged.
Using with ASP.NET Core
Imagine your blog from the previous example is an ASP.NET Core MVC application. In that case, all you need to do is to accept PartialUpdate<T>
type from the request body and pass your entity type as a generic parameter.
public class BlogController : Controller
{
[HttpPatch]
public void UpdatePost([FromBody] PartialUpdate<Post> partialUpdate)
{
Post originalPost = ... //retrieve the original post
partialUpdate.Apply(originalPost);
//save the post
}
}
Building from source
git clone https://github.com/MarkoPapic/PartialUpdater.git
cd PartialUpdater
dotnet restore
dotnet build ./PartialUpdater.sln
dotnet test ./PartialUpdater.Tests/
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 10.0.3)
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 |
---|---|---|
1.0.0-beta | 828 | 1/26/2018 |