C
C#2y ago
barcode

❔ FluentValidation DTO

Hello, lets say I have a dto like this:
public uint CenterId { get; set; }

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
public uint CenterId { get; set; }

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
When the user supplies a request, if he doesn't supply center id it will be auto initialized to 0, making it impossible to detect if he even supplied that property The solution is making the property nullable and checking if .NotNull()
public uint? CenterId { get; set; }

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
public uint? CenterId { get; set; }

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
however then IDE cries for nullables so we come to this:
public uint CenterId { get; set; } = null!;

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
public uint CenterId { get; set; } = null!;

public int Page { get; set; } = 0;
public int Count { get; set; } = 10;
Is this the correct way to setup DTOs?
3 Replies
Saber
Saber2y ago
If 0 isn't a valid value for CenterId, it doesn't matter the it gets defaulted to 0, you can validate with with NotEmpty
barcode
barcodeOP2y ago
Oh I didn't know NotEmpty works on numbers, ty
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?