Assuming I wan't to use single class to create and update an object in REST API - how I can do this without code duplication?
Create an object (POST) - all fields should be [Required]
.
Update an object (PATCH) - I want to use same class but fields should not be required as it can be done partially.
I can use below class for POST but can't for PATCH - as only Name
may be updated (provided along the request) - which is OK.
public class Person
{
[Required]
public string Name { get; set; }
[Required]
public string LastName { get; set; }
}
The only solution I see (that causes code duplication) is:
public class CreatePerson
{
[Required]
public string Name { get; set; }
[Required]
public string LastName { get; set; }
}
public class UpdatePerson
{
public string Name { get; set; }
public string LastName { get; set; }
}