Pattern for highly validated data entry and usage?

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
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.
 

Dracorat

Ars Tribunus Militum
2,798
I would still say validate in the setter, but instead of throwing an error about validation, set a multi-value property (like an Enum) to a setting indicating the highest level of validation the object succeeds at.

Then for methods which require the object - or methods that act upon higher valid states of the object, you can check the property to see if it's valid up to the required level. If not, then have a property that can be accessed for why not or have the validation methods publicly callable with returnable Exceptions.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Take it into your type system.

You have one type, which is maintains the business rules invariants, and another, which allows whatever. (I suggest making at least the former immutable)
Make it easy to attempt to convert from the anything goes one to an instance of the fixed one and vice versa (but guaranteed in that case).

If your conversion attempt function could supply an optional detailed list of the reasons for failure so much the better.

Clearly demarcate within your system whether you are working with the definitely valid one, or one in progress (this is a good thing anyway).
Pretty much job done at that point.

If you really wish some code to be able to take a read only view onto either and avoid having to duplicate things make them have a common, but read only, interface. This makes life easier anyway in enforcing the convention on names for the properties/methods/whatever.
This does however force you to use properties/methods if you're in a language where interfaces may not define fields (i.e. most of the LOB ones). I don't see that as a serious problem personally, but as we've found recently opinions on this differ.

That last bit is rather OO centric, a dynamic lanaguage could just rely on your being consistent in naming. A functional one with inlinable duck typing capabilities (like f#) would also be an option, but with strong typing.
 

wallinbl

Ars Legatus Legionis
14,335
Subscriptor
Yes, you validate in the property setter, but you don't reject the invalid value, you raise the appropriate event/set the appropriate error mechanism for your framework. In .Net, for example the framework will recognize this and feed this information back to the UI (if you have configured it).

You certainly prohibit saving when in an invalid state - it's okay in memory, but not the database.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
wallinbl":33tcpc1f said:
You certainly prohibit saving when in an invalid state - it's okay in memory, but not the database.
No, it's most certainly not okay in memory. You're breaking the contracts of the object. This is why the Transfer Object pattern exists: so that you have a known-unsafe object that cannot be unintentionally used in code that is predicated upon receiving safe objects.
 
Status
Not open for further replies.