开发者

Automatically update/copy/inherit DTO javadocs from entity class

In our system we fetch ORM managed entity beans from the databas开发者_如何学运维e (using hibernate) and then we copy them onto DTO beans to send down to the UI.

Most of the properties on the DTO have the same name as on the entity, but the return types are often different since any related entities referenced have to be converted to store only the ID or another DTO.

In my ideal dream world somehow when I edited the javadocs for the entity, the javadocs on same-named methods in the DTO would be updated to match. This could be done via build tool or eclipse plugin.

Has anyone seen anything like this out there?


Well it's definitely possible. What you need is a source code parser, I'd recommend javaparser.

Write two Visitors, one to read the JavaDocs, the other to write them. In both, you will probably start with VoidVisitorAdapter and overrride public void visit(MethodDeclaration n, A arg) and public void visit(JavadocComment n, A arg)

Make all of this accessible from a main class and call that main class via maven (Exec-Maven-Plugin) or ant (Java Task) during build.


I think what you want may be unreasonable.

A DTO is not supposed to be mapped to an entity. If all the same, why do you need a DTO?

An entity's name is generally mapped to a table name and the fields are mapped to a column. If you got the entity's field name, then you can easily access the table object in an ORM system. This is not secure.

In addition, a DTO is supposed to be flexible to adapt to the UI needs and other layer's needs. So it's about data transfer, transformation, combination, less fields than an entity and more.

E.g. you can combine 4 entities or the view's data in one DTO for a remote web service call. Because of the coarse grain processing, this is a performance concern.

In conclusion, if it is an enterprise level Java EE application, then a DTO is very necessary. Don't copy the name or have a very dependent entity, but derive from the entity.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜