I'm playing with LINQ-To-SQL in .NET 3.5 and have built some classes based on an underlying database. I now face the decision: do I validate data in the class code, or in the database?
I can see pros and cons either way. If I do validation in the database, the validation takes place no matter what app is using the database. (I currently don't think that any other app will ever access this database, but who knows.)
OTOH, the class provides a much more robust programming environment to do my validation. Certainly, it allows me to catch the problem while the UI is in a state where the user could correct the problem, rather than waiting until the item is submitted to the database. The Linq-to-SQL can capture datatypes for columns, but doesn't (AFAIK) provide programmatic enforcement of any CHECK or DEFAULT constraints in the underlying database.
So, for instance, a "Last Modified" property of the entity: I could update that using an ON INSERT trigger in the database, or I could do it in respone to the PropertyChanged event in the class itself (having a class subscribe to its own event sounds odd, but since the class is in two files defined as partial classes, and one of those files is autogenerated by the IDE, doing it this way means I don't have to tamper with the autogenerated file, so it won't break if I have to regenerate the .dbml), or I could do it in the app when the entity is submitted to the database.
I don't want to duplicate validation in the database and in the class. I like the robust environment of validating in the class code. But the purist in me thinks that validating in the database ensures consistency even if another app (which, again, I don't think will ever happen) uses the database.
What do others think of this topic?