要用于验证的设计模式 [英] Which Design Pattern To Use For Validation

查看:134
本文介绍了要用于验证的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我不是唯一面对这个问题的人,而是到现在为止我找不到解决方案。问题如下。

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.

我已经发布了一个Web服务(我没有任何控制,我不能改变它)从遗留系统和所有的客户端都在使用它。我收到来自Web服务和对象的请求,我得到的是一个非常复杂的,但为了一个例子让我说我从Web服务调用接收对象A,其中包含其他对象,如对象B,其中的对象C等;此外物体B& C还有一些原始数据类型以及其他一些对象。我的问题是,我想验证整个对象A(包括对象和子对象),这里建议哪种设计模式?其次,这里的要点是我可以从Web服务中获取不同类型的对象A.不同类型的对象A我的意思是,对象A取决于客户端,即某些客户端将发送对象A而不在包含的对象B中填充数据,甚至可以部分填充对象B,然后将其发送到对象A所以我必须根据客户端验证对象A(因为一些客户端将需要包含对象B一些不会,有些将需要对象B中的几个元素等)。所以换句话说,我会有一个地方,我也将存储每个客户端的验证规则,这将告诉我这样的客户端ABC要求对象B中的字段abc为字符串类型,最大长度为25个字符必须在该字段中拥有数据。

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.

我想执行的某些验证是

检查字段对于数据类型的某些对象(例如对象B),特定字段的长度,是该客户端所需的字段,或者是可选的等等...

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.

此特定示例的对象A的结构如下。

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
    }

PS:我给了任意名字课程和成员只是为了一般的理解。是的,我忘了提到我在这里使用java,但并不重要,因为设计原则或模式可以应用于任何语言。希望能听到你们很快.. ..))

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

推荐答案

验证是一个十字切入的关切。有几种方法和几种设计模式可以实现。

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

在Asp.net中,它是通过属性完成的,在Java Spring中,它通过注释来保存代码清洁,可读和可维护。

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.

这篇关于要用于验证的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆