Want implement a utility class which methods represent steps in a validation process. Is there a pattern or best practise for that?
-
06-07-2019 - |
Question
i want to implement a utility class which methods are internal steps of a validation process. Is there a pattern for this or should i use a totally different approach? Im open for suggestions. (I´m coding in abap but i dont think that is important)
Edit: Its no frontend validation of text, but a check if certain conditions are matched. (The parameter is actually a table. For each row i check if there are conditions matched as an example if there is a valid entry in an other db table.)
Somthing like this:
Class Validator
{
private bool flag_error;
private Step1 ( var a, var b )
{
//do somthing ...
}
private Step 2 ( var a )
{
//do somthing ...
}
private Step 3 ( var c )
{
//do somthing ...
}
static Check(var a, var b, var c)
{
Step1(a, b );
Step2( a );
Step3( c );
return flag_error;
}
}
Usage:
if (Validator.Check(a,b,c) )
{
//do good stuff
}
else
{
//do error handling
};
Solution
Design decisions really depend on the details. Will there be multiple validator algorithm implementations? Try a Strategy or Template Method pattern.
If you only need this one class performing these multiple steps, you've already implemented a pattern, Composed Method. Keep it simple. Don't add layers of complexity unless they're truly needed.
OTHER TIPS
There is one patten that comes to mind. It is the template pattern
I would make an abstract class with Check
implemented as above and the Step
methods to be implemented by the concrete classes.
I once wrote a visitor pattern, which worked quite nicely for separating the actual validation from the fields.
I use C# attributes for validation purpose. You can find a detailed article here. This helps you to implement validation by configuration over convention. Using this technique your validation routines become highly abstract and reusable.