I would like to extract DTO from my application to provided them as jar to tiers applications.

But I used Bean Validation, so DTOs are annotated with custom constraints. And this custom annotations have dependency (link) on validation implementation.

@AuthorisedUser
public class UserDTO implements Serializable {
    // ...
}

@Constraint(validatedBy = { AuthorisedUserValidator.class })
public @interface AuthorisedUser { ... }

Thus, my DTO module depends annotations, which depends on Validator, which depends on DAO and then the full core application comes.

Is there a way to break this dependence cycle? What's the good practices to provide DTO jar without dependencies (or only to bean validation API)?

Thanks

有帮助吗?

解决方案

  • Remove the constraints annotations and create your plain DTO jar
  • Bundle your custom constraints and implementations into a separate jar
  • In the service layer (or where ever you want to have the validation), add Bean Validation, Hibernate Validator, you custom constraints and use XML to configure the constraints

If your biggest concern is breaking the cycle, your best bet is using XML configuration.

其他提示

General Rule of dependencies should be like this. Domain objects and DTOs should not depend on any other modules. Then DAOs come. They should depend only on the domain objects/DTOs. Then comes Service layer, which depends on on the DAO layer. This is where you write your DB related validations. Now UI layer uses Service API, On UI layer you usually run the UI validations.

In your specific case you AuthorisedUserValidator class should validate only DTO classe.

You just can provide a JAR with your DTOs without providing the Bean Validation JAR or a JAR containing your custom constraint annotations. The DTO classes can still be loaded and used also if they are annotated with annotations whose type is not present (of course you can't make use of Bean Validation in this case).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top