Another often occurring scenario in my coding life that I've never found a solution I truly like:
Your application deals with a complex object that has to meet rigid validations to be useful. However, there are times in the application run-time where the object will validly be in an invalid state. Typically these are when the object is being constructed/edited/loaded. Sometimes, the invalid state has to persist across serialization/deserialization (E.G. User makes some of the modifications, runs out of time, and needs to save the work in progress, and later (possibly many reboots later or on another machine) load the previous work and continue modifying it.
I always struggle with balancing usability with rigid validation. On one hand, my instincts want to validate values in every property setter and never let bad data into the system. (Bad data is bad, M'Kay?) On the other, I can't make the workflow overly burdensome on the user and I have to give them the ability to work with invalid entries in the editor, and even save/load bad data, forcing me to allow bad data into the system, at least to some extent.
Example use case: I've got two projects with a certain similar feature that highlights this issue: The user gets to edit XML in a text editor, but the end-product has to fit not just the basic XML validation stuff, but some domain specific critical rules as well, and my app needs to carefully validate it. So, I'll need an object that somehow can be accessed as either a raw, unvalidated string, and as a strictly validated XML object that will throw exceptions the second an errant line of code tries to do something invalid with it.
Obviously the standard "validate in the setter" pattern won't work here, and I'm searching for the best pattern that would work here.
Your application deals with a complex object that has to meet rigid validations to be useful. However, there are times in the application run-time where the object will validly be in an invalid state. Typically these are when the object is being constructed/edited/loaded. Sometimes, the invalid state has to persist across serialization/deserialization (E.G. User makes some of the modifications, runs out of time, and needs to save the work in progress, and later (possibly many reboots later or on another machine) load the previous work and continue modifying it.
I always struggle with balancing usability with rigid validation. On one hand, my instincts want to validate values in every property setter and never let bad data into the system. (Bad data is bad, M'Kay?) On the other, I can't make the workflow overly burdensome on the user and I have to give them the ability to work with invalid entries in the editor, and even save/load bad data, forcing me to allow bad data into the system, at least to some extent.
Example use case: I've got two projects with a certain similar feature that highlights this issue: The user gets to edit XML in a text editor, but the end-product has to fit not just the basic XML validation stuff, but some domain specific critical rules as well, and my app needs to carefully validate it. So, I'll need an object that somehow can be accessed as either a raw, unvalidated string, and as a strictly validated XML object that will throw exceptions the second an errant line of code tries to do something invalid with it.
Obviously the standard "validate in the setter" pattern won't work here, and I'm searching for the best pattern that would work here.