Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide examples for validations and multiple dependencies #2

Open
4 tasks
Lakshaya-Sood opened this issue Aug 4, 2020 · 1 comment
Open
4 tasks

Comments

@Lakshaya-Sood
Copy link

Lakshaya-Sood commented Aug 4, 2020

Hi @dcaponi

I really like your approach and yours is the only I found so close to what I was looking for.
Therefore, I am planning to migrate an existing service from SailsJs (MVC/ruby on rails philosophy based) to Onion architecture. Moreover, the current service doesn't even use the View layer of MVC. So everything lies in the model and controller.

  • Can you please tell me more about validations and which layers should do what kind of validation? Following are my concerns:
  1. Incoming request body validation. (do we need additional modal for this)
  2. Application-level database validation with referential integrity (in the context of Mongo DB)
  3. Since MongoDB 3.6, we have $jsonSchema available. Should we put database level schema validation instead of the application level?
  4. any specific reason, why you choose to use Mongoose. What do you think about MongoDB native driver?
  • if let say StudentUseCase layer requires the use of both StudentRepository and ClassRepository, then how should multiple dependencies be passed

  • any tips on handling a promise .then() chain when there is multiple to and fro movement of flow from use case to the repository

  • Example on error should bubble up till controller and logging errors with a better description

Please let me know if you forget to mention any detail. Feel free to correct me. Your suggestion are welcome 💯

@dcaponi
Copy link
Owner

dcaponi commented Aug 4, 2020

Hey @Lakshaya-Sood Thanks so much for your interest! I honestly wasn't expecting someone to really deep dive into this, so please forgive me if my answers seem shallow. I'll answer in the italics to as many of your questions as I can.

Moreover, the current service doesn't even use the View layer of MVC. So everything lies in the model and controller.
Yes, there is no view, as I wanted that to be completely independent of the API. I've seen what happens when the backend and the frontend get tightly coupled (old ruby on rails apps) and it isn't pretty, so I just return json responses and let a free-standing frontend figure out what to do with the responses

Incoming request body validation. (do we need additional modal for this)
Application-level database validation with referential integrity (in the context of Mongo DB)
Im not 100% sure what you're asking here... it seems like this and the question above refer to where you validate incoming data right? If thats the case, I'd aim for validating request-related things (authentication headers, request headers etc) in the controller, then validate data required for the use case at the use case level. You might also want to do data validation here as well (e.g. required fields) depending on your data store of choice and whether or not it supports data validation natively. There is some guidance that suggests letting database data validation be the last resort as data layer trips are more expensive

Since MongoDB 3.6, we have $jsonSchema available. Should we put database level schema validation instead of the application level?
yes. when I wrote this, I wasn't all that familiar with MongoDB (and admittedly am still not) but your reasoning is sound here and it makes sense to let the data storage layer (mongo) figure out if the schema which applies to it is valid.

any specific reason, why you choose to use Mongoose. What do you think about MongoDB native driver?
I used Mongoose in the past, otherwise, no reason not to use native driver

if let say StudentUseCase layer requires the use of both StudentRepository and ClassRepository, then how should multiple dependencies be passed
this is where this example starts to break down. when I designed this, I was really going for isolated paths for each model. In fact, I'm not entirely convinced that having use cases is the best idea either. you could probably repeat the structure by adding a students_classes folder and the routes/controllers to support endpoints like /students/:id/classes but that might cause a bunch of duplicate code. You could probably also modify the controller to accept an array or map of repositories and then depending on if the route is asking for a nested resource, you'd retrieve one, then the other using the multiple repositories

any tips on handling a promise .then() chain when there is multiple to and fro movement of flow from use case to the repository
unfortunately no, Im not sure what your use case is and its been a long time since I've worked with this code (or node in general) :)

Hope this helps somewhat. Good luck on your project. Link me to your repo (if you can) I'd love to see what you come up with!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants