I have my model class like below
public class MyModel
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public int Age{ get; set; }
public string AgeDisplay { get; set; } = string.Empty;
}
You can see i have added one unncessary property called AgeDisplay Somewhere in code i change AgeDisplay based on condition like
if (Age > 18)
AgeDisplay = "Adult";
else
AgeDisplay = "Child";
I bind AgeDisplay to my mudtable. I feel its wrong way to add extra property to model. What is other alternative?
This is an example of Primitive Obsession. Age is not an integer.
Int.MinimumValueis not a valid age.You can create a value type
Ageand then encapsulate all the necessary logic withinAge.Here's an example that also demonstrates some basic validation:
A second quick and dirty way is to create an extension class:
Which you can then use like this: