1

I've the following project structure (simplified):

  • domain
  • dto
  • view

In domain model is kept. dto classes are used to pass data from domain to view. And in view I've a controller and a handful of *req, *res classes to pass the data via HTTP. So there are 3 classes (in model, dto and view) to express the same thing.

The problem is that I had a SalesEntry and PurchaseEntry in the model. Both classes where exactly the same so I had two entities but only one EntryDto along with EntryReq and EntryRes. All good. Now, it turned out that I need to add some fields but, to PurchaseEntry only. So I added some abstraction and doubled the classes in every layer. This was a big cost. Where did I go wrong and how should it be done correctly?

In languages other than java I'd probably pass some maps here and there, but in java in particular I need to have a concrete class in every layer. How to simplify it?

7
  • 2
    Generally you want to encapsulate the model in a facade to be used in other parts of your program. In other words, you don't need more than one class holding the data for every possible entity. If you're copying and pasting code in other parts of your program to do very similar things, you're probably doing something wrong. Commented Oct 17, 2018 at 7:57
  • I already have a facade to prevent leaking model. The facade uses dto objects to return the model data. But I don't want use dto to transport data via HTTP layer. That's the question, what am I doing wrong? ;) Commented Oct 17, 2018 at 8:08
  • Is there a particular reason why you must have a dedicated class for the HTTP layer? If you were sending back JSON, for example, your facade could simply produce that JSON to be sent representing the data without the need for another layer. Commented Oct 17, 2018 at 8:10
  • @Neil, ok now I understand where you point me - to eliminate req and res classes, yes? Facade might be also used to communicate with other facades - and it definitely should not be done via JSON files. Commented Oct 17, 2018 at 8:13
  • 1
    "Facade might be also used to communicate with other facades" - IMO you'd have to distinguish between two use cases: 1) you have communication inside one application in which case I'd not use a facade (which basically is meant for outside communication) and 2) you have communication between multiple applications (e.g. microservices) in which case using JSON as the interchange format wouldn't be that bad a choice as you'd need some serialization anyways. Commented Oct 18, 2018 at 8:32

0

Your Answer

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.