开发者

Validation framework

In an开发者_StackOverflow interview my friend was asked to design a validation framework, can any one give me idea how to design a efficient framework

Our approach was an interface having all method and class implementaion of all methods


Agree with Peter. Here's a nice introduction to the annotation-based approach using Spring. There's also JSR303 if you're interested.


I am not sure I understand your approach, it sounds quite vague to me.

I would go with an annotation-based approach. Different classes of validations with specific parameters can be defined in an enum, then linked to specific properties via annotations.

public enum Validation {
    NUMERIC,
    TWO_DECIMAL_PLACES,
    ...;
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
    ...
    Validation[] validation() default { };
    ...
}

...
@MyAnnotation(
        ...
        validation = { Validation.NUMERIC, Validation.TWO_DECIMAL_PLACES })
public BigDecimal getCharge() {
    return Charge;
}

At runtime, the annotations can be processed using bean introspection, with PropertyDescriptor and Method.getAnnotation().

I have recently implemented a simple version of this in our own project, but there are several robust examples around (possibly with different implementation), e.g. in Hibernate and Spring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜