What is better design / practice: Nullable property or 1 property value and 1 bool "has" property?

I am working on an ASP.NET MVC application developing domain models using (testing) the new EF Code First feature.

I have an Activity object 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 about the first option, but then I started to think that maybe the second option would be better with respect to the database ...

Are there any recommendations on this?

+6
c # asp.net-mvc data-modeling code-first
source share
5 answers

I would choose the first option. After all, this is a finely encapsulated form of the second.

Encapsulation makes it clear that you have only one logical value (or lack thereof). In the second form, you can consider properties as if they were completely independent, but logically they are not.

In terms of the database, I would expect the first form to be just as simple ... Presumably, you will have a zero DATETIME field in the database, right? It should be displayed directly.

+10
source share

How about a combination of both just to make your code more readable?

 public DateTime? Dealine{get; set;} public bool HasDeadline { get { return (Deadline != null); } } 

Easy to read and does the same as the consumer. Besides,...

 if(HasDeadline) doStuff(); 

easier to read than

 if(Dealine != null) doStuff(); 

:)

+3
source share

I would use the first option. Ultimately, the second option is likely to cause some maintenance problems, because you should remember to check and use both properties.

Also, one option is to use a single property, but instead of making it null, you can return a Null object (also known as Special Case ).

+1
source share

The database is used to store NULL values ​​- store the Min value in the databsae, and then have a flag to indicate whether you should trust this value, which makes queries difficult.

I like types with a null value, because they reflect the intent of the domain - there is no date, not "no date", so pretend that the first of January 1970 means "no date".

There is also the overhead of storing the HasDealine value - you need to set it every time the corresponding property is updated. Also how do you clean it? If you set Deadline to a date, it will set HasDeadline to true. How can I turn it off? Would you set HasDeadline to false, but leave the Deadline field unchanged with the previous value?

Common icky.

+1
source share

You should use a nullable value, as it does exactly what you want. Using two separate properties means that you are losing the connection between them, and you need to explain with the documentation that they have a relationship.

A type with a null value should also be better suited to the type of database, however you must first design your object for how it works as an object, and not how to save it in the database. If using the database generation tool leads to erroneous decisions in the development of the code, it is consistent.

+1
source share

All Articles