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?

4
  • "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. Is that what you needed? Sep 25, 2019 at 1: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, 2019 at 5:21
  • 2
    Well the DTO's were InputData, OutputData, and Entities. My advice for how to handle multiple optional fields comes from: Giulio Canti Sep 25, 2019 at 8: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, 2019 at 17:24

0

Your Answer

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