Four flags with zero value with boolean multiple selection.

I have a problem with litle. I’m a new developer, and I have an age criteria view for my product.

These parameters are: "<24", "24-35", "35-45", "> 45".

The json I need to work looks like this:

"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 the viewmodel to the domain model. But these booleans are NULL, so they can be null, false, or true. OLSO can be multi-selective. Thus, these parameters can be "lessthan24" and "between35and45" or all of them or none.

I'm thinking of creating some kind of awful BIG IF construct. But how can I check all the parameters? Is there a good way?

I have that at the moment:

  if (age == null) return null; AgeCriterion criterion = new AgeCriterion(); if (age.LessThan24.HasValue) { if (age.LessThan24.Value) { criterion.From = 0; criterion.To = 24; } } 
+6
source share
2 answers

You can create an enumeration and use a bitmask:

 [Flags] public enum AgeCriterion { NotSpecified = 0, BelowTwentyFour = 1, TwentyToThirtyFive = 2, ThirtyToFourtyFive = 4, MoreThanFourtyFive = 8, } public class AgeCriterion { public AgeCriterion Age { get; set; } } 

And map the mapping model to this listing as follows:

  var criterion = new AgeCriterion(); if (age == null) return criterion; // fill free to return null if it better suit your needs AgeCriterion age; age = age.LessThan24 == true ? age | AgeCriterion.BelowTwentyFour : age; age = age.Between24And35 == true ? age | AgeCriterion.TwentyToThirtyFive : age; age = age.Between35And45 == true ? age | AgeCriterion.ThirtyToFourtyFive : age; age = age.MoreThan45 == true ? age | AgeCriterion.MoreThanFourtyFive : age; criterion.Age = age; return criterion; 

PS To check specific criteria in the code, you can do this:


To do something for people from 24 to 35 years old and older than 45 years (I would not say that it makes a lot of sense to me, but still)

 if(criterion.Age == AgeCriterion.TwentyToThirtyFive & criterion.Age == AgeCriterion.MoreThanFourtyFive) 

To do something for people 24 to 35 years old or over 45


 if(criterion.Age == AgeCriterion.TwentyToThirtyFive | criterion.Age == AgeCriterion.MoreThanFourtyFive) 
+5
source

can you use ?? Operator (C # Reference) to minimize if / else.

 if (age.LessThan24 ?? false) { criterion.From = 0; criterion.To = 24; } else if (age.Between24And35 ?? false) { criterion.From = 24; criterion.To = 35; } // And so on 

what does he do:
if age.LessThan24 is null , than use false if not use its value:

 if (age.LessThan24 != null) { if(age.LessThan24.Value) { // .... } } 
+3
source

All Articles