{"id":23899358,"url":"https://github.com/leonardoporro/detached-patchtypes","last_synced_at":"2025-04-10T18:12:00.680Z","repository":{"id":65365284,"uuid":"348440101","full_name":"leonardoporro/Detached-PatchTypes","owner":"leonardoporro","description":"Create proxy types that tracks dirty properties to simulate 'undefined' values when deserializing json.","archived":false,"fork":false,"pushed_at":"2024-01-22T04:17:30.000Z","size":165,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-29T15:29:34.509Z","etag":null,"topics":["json","mappers","null","partial-updates","patch","primitives","proxy-types","serializer","undefined"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonardoporro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-16T17:44:43.000Z","updated_at":"2024-05-07T13:05:11.000Z","dependencies_parsed_at":"2025-01-04T18:18:55.164Z","dependency_job_id":"1b4eca45-070d-49c1-adff-9d2e052c0e64","html_url":"https://github.com/leonardoporro/Detached-PatchTypes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardoporro%2FDetached-PatchTypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardoporro%2FDetached-PatchTypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardoporro%2FDetached-PatchTypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardoporro%2FDetached-PatchTypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonardoporro","download_url":"https://codeload.github.com/leonardoporro/Detached-PatchTypes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248268949,"owners_count":21075702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","mappers","null","partial-updates","patch","primitives","proxy-types","serializer","undefined"],"created_at":"2025-01-04T18:18:50.018Z","updated_at":"2025-04-10T18:12:00.663Z","avatar_url":"https://github.com/leonardoporro.png","language":"C#","readme":"![Detached Banner](banner.png?raw=true)\n\n# Patch Types\n### What is it\nAllows 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.\nIt's a part of [Detached.Mappers](https://github.com/leonardoporro/Detached-Mapper) library.\n\n### What does it solve\nUnlike JavaScript, C# does not provide the 'undefined' value to check what properties were set during the deserialization of a \nrequest or file.\nThis library creates proxy types that internally keep tracking of the dirty state of each property.\nDetached.Mappers uses IPatch types to select which properties to copy when working with ORMs.\n\n### How it works\nA manual call to PatchTypeFactory.Create() can be made to create/retrieve a proxy type for a given type.\n\n```csharp\n// create a patch type for Entity, implementing IPatch\nEntity entity = PatchTypeFactory.Create\u003cEntity\u003e();\n// set any property value \nentity.Name = \"newName\";\n// access to IPatch members\nIPatch entityChanges = (IPatch)entity;\n// check for the property status\nAssert.True(entityChanges.IsSet(\"Name\"));\n```\n\nSystem.Text.Json serializer can be configured to deserialize DTOs to patch types automatically.\n\n```csharp\nJsonSerializerOptions jsonOptions = new JsonSerializerOptions();\n// configure the serializer\njsonOptions.Converters.Add(new PatchJsonConverterFactory());\njsonOptions.IgnoreReadOnlyProperties = true;\n\nstring json = @\"\n    {\n        'Id': 1,\n        'Name': 'test name'\n    }\".Replace(\"'\", \"\\\"\");\n            \n// deserialize some entity\nEntity entity = JsonSerializer.Deserialize\u003cEntity\u003e(json, jsonOptions);\n\n// check for undefined properties\nIPatch changeTracking = (IPatch)entity;\n\nAssert.True(changeTracking.IsSet(\"Name\"));\nAssert.True(changeTracking.IsSet(\"Id\"));\nAssert.False(changeTracking.IsSet(\"Date\"));\n```\n\n#### Note: As in any other proxy tool, properties must be marked as virtual in order to override them with the proper dirty tracking code.\n\n## Configure\nPatchJsonConverterFactory takes a IPachTypeInfoProvider as a parameter. This class has the responsability of determine if the type should be \npatched or not.\nDefault is DefaultPatchTypeInfoProvider, that will patch everything but primitives in .Primitives property.\nAnnotationPatchTypeInfoProvider checks for [UsePach] attribute before allow the patch creation.\n\n## Integrate to MVC\nIn order to get Patch Types in the bodies of Post methods, in Startup.cs, add the converter to main serializer like this: \nAnnotationPatchTypeInfoProvider is the preferred method, as you may not need all types to be proxied.\nYou 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\nwill occur.\n\n```charp\nservices.AddControllers().AddJsonOptions(j =\u003e\n{\n    j.JsonSerializerOptions.Converters.Add(new PatchJsonConverterFactory(new AnnotationPatchTypeInfoProvider()));\n});\n```\nThen add the [UsePach] attribute to your model:\n \n```csharp\n [UsePatch]\n    public class SampleModel\n    {\n        public virtual int Id { get; set; } // don't forget to add virtual, otherwise, patch factory won't be able to override.\n\n        public virtual string Name { get; set; }\n\n        public virtual DateTime? DateTime { get; set; }\n    }\n```\n\nWrite your controller and enjoy!\n\n```csharp\n[HttpPost]\npublic IActionResult PostPatcheableModel([FromBody] SampleModel model)\n{\n    // at this point, model is a proxy that inherits SampleModel and implements IPach for other libs like Detached.Mappers\n    // (or your library!) that need to check property status.\n\n    // just some code to print the status of the properties\n    IPatch patch = (IPatch)model;\n\n    // use patch.IsSet(propName) to check the status of the properties. Or install Detached.Mappers.EntityFramework to map directly to EF Core.\n}\n```\n\n*Check Sample folder for a working example!*\n\n#### Any help on debugging, or adding new features is very welcome!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardoporro%2Fdetached-patchtypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonardoporro%2Fdetached-patchtypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardoporro%2Fdetached-patchtypes/lists"}