I’m writing a VB.NET Winforms project based on MVVM (using Winforms binding). My instinct is to never allow a domain entity be in an invalid state. This requires that I do validation checks in the constructor for new entities and in each setter for existing entities:
Public Class Product
Public Sub New(ProductID as Integer, Name as String)
If ProductID > 0 AndAlso Name.Length > 5 Then
_ProductID = ProductID
_Name = Name
Else
Throw New InvalidProductException
End If
End Sub
Private _ProductID as Integer
Public Property ProductID as Integer
Set(value as String)
If value > 0 then
_ProductID = value
Else
Throw New InvalidProductException
End If
End Set
End Property
'Same principle as above for Name setter.
End Class
Then I ran across Data Annotations, which seemed pretty slick. I noticed that most people using Data Annotations allow the domain entity to become invalid temporarily, and then validate the entity at some later point with a call to Validate.ValidateObject. By this point the entity is invalid and the original state has been lost unless you have some other mechanism to roll it back.
Two Questions:
1) Do you allow domain entities to become temporarily invalid?
2) Based on your answer to #1, what techniques do you use to validate the entity?
No, in my opinion domain entities should never be allowed to be invalid, even temporarily. The problem is that it if you allow the domain to be invalid, just like you described in your question, it gets difficult to introduce new rules as complexity grows. For example you allow entity to be invalid due to some attribute, assuming that it will be validated later. But before that happens someone adds another rule, that varies its result in accordance to the same attribute – how do you know if the rule behaves correctly? You don’t. Believe me, it happens quite often in non trivial domains.
The other reason for nor allowing state to be invalid is that in certain scenarios it can introduce problems with ORMs – I have personally seen an issue involving NHibernate cache and sub-entities that were invalid but somehow still remained in cache, I can’t recall any specific details though.
The technique I tend to use bases on validation rules and validation results. In short, most of the methods on entities is implemented in the following way (C#, if you don’t mind):
The most important thing about this code, is that certain validation rules are checked even before the entity is changed. This example shows usage of result sets, as very often I need to provide information about validation even if it succeeds (in other words, I have validations that fail and information about it has to be shown to user; however the domain entities are still valid.
The
OperationResultSetandValidatorOfare pretty simple infrastructure classes that allow adding new validators easily with fluent interface. The validators are implemented as classes implementingIValidatorinterface, which allows implementation of pretty complex validation rules and it is easier to test them individually as well.My point is, that validation should be performed before the changes to domain entities are performed – with the right convention and some infrastructure it even simplifies the code structure.
Edit note: due to some critical voices for this answer, I’ve decided to change the sample code to one that throws an Exception instead of returning results. Although I still believe that it is the way to go for my kind of scenarios, I agree that without specifying full context this might be misleading – the Exceptions should be indeed the first option and additional factors should exist to choose alternatives.