Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Four nullable boolean multiple choice checkboxs.

Tags:

c#

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;
            }
        }
like image 654
Nerf Avatar asked May 27 '16 11:05

Nerf


2 Answers

You could create enum and use 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 viewmodel to this enum like this :

  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;

P.S. To check for specific criterion(s) in code you can do this :


To do something for people from 24 to 35 and over 45 years old (i wouldn't say it makes much sense to me, but still)

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

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


if(criterion.Age == AgeCriterion.TwentyToThirtyFive |  criterion.Age == AgeCriterion.MoreThanFourtyFive)
like image 189
Fabjan Avatar answered Nov 03 '22 18:11

Fabjan


you can use the ?? Operator (C# Reference) to minimize your 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 it does is:
if age.LessThan24 is null than use false, if not use the value of it:

if (age.LessThan24 != null)
{
    if(age.LessThan24.Value)
    {
        // ....

    }
}
like image 22
Maximilian Ast Avatar answered Nov 03 '22 16:11

Maximilian Ast