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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
reiley
  • 3,759
  • 12
  • 58
  • 114

3 Answers3

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.

Henry
  • 42,982
  • 7
  • 68
  • 84
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?

Community
  • 1
  • 1
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
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.

slemesle
  • 1,099
  • 7
  • 5