3

I want to create an Express REST API and want to try following the clean architecture ideas. I was reading about it but didn't get the idea of the communication between the abstraction layers.

Let's say I have this structure

routes => controllers (sending the response) => use cases (bundling all the logic) => repositories (fetching data from the database) => database queries

I read about object mappers creating models to communicate between the layers. But when I'm thinking about complex objects with many optional fields I might come up with this case:

I want to create a new user when calling POST /users and have to pass in a username and a password and might pass in multiple optional fields. When creating a UserEntity / UserModel, should I setup nullable fields for every optional field? And when I want to return the created database user I have to create a model again by mapping all those properties.

I think this might get really messy if your models expect many constructor parameters. And things might get even more messy if your database model differs from the response model being sent to the client.

Would you mind explaining a clean communication flow when passing in data from the controller all the way down to the database and returning it all the way up back to the controller to send the response to the client?

Question3r
  • 155
  • 11
  • "Would you mind explaining a clean communication flow when passing in data from the controller all the way down to the database and returning it all the way up back to the controller to send the response to the client?" I've already done that [here](https://softwareengineering.stackexchange.com/a/380254/131624). Is that what you needed? – candied_orange Sep 25 '19 at 01:30
  • thanks for your reply :) hmm not really. I already know how to create that flow but I just would like to know how to handle all those data transfer objects from layer to layer – Question3r Sep 25 '19 at 05:21
  • 2
    Well the DTO's were `InputData`, `OutputData`, and Entities. My advice for how to handle multiple optional fields comes from: [Giulio Canti](https://gcanti.github.io/2014/09/25/six-reasons-to-define-constructors-with-only-one-argument.html) – candied_orange Sep 25 '19 at 08:12
  • @ candied_orange thanks, I already use destructuring :) So you would create models for the input down to the database and models back to the client? – Question3r Sep 27 '19 at 17:24

0 Answers0