I’m working on an ASP.NET MVC app, designing the domain models, using (testing) the new EF Code First feature.
I have an Activity entity that may or may not have a Deadline, what is the best way to approach it?
1 property:
public DateTime? Deadline {get; set;}
and check vs null before using
or
2 properties:
public DateTime Deadline {get; set;}
public bool HasDeadline {get; set;}
At first I thought of the first option, but then I started thinking that maybe the second option would be better regarding the DB…
Is there any best practice regarding this?
I’d go with the first option. After all, it’s exactly an encapsulated form of the second.
The encapsulation makes it clear that you’ve only got one logical value (or lack thereof). In the second form you can treat the properties as if they were entirely independent, which they’re logically not.
In terms of the database, I’d expect the first form to be just as easy too… presuambly you’ll have a nullable DATETIME field in the database, won’t you? It should map directly.