Mapping a non-getter entity method to a DTO field

127 views
Skip to first unread message

Carlo

unread,
May 7, 2014, 12:18:49 PM5/7/14
to geda-generic-dto-asse...@googlegroups.com
Hello everybody :)

I have a non-getter entity method which i want to map to a DTO Field.
Example:

public Entity {

    private long id;

    public String nonGetterMethod() { ... }

    /* getters/setters*/
}

@Dto("com.example.Entity")
public Dto {

    @DtoField
    private long id;

    @DtoField("nonGetterMethod")
    private String value;
}

Unfortunately GeDA seems to only map public fields or getters. 
Is there a way to actually map regular entity methods to a DTO field?

Greetings Carlo

Denis Pavlov

unread,
May 7, 2014, 1:23:00 PM5/7/14
to geda-generic-dto-asse...@googlegroups.com
Hey Carlo,

In short - yes you can :).
Before answering to your query I would like to correct you - GeDA only works with public getters/setters (i.e. no direct field access). This is by design to promote encapsulation (no nasty peaking inside the objects behind the scenes)!

Option 1: fake getter (recommended)
So in order to have a nonGetterMethod you need:
1. Mark it as readOnly = true - that will tell GeDA not to look for setter see annotation or DSL docs
2. have a "public String getNonGetterMethod()" on your entity, so it follows the getX naming convention. But it can "get" whatever you like, so any custom logic can go in there.

Option 2: virtual field
GeDA can map virtual fields (i.e. derived or computed), which in essence really your nonGetterMethod itself. To use virtual field you need:
1. Use virtual field mapping api, see annotations or DSL docs for this
2. Have a custom implementation of ValueConverter. As this is virtual field this converter will have the whole entity as its parameter, so you can just invoke the nonGetterMethod, or better yet the implementation of your value converter.convertToDto() would be your implementation of nonGetterMethod. This way you can separate nasty logic from your domain object.

Let me know if you have further questions.
Reply all
Reply to author
Forward
0 new messages