One BO, two different propertynames, one validation value

One BO, two different propertynames, one validation value

Old forum URL: forums.lhotka.net/forums/t/2075.aspx


cultofluna posted on Wednesday, January 03, 2007

Hello,

In the book a custom validation method is used for the comparison of the start and end date comparison. The validation function uses templating based on an interface, where in the definition of the interface the propertynames of the BO are used.

But what if I have custom validation that should be used by two properties in one BO that have different names?

I prefer of course to have only one validation function, one interface and two properties, but in my opinion the following problems occur:

* the interface must have two different propertynames, else it cannot handle the two properties.

* the function can only handle one interface using templating, so it must always validate both properties, because I don't want to call it twice

* the function cannot be dynamic,

* etc.

I can't seem to get a grip one centralizing and using the validation rules for use in multiple objects and being able to use validation information in the GUI.

Any thoughts?


RockfordLhotka replied on Wednesday, January 03, 2007

Your options are really limited. The question is how to write a method that performs an action against a property when the name of the property isn't known until runtime. The obvious solution is reflection, but there are two others (at least):

  1. Use reflection like I do in CommonRules
  2. Use a Select/switch statement based on the property name. At a minimum in the switch statement you'd get the property value from the appropriate property/field and put it in a temp variable so the rest of the actual logic could run against that temp value
  3. Create three methods: A' and A'' that are property-name specific and which delegate to B which is the actual validation logic - B would accept the property value as a parameter, and A' and A'' would get the value from the correct property/field to pass to B

There may be other solutions, but these are the three that seem clear to me. Of these options perhaps #2 is the easiest to implement, though not necessarily the easiest to maintain.

Copyright (c) Marimer LLC