Assume you have the following model with navigation properties for EF Core
public class Person
{
public Guid personId { get; set; }
public Guid departmentId { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public int Age { get; set; }
public double Salary { get; set; }
public virtual Department Department { get; set; }
}
Now I would for example create the following DTO to send and receive data from a client app that contains the following
public class Person
{
public string personId { get; set; }
public string departmentId { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string Age { get; set; }
public string Salary { get; set; }
}
I would do it this way (i.e making all properties strings) for the following reasons:
It would cause my application to throw an exception if one of the properties has a wrong type (i.e the client provided the
personID
as a string instead ofGuid
) and there is no way to catch this exception, even with an exception middleware (at least I haven't found a way to do that yet). I know ofUseStatusCodePages
to catch errors so maybe I can use it to catch error code 500(assuming that is what the application would throw) and return an appropriate response to the client?Following the first reason, not throwing an exception would allow me to validate the properties using Fluent Validation.
Is this a bad practice or anti-pattern? And if you have a solution to how to catch the exception I mentioned I would really appreciate it.