# Domain Model for Simple Yes/No

Working from a C# and EntityFramework background. Jimmy Bogard makes the point of modelling your domain model correctly, such that an Email Address is not a simple string but should be of type EmailAddress.

Also known as Primitive Obsession.

I've come to this part of a piece of software where a Application form is to be filled out.

For the user a Yes/No question where they can answer Yes or No is a very easy thing, you can even create really go user experiences around yes/no.

So I have quickly thought of two ways to handle Yes/No.

A complex object:

public class YesNoAnswer
{
{
this.SetState(state);
}

public bool Yes { get; private set; }

public void SetState(string state)
{
var uppedState = state.ToUpper();

if (uppedState == "YES")
{
this.Yes = true;
return;
}
if (uppedState == "NO")
{
this.Yes = false;
return;
}

}
}


Or a simple extension method:

public static class StringExtensions
{
public static bool GetYesNoState(this string state)
{
var uppedState = state.ToUpper();

if (uppedState == "YES")
{
return true;
}
if (uppedState == "NO")
{
return false;
}

}
}


Obviously the validation should be done earlier in the pipeline (or at least it is in our application).

Which is better? They both achieve the same thing, the first is much clearer, neater. The second is well, slightly obfuscated - not a great deal but takes more than a second to think about what it is doing.

### Usage of both

var application = new ApplicationForm { IsOkayToWork = new YesNoAnswer(state) };


or

var application = new ApplicationForm { IsOkayToWork = state.GetYesNoState() };

• Why is this mutable? – CodesInChaos Feb 22 '16 at 15:20
• This code doesn't even compile. YesNoAnswer has no property/field called No and string doesn't have either Yes or No. – CodesInChaos Feb 22 '16 at 15:26
• it's more of a concept than actual working code, I only wrote that in the edit box. Why shouldn't the class be able to be changed? Are you saying just keep assigning a new value object to IsOkayToWork everytime you want assign a new value? – Callum Linington Feb 22 '16 at 16:05
• updated to compile now. – Callum Linington Feb 22 '16 at 16:07
• 1) NO should return false 2) I don't think you're allowed to edit code like that. – CodesInChaos Feb 22 '16 at 16:17

The idea of creating a Value Object instead of using primitives is to have a highly descriptive domain. Using the second option (the extension method) misses the point. You definitely want to be using the first.

e.g.

public class QuestionAnswer
{
public int QuestionId { get; set; }

public YesNoAnswer YesNoValue { get; set; }
}


Is more descriptive and specific than:

public class QuestionAnswer
{
public int QuestionId { get; set; }

public bool YesNoValue { get; set; }
}


or

public class QuestionAnswer
{
public int QuestionId { get; set; }

public string YesNoValue { get; set; }
}


One thing I would add is that you probably want a conversion to bool:

public class YesNoAnswer
{
// omitted

{
}

}


Then you can use it naturally in code:

var someAnswer = new QuestionAnswer { QuestionId = 1, YesNoValue = new YesNoAnswer("YES") }:


You should also override Equals and GetHashCode as Value Objects should have value equality not reference equality.
• Yeah that is what I was leaning towards. What property type did you have holding the yes/no value in YesNoAnswer? Was it a string of the response? I like the bool conversion, that is a nice touch – Callum Linington Feb 22 '16 at 13:12