9

Let's say I'm building a Customer web application(in Spring Boot 2), which also exposes rest end points. I'm modeling my application into 3 layers.

  1. a) UI - CustomerDTO

    b) REST - CustomerRESTResponse/CustomerRESTRequest

  2. Service - CustomerDTO

  3. Data - Customer (Entity)

Does every layer need to have it's own business object representation? If entity is almost same as the DTO, then what's the use?

Dherik
2,50423 silver badges34 bronze badges
asked Apr 5, 2018 at 10:23
1
  • About your last question, see this two answers. TL;DR: Don't use your entity as DTO. Commented Apr 5, 2018 at 11:41

1 Answer 1

11

There is no "correct" answer here. It's really about finding the tradeoffs you're happy with.

Sharing one model across layers trades coupling for development speed. It will allow you to get your code working faster, but it inherently couples all your layers together. A change to the model in the Service layer will affect the models the API consumer sees.

On the other hand, having dedicated models for each layer increases development time, but decouples your layers. If you make a change to a model in the Service layer it doesn't automatically trickle over into the other layers. This reduces the risk of inadvertently breaking things in other layers or inadvertently leaking unnecessary information to API consumers or the database.

The downside here is, as mentioned before, the additional upfront development cost, as well as the additional cost required when the model changes actually do need to apply to other layers.

So, you're trading between loose coupling and speed of development.

Does every layer need to have it's own business object representation?

No, it doesn't need to.

If you value speed of development over maintenance costs then sharing a model across layers is a good choice.

On the other hand, most software project's costs are in the maintenance phases. So organising your code to make maintenance easier could save you time/money in the long term.

Should every layer have its own business object representation?

The answer really depends on what your optimising for.

answered Apr 5, 2018 at 11:21
1
  • Actually having separated objects will save development time where changed to only one layer will not force you to add extra if...else statements for other layers Commented Apr 7, 2018 at 23:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.