-2

So I have a POJO class let´s call it: InnerDomainObject. Then I have an object representing this object, with a few more fields, for communication towards different clients (it s an API DTO): OuterDomainObject

Because the DTO has in fact all of the POJOs fields, I made OuterDomainObject inherit from InnerDomainObject.

Now I need to somehow cast InnerDomainObject to OuterDomainObject --> not possible.

I want to avoid writing a constructor iterating through all the fields. I want to avoid writing useless code.

I just want OuterDomainObject to be created out of InnerDomainOBject´s values and then add some to it before sending it to the client.

What´s the best way of doing this ?

noneconnex
  • 29
  • 6
  • One way is to use composition rather than inheritance. – JB Nizet Jun 18 '16 at 16:57
  • Unfortunately not, JB Nizet. It is crucial for me to add additional attributes within the same levle of hierarchy. A composition would introduce an additional level of hierarchy inside the composed object: like: outerDTO.innerDTO.innerattribute and outerDTO.outerattributes - I need all the attributes flat – noneconnex Jun 18 '16 at 17:00
  • In the DTO, or in the JSON (assuming the DTO is transformed to JSON)? If you want inheritance, then you'll have to copy. – JB Nizet Jun 18 '16 at 17:01
  • Ok. Another way would be to code a utility transforming the parent to the child for those cases by using reflections. Or is there something like that out there already ? I d guess so – noneconnex Jun 18 '16 at 17:05
  • There are several bean mappers doing that sort of thing, but it makes the code slow, harder to understand and use, and only saves a few keystrokes. – JB Nizet Jun 18 '16 at 17:07

1 Answers1

0

You sound like you are using the Adapter Pattern. You shouldn't need to cast an InnerDomainObject to an OuterDomainObject. You should use composition: the OuterDomainObject should hold a reference to an InnerDomainObject, which will likely be passed into a constructor. When a client invokes a method on an OuterDomainObject, if that method exists in InnerDomainObject, the OuterDomainObject should call that method on its instance of InnerDomainObject. Instead of casting an InnerDomainObject foo to an OuterDomainObject, just create a new OuterDomainObject and pass in foo: new OuterDomainObject(foo). You will need to write some simple glue code, but the result is very clean.

Daniel O
  • 336
  • 2
  • 3
  • 8
  • Mh. What would the OuterDomainObject look like then ? Like: holding the innerdomainobject ? Would the innerdomainobject be an attribute of it ? – noneconnex Jun 18 '16 at 18:09
  • Yes, the OuterDomainObject would hold a reference to an InnerDomainObject, which would be set in OuterDomainObject's constructor. – Daniel O Jun 18 '16 at 18:13
  • Well Daniel, that was what first came to mind. But for "reasons" (this whole "problem" is actually only there because I m in a transition to unify/generify all objects in the API) I wanted it to be flat. Therefore inheritance. Anyhow, thanks for your answer - it seems it cannot be done and it seems I ll have to live with this until the API has made its transition. – noneconnex Jun 18 '16 at 18:27