leonardoporro / detached-patchtypes Goto Github PK
View Code? Open in Web Editor NEWCreate proxy types that tracks dirty properties to simulate 'undefined' values when deserializing json.
License: MIT License
Create proxy types that tracks dirty properties to simulate 'undefined' values when deserializing json.
License: MIT License
Lots of advice is to use the NewtonsoftJson serializer in asp.net 5 projects. It would be good to support this scenario.
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...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.