17

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.. :)

Sam ツ
  • 583
  • 2
  • 7
  • 17
  • 1
    Validation can be done in numerous ways and is very much opinion based. Have a look at the [Strategy pattern](http://en.wikipedia.org/wiki/Strategy_pattern). – Bart Aug 19 '13 at 15:09
  • Can you recommend any alternative other than Strategy Pattern? – Sam ツ Aug 20 '13 at 07:31
  • 1
    I suggest you to use combination of Visitor and Factory/AbstractFactory. Factory can create any validator classes by client type with special rules. Visitor car validate entire structure using validator class – Glauco Cucchiar May 12 '16 at 13:56

2 Answers2

7

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.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • You have mentioned a very nice point here (which i totally forgot) that validation is a cross cutting concern. Thanks for that. If i write the validation code in my business objects or any object, won't it will clutter the code? I think it will violate the single responsibility principle, what do you say? – Sam ツ Aug 20 '13 at 06:20
  • 1
    yeah, it does and that s why validation is a cross cutting concern, just like logging, sometimes, you cant avoid it. what you need to pay attention is correctness of your code, clean, readable, non-repetetive, and maintainable. – DarthVader Aug 20 '13 at 06:28
  • Can you suggest me a design pattern other than strategy pattern? I am trying my best to keep my code clean, readable, maintainable and I am following the DRY principle as well but I think i have reached a point where i have to get my hands dirty.. :) – Sam ツ Aug 20 '13 at 07:27
  • 3
    You can use decorator pattern. Asp.net and java frameworks use decorator pattern for validation. – DarthVader Aug 20 '13 at 11:12
0

Each class should know how to validate itself. You can derive each class from an interface (say IValidatable) that has a .Validate() method. When you call .Validate() on object A, have it validate itself, and then call .Validate() on all children. Those children can validate themselves in a similar fashion.

I think this is a really simple version of the command pattern. Sort of.

You could also compose a Validator that you can attach to your class. The validator can know how to validate email addresses, regular addressess etc. This is a bit more extensible as you are basically creating a toolbox to attach to your class that only contains the tools it needs.

Ryan Bennett
  • 3,404
  • 19
  • 32
  • 3
    Yes this is initially what i thought of that each class will be responsible for its own validation but i think by doing this single responsibility principle will be violated and in addition the actual code can be cluttered with the business code. Any thoughts on this? – Sam ツ Aug 20 '13 at 06:22
  • Short answer: it depends. If you used Domain Driven Design, you could validate all the properties on the setters - that would certainly be a safe way to do it if you are worried about single responsiblity. I think by using the Validator strategy scenario, you are still safe. The validator has the responsibility for validating. it just happens to be referenced by the object it validates. – Ryan Bennett Aug 20 '13 at 13:54