Coder Social home page Coder Social logo

leonardoporro / detached-patchtypes Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 1.0 165 KB

Create proxy types that tracks dirty properties to simulate 'undefined' values when deserializing json.

License: MIT License

C# 100.00%
partial-updates patch json undefined null proxy-types serializer mappers primitives

detached-patchtypes's Introduction

Detached Banner

Patch Types

What is it

Allows to create a proxy type for a given DTO that implements IPatch interface and allows to check for dirty (or set) properies. It also provides a JsonConverter that handles IPatch types. It's a part of Detached.Mappers library.

What does it solve

Unlike JavaScript, C# does not provide the 'undefined' value to check what properties were set during the deserialization of a request or file. This library creates proxy types that internally keep tracking of the dirty state of each property. Detached.Mappers uses IPatch types to select which properties to copy when working with ORMs.

How it works

A manual call to PatchTypeFactory.Create() can be made to create/retrieve a proxy type for a given type.

// create a patch type for Entity, implementing IPatch
Entity entity = PatchTypeFactory.Create<Entity>();
// set any property value 
entity.Name = "newName";
// access to IPatch members
IPatch entityChanges = (IPatch)entity;
// check for the property status
Assert.True(entityChanges.IsSet("Name"));

System.Text.Json serializer can be configured to deserialize DTOs to patch types automatically.

JsonSerializerOptions jsonOptions = new JsonSerializerOptions();
// configure the serializer
jsonOptions.Converters.Add(new PatchJsonConverterFactory());
jsonOptions.IgnoreReadOnlyProperties = true;

string json = @"
    {
        'Id': 1,
        'Name': 'test name'
    }".Replace("'", "\"");
            
// deserialize some entity
Entity entity = JsonSerializer.Deserialize<Entity>(json, jsonOptions);

// check for undefined properties
IPatch changeTracking = (IPatch)entity;

Assert.True(changeTracking.IsSet("Name"));
Assert.True(changeTracking.IsSet("Id"));
Assert.False(changeTracking.IsSet("Date"));

Note: As in any other proxy tool, properties must be marked as virtual in order to override them with the proper dirty tracking code.

Configure

PatchJsonConverterFactory takes a IPachTypeInfoProvider as a parameter. This class has the responsability of determine if the type should be patched or not. Default is DefaultPatchTypeInfoProvider, that will patch everything but primitives in .Primitives property. AnnotationPatchTypeInfoProvider checks for [UsePach] attribute before allow the patch creation.

Integrate to MVC

In order to get Patch Types in the bodies of Post methods, in Startup.cs, add the converter to main serializer like this: AnnotationPatchTypeInfoProvider is the preferred method, as you may not need all types to be proxied. You can write your own, just don't forget to check if the type you are patching is not already a patch type, otherwise a stack overflow will occur.

services.AddControllers().AddJsonOptions(j =>
{
    j.JsonSerializerOptions.Converters.Add(new PatchJsonConverterFactory(new AnnotationPatchTypeInfoProvider()));
});

Then add the [UsePach] attribute to your model:

 [UsePatch]
    public class SampleModel
    {
        public virtual int Id { get; set; } // don't forget to add virtual, otherwise, patch factory won't be able to override.

        public virtual string Name { get; set; }

        public virtual DateTime? DateTime { get; set; }
    }

Write your controller and enjoy!

[HttpPost]
public IActionResult PostPatcheableModel([FromBody] SampleModel model)
{
    // at this point, model is a proxy that inherits SampleModel and implements IPach for other libs like Detached.Mappers
    // (or your library!) that need to check property status.

    // just some code to print the status of the properties
    IPatch patch = (IPatch)model;

    // use patch.IsSet(propName) to check the status of the properties. Or install Detached.Mappers.EntityFramework to map directly to EF Core.
}

Check Sample folder for a working example!

Any help on debugging, or adding new features is very welcome!

detached-patchtypes's People

Contributors

leonardoporro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bubdm

detached-patchtypes's Issues

I need some help with mapping related entities

For now my test action in abstract crud controller be like:

        [EnableQuery]
        [HttpPatch("/odata/[controller]({id:int})")]
        public virtual async Task<ActionResult<TEntity>> Patch(int id, [FromBody]TEntity delta, CancellationToken ctx)
        {
            var entity = PatchTypeFactory.Create<TEntity>();
            var type = typeof(TEntity);
            
            foreach (var property in type.GetProperties())
            {
                property.SetValue(entity, property.GetValue(delta));
            }
            entity.Id = id;
            await _db.MapAsync<TEntity>(entity);
            await _db.SaveChangesAsync(ctx);
            return Updated(entity);
        }

and entities are:

    public class User : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
        public virtual int? RoleId { get; set; }
        [Aggregation]
        public virtual Role? Role { get; set; }
    }

    public class Role : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        [Composition]
        public virtual List<User>? Users { get; set; }
        [Composition]
        public virtual List<Permission>? Permissions { get; set; }
    }

    public class Permission : IHasId
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        [Composition]
        public virtual List<Role>? Roles { get; set; }
    }

and when I send this request:

PATCH https://localhost:5001/odata/users(8)
Accept: application/json
OData-Version: 4.0
Content-Type: application/json;odata.metadata=full;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{
  "RoleId": 8,
  "Role": {
    "Id": 8,
    "Name": "RENAMED Role 8",
    "Permissions": [
      {
        "Id": 14,
        "Name": "RENAMED Permission 14"
      },
      {
        "Id": 15,
        "Name": "NEW Permission 15"
      }
    ]
  }
}

(and state of records in db: user exists without any role, role exists without any permissions, first permission exist and second permission not)

I'm expecting, that user #8 be related to role #8, role #8 be related to existed permission #14 and permission #15 will be created and related to role #8.

But for now only user #8 related to role #8...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.