I started using the migration class to manage my data model (it’s great, by the way).
Is it recommended to keep the ormlite database attributes like [Index], [Required], etc only on the private classes of the migration file only and the validation attributes on the dto?
Like so:
public class Migration1000 : MigrationBase
{
private class Customer
{
[AutoIncrement]
public int Id { get; set;}
[Required]
public string Name { get; set; }
[Required]
public CustomerStatus CustomerStatus { get; set; }
}
private enum CustomerStatus
{
Active,
Disabled,
Archived,
Deleted,
Unknown
}
}
And the dto:
public class Customer
{
public int Id { get; set;}
[ValidateNotEmpty][ValidateNotNull]
public string? Name { get; set; }
[ValidateNotEmpty][ValidateNotNull]
public CustomerStatus? CustomerStatus { get; set; }
}
public enum CustomerStatus
{
Active,
Disabled,
Archived,
Deleted,
Unknown
}
[Tag("customers"), Description("Update an existing Customer")]
[Route("/customer/{Id}", "PATCH")]
[ValidateHasRole("Admin")]
public class UpdateCustomer : IPatchDb<Customer>, IReturn<IdResponse>
{
public int Id { get; set; }
public string? Name { get; set; }
public CustomerStatus? TenantPbxStatus { get; set; }
}
On the dto, do you recommend making all the properties nullable by default and add validation?
For example, if I want to use Patch with the ormlite api so that in my service I can have:
public object Patch(UpdateCustomer req)
{
var customer = req.ConvertTo<Customer>();
int count = Db.UpdateNonDefaults(customer, x => x.Id == req.Id);
//throw if nothing updated
return new CustomerResponse();
}
Since the default can be used in a patch operation for Enum and potentially other properties, I guess it’s better to make all the properties nullable on the UpdateCustomer class? Otherwise, in my example, Active is the default if the property is non nullable and patching to Active would not work. I’m not sure ConvertTo is intended to be use like this.