I have litle problem. I'm a new dev and I have view with "Age criterion" for my product.
These options are: "< 24", "24 - 35", "35 - 45", "> 45".
A json which I need to work looks that:
"age": {
"lessThan24": true,
"between24And35": true,
"between35And45": true,
"moreThan45": true
}
My View model:
public class AgeCriterionViewModel
{
public bool? LessThan24 { get; set; }
public bool? Between24And35 { get; set; }
public bool? Between35And45 { get; set; }
public bool? MoreThan45 { get; set; }
}
And my domain model:
public class AgeCriterion
{
public int? From { get; set; }
public int? To { get; set; }
}
So here is my problem. I need to map viewmodel to domain model. But these booleans are nullable so they can be null, false or true. OLSO it can be multiselect. So these options may be "lessthan24" and "between35and45" or all of them or none.
I thinking about build some terrible BIG IF construction. But how can i check all options? Is there any nice way?
I have that at this moment:
if (age == null)
return null;
AgeCriterion criterion = new AgeCriterion();
if (age.LessThan24.HasValue)
{
if (age.LessThan24.Value)
{
criterion.From = 0;
criterion.To = 24;
}
}