I'm building a C# WPF application using the MVVM pattern. I have repository classes that use NHibernate to persist my domain model.
My model consists of a larger tree structure (Recipe
s containing Operation
s containing Phase
s). Operations and phases both contain a dynamic list of key-value-mappings as an IDictionary<string, string>
. The corresponding NHibernate mapping for an Operation
is
<class name="Operation" table="operations">
<id column="id" type="int" generator="native" />
<property name="Name" column="name" />
<map name="Parameters" table="operation_params">
<key column="operation" />
<index column="param" type="string" />
<element column="value" type="string" />
</map>
<list name="Phases" cascade="all-delete-orphan">
<key column="operation" />
<index column="`index`" />
<one-to-many class="Phase" />
</list>
</class>
Now, that part is easy and works quite well. The Operation
class currently is a POCO with nearly no logic inside, a simple data container.
My problem is: I have to validate the parameters against an external schema my application reads from an .xml file. The schema contains restrictions for single parameters (range, valid values etc.) as well as dependencies between several parameters (i.e. the valid values vary depending on another parameter's value).
What is the best way to integrate my validation logic? I read a lot the last few days and until now, I stumbled upon the following alternatives:
Add the validation logic to the model class itself.
For this, I don't know how to properly inject the validation schema into the objects created by NHibernate. I don't need the validation functionality all the time, only when the user is editing the parameters or when I'm importing an operation (e.g. from a backup). So maybe I could implement the actual validation logic in the model class and inject the validation rules using a property whenever I really need validation? Is it considered good practice to add that functionality to the model classes that I store using NHibernate or should the model classes stay 'dumb'?
Create a decorator class for the validation logic that wraps around my
Operation
objects.This way I would use the wrapper everytime I need validation and the bare model class when I only need to display it. My problem here is that my ViewModel classes already are wrappers, so I would get another layer of wrapping here. Also, as the Operation class is part of a larger tree structure (Recipe/Operation/Phase) I would need to create wrappers for the collections and map collection changes back to the underlying collections which may be a complex task.
Create a service class that I call passing the operation whenever I want to validate it.
The problem that I see here is that the service is stateless and would therefore have to re-validate the whole parameter list everytime the user changes a single parameter. This doesn't seem to be the best approach, especially when I want to fire some kind of a change event for the UI when the validation status for a parameter changes.
What is the common approach for my problem? Is there a pattern I haven't found yet that is perfectly what I need? I mean, there are a lot of implementations out there that rely on external schema definitions for validation (read: XML/XSD and similar document structures), there just have to be some geniuses who already found the perfect solution for my problem ;-) Help me, SO!