views:

237

answers:

3

One of the tenants of DDD is to not allow your objects to enter an invalid state. To me this means there shouldn't be a public parameterless constructor because that's going to be an object in an invalid state 99% of the time.

Is this a good way to move forward? It becomes a huge PITA when you just want to new-up a class real quick.

+1  A: 

Good question. I have DDD nazi friends who say parameterless constructors are the devil. I agree with that to a certain extent, but I also feel it depends on the class's purpose.

Kilhoffer
+1  A: 

As Kilhoffer stated, it depends on what you are trying to do with the class. Under what circumstances would you want to new-up a class without actually initializing properties? If you have instance methods you wish to call, which don't require any data, you might consider either marking those methods as static or moving the methods to a separate class. It's also possible that your class has one additional valid state - totally empty.

Personally, I believe in everything in moderation. If the PITA factor is high and you are reasonably sure you aren't going to run into problems, then it seems parameterless constructors would be fine. At some point I think it becomes a matter of opinion.

Pedro
A: 

It all depends on who calls the constructor. If only your factories do, then there isn't really a problem, because your factory methods become the "domain-side" constructors and the real constructors are an implementation detail.

Wouter Lievens