Why do we need a bean to bean mapper like dozer in a web application
Asked Answered
S

3

6

In simple terms, why do we need 'a bean to bean mapping service' (like Dozer) in a web-application.

Suppose I'm working on a web-service.

  1. I'm receiving an XML in request.
  2. I fetch the the values from XML elements.
  3. Perform the required operation on the fetched values.
  4. Prepare the response XML.
  5. Send the response XML as response

Why should I add one more steps of mapping XML elements to own custom elements.

I'm not able to convince myself, probably because I'm not able to think of a better situation/reason.

Please suggest, with example if possible.

Sellma answered 29/8, 2014 at 7:9 Comment(3)
If you don't see a use for it, you don't need it.Paramorphism
@FlorentBayle, it is being used in a very similar project.Sellma
This doesn't mean that you need it. It could be used for historical reasons, for bad reasons, to answer a need you will not face,... You have to ask the other project why this choice was made.Paramorphism
H
4

It helps to reduce coupling between the presentation (i.e. the XML schema) and the business logic. For example in case of schema changes you don't have to touch the business logic, just the mapping between the objects.

In simple cases it might not be worth the additional complexity. But if the objects are used extensively in the business logic component you should consider it.

Himeji answered 29/8, 2014 at 7:19 Comment(0)
M
2

Just as a quick answer, the case you described is not the only one :).

Suppose you are working with an internal library providing some POJO / entity / other beans. You want to abstract from the internal representation (for a reason or anohter), you then want to map those bean to yours. It works :

  • for ejb client, or somehting like that,
  • when you don't want to expose internal entities (Business Object vs Presentation object) (see @Henry's reply)
  • you have beans that don't inherit from the same parent (and can't for any reason, even leacy) and you want to tarnsfert value from on to another

There are plenty of (other) reasons :)

As an advice see also and this post : any tool for java object to object mapping?

Mahmoud answered 29/8, 2014 at 7:23 Comment(0)
D
0

Short answer for me as henry said it helps reduce coupling between what you expose or consume and your core data model.

It is one way build Hexagonal Architecture. You can freely modify your core model without impacting the exposed model. In hexagonal architecture, it is used to expose only a small relevant part of the core model.

It is also a very goog way to handle services and model versionning since multiple versions can be mapped to the core model.

When working with XML services I tend to build contract first application so, I first write the XMLSchema then generate Jaxbeans and I realy don't want my business code to be polluted by JAxb annotations.

If you know that your exposed model will always be the same and that your application does not fall in the previously mentionned cases, so you realy don't need to use DTO.

Last, I would recommend using a framework with strong compile time checking like Selma instead of Dozer or Orika because they are evaluating the mapping only at runtime which is weak typing and sensible to refactoring.

Device answered 2/9, 2014 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.