Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Design Pattern To Use For Validation [closed]

I am sure I am not the only one who's facing this problem but I am unable to find a solution till now. The problem is as below.

I have published a web service (on which I don't have any control and I cannot change it) from a legacy system and all the clients are using it. I receive requests from the web service and object which i get is a very complex one but for the sake of an example lets say I receive object A from the web service call which contains several other objects like Object B, Object C in it etc and in addition objects B & C also have some primitive data types as well as some other objects in them. My problem is that I want to validate the whole Object A (all the including objects and sub objects), which design pattern is recommended here? Secondly, the main point here is that I can get different types of Object A from the web service. What i really mean by different types of object A is that the Object A depends upon the client i.e. some clients will send Object A without filling the data in the containing Object B or even they can fill Object B partially and then send it in Object A. So I have to validate the Object A based on the client (as some clients will require containing Object B some will not, some will require few elements in Object B etc). So in other words I will have a place where I will also store the validation rules for each client which will tell me something like this that client ABC wants field abc in Object B to be of type string and the maximum length is 25 chars and it is mandatory to have data in that field.

Some validations which I would like to perform is

Checking fields of some object (say Object B) for data types, length of a particular field, is the field required for this client or is it optional etc etc...

Any concrete working example will be extremely useful.

The structure of the Object A for this particular example is as follows.

    public class A
    {
        private B objectB;
        private C objectC;
        // and so on
    }

    public class B{
        private E objectE;
        private String name;
        private int age;
        // and so on
    } 

    public class C
    {
        private F objectF;
        private String address;
        private String country;
    }

    public class E
    {
        // Members here
    }

    public class F
    {
        // Members here
    }

P.S: I have given arbitrary names to the classes and the members just for the general understanding. O yes I forgot to mention that I am using java here but it doesn't really matter as the design principles or patterns can be applied to any language. Hoping to hear from you guys soon.. :)

like image 693
Sam ツ Avatar asked Aug 19 '13 13:08

Sam ツ


1 Answers

Validation is a Cross Cutting Concern. There are several ways and several design patterns can be implemented.

In Asp.net it is being done via attributes, in Java Spring it is done via Annotations to keep the code clean, readable and maintainable.

You can find tons of different approaches, what you need to keep in mind is the approach of these frameworks follow. ie code maintenance, readability and clean code.

There is no silver bullet. You can even write validation in your code.

like image 72
DarthVader Avatar answered Oct 30 '22 04:10

DarthVader