How to implement Clean Architecture in Node.js (and why it's important)

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.พ. 2025

ความคิดเห็น • 171

  • @gregtaylor9806
    @gregtaylor9806 2 ปีที่แล้ว +136

    I like this video because it’s about a specific design concept and not embedded in a massive beginner tutorial. There is not enough mid-level programming info like this. Thanks for making this and Keep it up!

    • @thehumanalive
      @thehumanalive ปีที่แล้ว +8

      Right, it's hard to find quality content for mid or advanced concepts.

  • @TheAkiller101
    @TheAkiller101 2 ปีที่แล้ว +32

    This is a better tutorial on dependency injection and inversion of control than hour long lectures and videos I had to listen to on the same subject.

  • @joseph_oluwayomi
    @joseph_oluwayomi 2 ปีที่แล้ว +8

    I’ve spent a lot of time trying to understand this architecture. I’m always lost when I get to the inversion of control, thanks to your video, it has reinforced my understanding of this architecture.

  • @brandonlange2260
    @brandonlange2260 2 ปีที่แล้ว +57

    Clean architecture, hexagonal and Domain Driven Design are really abstract concepts. They all encapsulate some really great ideas but I also strongly believe that it's something that you shouldn't get too caught up in. Find a level of abstraction that works for you and roll with it. I went on a whole craze a couple years back designing systems with all of these concepts and even though I did build some really cool stuff, it was extremely overkill for what was actually required. Great video though as always! It would actually be really great to pick your brain sometime about some stuff! Can't wait for the next video!

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +3

      You’d be surprised how empty my brain is ;). But if you have a particular topics you want me to talk about let me know

    • @exe2543
      @exe2543 ปีที่แล้ว +3

      Exactly!! The point of designing software is not to use the fanciest or most complex design, it's to get the job done and have a testable, maintainable, and resilient system.

  • @rohit3430
    @rohit3430 ปีที่แล้ว +1

    tomorrow is mine interview and i found this really helpful to explain how i have been doing previously and how this architecture could be helpful to improve further!
    thanks for the video!

  • @vlohar08
    @vlohar08 2 หลายเดือนก่อน +1

    Very helpful. I watched all your clean architecture videos and probably will watch them again to understand it correctly.

  • @jordiyaputra8359
    @jordiyaputra8359 ปีที่แล้ว +1

    For many years, maybe +4 years I have been working with these kinds of code architecture and patterns but I absolutely have NOOO idea what I was doing. Now you are giving me an enlightment! I know you deserve more than this, but please accept my humble subscription to your channel 🙏

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว +1

      thanks for the sub! I'm glad you found my content useful

  • @renepatznick2526
    @renepatznick2526 8 หลายเดือนก่อน

    I spent several hours struggling with the high-level description of the clean architecture diagram in the Clean Architecture book. Your explanation in the video was fantastic - it really clarified things for me. Now it all makes sense!

  • @mibreit-photo
    @mibreit-photo ปีที่แล้ว +3

    Nice video!
    General observation about clean architecture: I'm all for abstraction, but one has to know when to stop ;-) At some point - same as with clean code - instead of making your project more maintainable, you're creating spiderweb of dependencies (even if it's mostly just abstractions / interfaces). Then, understanding and debugging code can mean jumping through a myriad of files. It also depends largely on the size of the project. You need to understand when you should apply those concepts and when it is overkill. Always remember - YAGNI

  • @markrather7863
    @markrather7863 12 ชั่วโมงที่ผ่านมา

    This is a very helpful explanation; appreciate the use of concrete examples when explaining otherwise abstract concepts

  • @pguti778
    @pguti778 2 ปีที่แล้ว +1

    Woooowwww, so clear and real !!! Complete agree that all these architecture things are too-too abstract. I think that architects (and most of the times the sellers) try to think this is magic, like it happened with Agile, or OOP. Thank you man !!!

  • @matthew1106
    @matthew1106 ปีที่แล้ว +2

    Great job going over the important aspect of the concept instead of living in theoryland. Definitely much harder to demo this without using typescript.

  • @krskvBeatsRadio
    @krskvBeatsRadio 2 ปีที่แล้ว +77

    Would be really interesting to see how you structure folders with this approach at your job!

    • @MattBidewell
      @MattBidewell 2 ปีที่แล้ว +11

      I would separate the routing functions into a controller folder, the buisness logic into a services folder with say "UserService", "Email service" and then database or whatever classes into a "DAO" folder (data access object)

    • @tonienguix6503
      @tonienguix6503 2 ปีที่แล้ว

      also you would need a validation folder and provably helpers. good video here: th-cam.com/video/oNlMrpnUSFE/w-d-xo.html&ab_channel=PedroTech

    • @naughtiousmaximus7853
      @naughtiousmaximus7853 11 หลายเดือนก่อน +1

      ​@@MattBidewell this sounds like NestJS?

  • @F4ILCON
    @F4ILCON 2 ปีที่แล้ว +1

    This is the far most understanding videos for people that are starting to understand the Clean Architecture concept, my sugestion would be show how folders are set-up, thanks for the video

  • @danielmaldonado5954
    @danielmaldonado5954 ปีที่แล้ว

    Awesome video ! Very clear. It's true most speakers talks hours and hours of abstract concepts. I like how you managed it to show a real world example in 20 minutes. Thank you !

  • @aaronmotacek9343
    @aaronmotacek9343 2 ปีที่แล้ว +2

    I think this was great. I’m a sucker for code organization practices, but I haven’t looked into backend best practices until now. The examples you gave clearly illustrate the benefit and I think you have just the right amount of detail for anyone that is searching for this topic. Excited to look around your channel more. #subscribed

    • @hojdog
      @hojdog 2 ปีที่แล้ว +2

      I don't think there's real difference between front and back end code architecture. You can apply these concepts to any codebase

  • @mattiadevivo9775
    @mattiadevivo9775 2 ปีที่แล้ว +1

    Really well explained, they should teach this concept in each CS high school and university

  • @wichaisawangpongkasame9237
    @wichaisawangpongkasame9237 2 ปีที่แล้ว

    You really nailed it! I've never understood the hexagonal architecture this much clearer. You do deserve more subscriber. Many thanks!

  • @travelwithahmedmurtaza
    @travelwithahmedmurtaza ปีที่แล้ว

    Very well explained. Instead of discussing abstract concepts you have covered a use case which becomes very easy to grasp the concept and much quickly. Thanks alot

  • @sobhanm9576
    @sobhanm9576 2 ปีที่แล้ว +1

    Fascinating content, most clean architecture are either simple examples or written in another language thanks for clear explanation, hope to see more content about ddd and clean architecture written in typescript 🙌🏻🙏

  • @rorythomas7851
    @rorythomas7851 2 ปีที่แล้ว +1

    My friend, your content is amazing and such a big help. please dont ever stop

  • @AssassinNinja98
    @AssassinNinja98 9 หลายเดือนก่อน

    Thank you so much. I really like that you show the practical side of clean architecture

  • @joseavilasg
    @joseavilasg 11 หลายเดือนก่อน +1

    Thank you so much for this video. It was very helpful. Well explained.

  • @Izzat-bj1le
    @Izzat-bj1le 5 หลายเดือนก่อน

    Finally a tutorial that actually implements clean architecture instead of talking about it. Thank you

  • @FeyroozeCode
    @FeyroozeCode 11 หลายเดือนก่อน

    Good video, with nestJs all is inside the box (Clean architecture,Separation of concerns, Dependency Injectection etc..)

  • @caskispace
    @caskispace ปีที่แล้ว

    Thank you for the vid! Clear, straight to the point and from a perspective that is easier to grasp.

  • @chrisbirkenmaier2277
    @chrisbirkenmaier2277 ปีที่แล้ว

    Would love to see a course about this topic. 3 hour video on how to create a clean application with node, nextjs, maybe some database, everything hooked up with docker compose, testing included. Would pay serious $$$ for something in that direction.

  • @brookesstephens
    @brookesstephens หลายเดือนก่อน

    Thanks!

    • @WebDevCody
      @WebDevCody  หลายเดือนก่อน

      Thank you!

  • @shahzaibshahzaibkhan6480
    @shahzaibshahzaibkhan6480 ปีที่แล้ว

    Very usefull, plus the actual example made everything a lot clearer than if it were just theory. ❤

  • @michaelpesin946
    @michaelpesin946 ปีที่แล้ว

    Great video!
    This a great example to send to junior developers

  • @sahaneakanayaka3394
    @sahaneakanayaka3394 ปีที่แล้ว +1

    Really enjoyed it. Great content. Thank You! 🙏❤

  • @NaveenThally
    @NaveenThally 2 ปีที่แล้ว +4

    It's a nice explanation, if you could share the boiler plate code for clean architecture would be helpful. A series on clean architecture with es6 would also be helpful, Dependency injection as a topic is worth to cover.

  • @captainnoyaux
    @captainnoyaux ปีที่แล้ว

    I really like this topic ! Backend and frontend (I've not yet to find a video on clean architecture with redux thought is someone has one I'd love to take a peek at how people might use it)

  • @gosnooky
    @gosnooky ปีที่แล้ว +2

    Raw-dogging JS like this is hard, even with TS. Our team started using NestJS which is based on dependency injection and this philosophy. It's slightly opinionated, but takes all the guesswork out of architecture, so any newcomers to the projects intrinsically have a working knowledge of the application without looking at a single line of code.

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว

      I haven't tried nest.js yet, but it looks like a solid way to write a rest api from their docs

  • @tarolee7323
    @tarolee7323 2 ปีที่แล้ว

    This video is very helpful!!
    Hope you can give more concrete examples to help us understand the architecture more.
    Thanks again!

  • @fkondratov
    @fkondratov ปีที่แล้ว

    good video, would be great if you had some kind of a series about architecture in general

  • @abhiramkrishnabstb
    @abhiramkrishnabstb ปีที่แล้ว

    Thanks dude this is an eye opener.

  • @wensmartsandy
    @wensmartsandy ปีที่แล้ว

    Great video. Learned function injection. Thanks!

  • @adventurer2395
    @adventurer2395 ปีที่แล้ว

    Awesome! So you're basically organizing the system into framework agnostic controllers, a service layers for business logic, and a repositories to abstract db operations-everything with dependency injection and inversion. Question I have is:
    Do you like doing this at a granular level per use case. For example, create 3 three layers for each user use case, like createUser, updateUser, etc. Or do you prefer to encapsulate related functionality like having a controller, service, and repository/persistence that encapsulate all user-related operations?

  • @balasuar
    @balasuar ปีที่แล้ว

    My 2c's:
    Clean architectural pattern works if you know your business logic; said differently: it's a good choice when starting a project based on an existing product.
    Swapping out the database isn't something teams tend to do often--usually at the very early prototype stages, OR when sunsetting an MVP and moving to vNext.
    If entities implement enterprise logic (and typically requires persisting to a database), where should that logic live? Relationships between entities is often a function of foreign-key relationships across database tables. Does that then mean all enteprise logic lives in the database? Does that then mean enterprise logic by definition is only the type of logic that can be enforced by the database engine?

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว

      I would keep all business logic out of the database. It’s ok to have some basic data integrity checks on your columns, but your code entities should verify a string field is an actual email, or an age isn’t negative and isn’t greater than 200, etc

  • @hakimbencella4242
    @hakimbencella4242 ปีที่แล้ว

    Thank you, Keep the good work!

  • @MartinPerez-mi1ty
    @MartinPerez-mi1ty 2 ปีที่แล้ว

    I finally understood the dp injection thank you

  • @DigitalAlchemyst
    @DigitalAlchemyst 6 หลายเดือนก่อน

    So part of the front end learning process, we do things like create json files with constants of various data, example a lot of portfolios essentially have a json resume that get mapped across the application in various ways.
    Would you say its safe to think of using this practice over having a big chunk of json at the top of the code that uses as the most micro implementation of clean arch? I see it as at least to a degree the code mapping the data in the portfolio has no knowledge ( or concern ) aside from the import of a json file or how the data is stored the code just gets data from else where. I have also gone through old projects built liked this and upgraded them to use a database with out having to change the presentation logic. of course even something that small could be broken down more like a pseduo DTO between the 2 or something.
    But I am just trying to see if i can think small and step my way up to a bigger understanding.

  • @KawtharAlekri
    @KawtharAlekri 11 หลายเดือนก่อน

    Supet great and understandable!! Huge thanks 🙏🏻

  • @logisticedits7429
    @logisticedits7429 2 ปีที่แล้ว +1

    Please make a full series of nodejs. But not basics but advance like how big scaling project code works, how uber uses nodejs

  • @teerapatprommarak2070
    @teerapatprommarak2070 2 ปีที่แล้ว

    Great explanation, thank you!

  • @andreimclive
    @andreimclive 2 ปีที่แล้ว

    FANTASTIC video. ThaNk you.

  • @joshuamangi6475
    @joshuamangi6475 2 ปีที่แล้ว

    Hi there mate, thanks for this video. I am subscribing and hope you can dive deeper into this topic with maybe showcasing a small project with MongoDB and how this can be implemented. Great work though

  • @theoldknowledge6778
    @theoldknowledge6778 2 ปีที่แล้ว

    Excellent video. Btw, which VS Code theme is this?

  • @joesmoo9254
    @joesmoo9254 ปีที่แล้ว

    outline: The speaker explains hexagonal architecture, also known as ports and adapters, and how it is used to protect business logic from changes in external sources. This architecture is beneficial in simplifying future changes to external dependencies, such as switching from MySQL to MongoDB. The speaker uses a pseudocode example of an Express app to demonstrate how to apply clean architecture, which involves decoupling business logic from external sources. The speaker emphasizes the importance of defining interfaces to pass in real implementations and avoiding tightly coupling to third-party dependencies or frameworks. The example includes removing Express-related code from the business function and creating a new file for database persistence.
    The speaker explains the implementation of clean architecture, which separates business functions from third-party dependencies. They show how to use dependency injection to achieve this, without getting caught in abstract concepts. They highlight the use of entities, which contain business rules and logic, and can be validated before being saved to the database. By using clean architecture and entities, the speaker suggests it is possible to structure code effectively and ensure it meets criteria. They advise viewers to use a framework or library to help with implementation.

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว

      Made with chat gpt?

  • @DigitalAlchemyst
    @DigitalAlchemyst 6 หลายเดือนก่อน

    Im still seeking those concrete examples because I am having trouble wrapping my brain around like what is the domain layer what is the business layer ( working in next now ) Hoping this clears things up more

  • @PJ-od9ev
    @PJ-od9ev 6 วันที่ผ่านมา

    Important question! Do you believe we must use Nestjs to have clean code for an Expressjs app?

    • @ebipahn
      @ebipahn 6 วันที่ผ่านมา

      I wann know that too

    • @WebDevCody
      @WebDevCody  5 วันที่ผ่านมา

      No, but it can help give structure if you’ve never built a backend api before

  • @judeclassic4179
    @judeclassic4179 2 ปีที่แล้ว

    bro.. i love you thanks for the video

  • @kreyzor
    @kreyzor 2 ปีที่แล้ว

    Amazing explanation!! Very Simple to understand and very straight forward!!

  • @dianrahmaji
    @dianrahmaji 2 ปีที่แล้ว

    this video is really helpful!

  • @hosseinnajafi2181
    @hosseinnajafi2181 5 หลายเดือนก่อน

    Thank you so much 🙏

  • @janschatz7223
    @janschatz7223 2 ปีที่แล้ว +1

    The separation between the Infrastructure and Interface Adapters layers is unclear to me. In which layer does createUserPersistence.js belong. It directly uses the database, which is infrastructure. So it can't be part of the Interface Adapters layer as it is not allowed to access the Infrastructure layer. But what is inside the Interface Adapters layer then?

  • @gbroton
    @gbroton ปีที่แล้ว

    Business logic is not a domain logic. Separating domain logic from business (aka application) logic is the purest form of a clean architecture also called the onion architecture. The thing which you are calling "businesses logic" is the "application business rules" in the clean architecture picture. But great video in general showing the whole idea in a clear way.

  • @alex-dk2rj
    @alex-dk2rj 2 ปีที่แล้ว

    Great video, thanks.

  • @iljushka7198
    @iljushka7198 2 ปีที่แล้ว +1

    Probably it is all about dependency inversion principal you’ve told in this video. And this is only one part of Clean Architecture, like in-out ports

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      It’s hard to do true CA with a dynamically typed language. Something like typescript could setup interfaces for the ports, but js doesn’t support explicit interfaces; all interfaces are implicit. So the in port to the interactor is just the arguments passed in and the out port is what is returned

  • @kaloianangelkov4919
    @kaloianangelkov4919 2 ปีที่แล้ว

    Amazing explanation ! Got my sub !! :) Do you have any tutorials or videos where you speak about DTOs? (Data Transfer Object)
    I'd love to hear more about this.. :)

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      Not really, I have a recent video about clean arch but I don’t often use dots

  • @kennedydre8074
    @kennedydre8074 ปีที่แล้ว

    This is amazing, thank you, if I wanted to use zod or joi for the validation how would I do it without actually requiring or importing the packages in the entities layer as those packages belong in the outer most ring?

  • @misterl8129
    @misterl8129 2 ปีที่แล้ว

    very good video for get a little close to the CA. Maybe if you can do a next video showing like, the adaptor. So if for example i want to change between mysql to postgress, i dont need to change nothing inside the code besides the caller.
    Thank you!

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      The useCase should be able to call the same methods used to get data from the database. You should be able to just swap out the persistence methods at the higher level

  • @DuyTran-ss4lu
    @DuyTran-ss4lu 2 ปีที่แล้ว

    wow, amazing!

  • @Jackinua
    @Jackinua ปีที่แล้ว

    Where will you put validation for Entity foreign key or Entity unique. Is it domain layer validation or iteration layer validation?

  • @dazzaondmic
    @dazzaondmic 2 ปีที่แล้ว

    This is really informative, thank you! Quick question: what are some other benefits of implementing this kind of architecture apart from being able to easily swap out implementations? I'd like to advocate for this at my company but I don't feel like I have a strong enough argument to really vouch for it.

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      It can help with testing since it’s easier to mock out dependencies that are passed in instead of relying on a mocking library to achieve that. It also keeps the code base organized so all business logic exists in a similar place instead of it being sprinkled all throughout your code base. It takes discipline to double check you’re adding code to the correct layers.

    • @Ahmad-ww4ue
      @Ahmad-ww4ue 2 ปีที่แล้ว

      Yeah testing is a big one. I've actually learned about dependency injection when we were trying to prevent tight coupling between classes so that each class can have its own unit tests written separately. That was why I understood the clean architecture approach that this video takes to dependency injection/inversion of control.

  • @adanos2006
    @adanos2006 ปีที่แล้ว

    this video makes me subscribe

  • @santiagopabloortiz6322
    @santiagopabloortiz6322 2 ปีที่แล้ว

    Pure gold.

  • @butwhothehellknows
    @butwhothehellknows 2 ปีที่แล้ว

    Good job babe!!

  • @guillaume5623
    @guillaume5623 ปีที่แล้ว

    This is gold

  • @rachidb9624
    @rachidb9624 2 ปีที่แล้ว

    Thanks a lot !

  • @gincegeo
    @gincegeo ปีที่แล้ว

    what is the best naming conventions? can you give us a repo we can refer?

  • @akay64x2
    @akay64x2 2 ปีที่แล้ว +1

    In what world switching from MySQL to MongoDB would take one line of code? They would not even have the same ORM layer. It will only be partially possible when using an ORM layer that supports the same type of DB as you want to swap and that also would be 80% coverage as for any nontrivial application, some queries will be written raw. - Just dropping this in here so someone wont get over sold or x or y architecture.

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +3

      The point is your business logic is decoupled from the code that accesses the database and those database access methods are injected as dependencies meaning you can potentially switch certain parts of your data models from one database to another. Your ORM would live in a persistence layer, it wouldn’t be one line so sorry if I said something dumb like that. The point was to make the code more decoupled and easier to refactor in the future

  • @marcusrehn6915
    @marcusrehn6915 ปีที่แล้ว

    Regardless of the abstraction, any non trivial app is going to be hell to migrate from mysql to mongo or vice versa

  • @baremetals
    @baremetals 2 ปีที่แล้ว

    Excellent.

  • @teamtosoz892
    @teamtosoz892 ปีที่แล้ว

    use cases are objects with their own request and and response objects

  • @marcosmolina8606
    @marcosmolina8606 2 ปีที่แล้ว

    Hi! I would ask if you don't think we can catch validation error before in some middleware. I use express-openapi and works very good.

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      You could do that, but I do think validation should be as close to your business entity as possible. It’s probably safe to validate the payload coming in, then also validate the entities as you change their properties

  • @bilkisuismail6096
    @bilkisuismail6096 ปีที่แล้ว

    @webDevCody, i was wondering if you need interns in you work place. I would break a leg just to be mentored by you. You inspire me a lot.

  • @andriisukhariev
    @andriisukhariev 2 ปีที่แล้ว

    F*ing good. Do you have anything on clean architecture folder structure for small / mid / about to scale projects ? tthanks

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      Not at the moment, I need to make a starter repo

    • @andriisukhariev
      @andriisukhariev 2 ปีที่แล้ว

      ​@@WebDevCody looking forward. By the way - why do you have "require" instead of "import" - do you use older versions of node for a reason ? Also i found very little amount of tutorials on project structure when you have both static pages and API in one project.... might be interesting for you next tutor. Thanks!

  • @edu.paixao
    @edu.paixao 2 ปีที่แล้ว

    Great example!
    Can you publish the code? Tks!

  • @codenameich
    @codenameich 2 ปีที่แล้ว

    thanks bro 🙏, any link on github for code in this video ?

  • @bollo_omar
    @bollo_omar ปีที่แล้ว

    What theme are you using for the text editor

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว +1

      Bearded theme stained blue

  • @phamucmanh1750
    @phamucmanh1750 2 ปีที่แล้ว +1

    The one thing I am afraid of it is that code perfomance will be slower

  • @qwertyntarantino1937
    @qwertyntarantino1937 2 ปีที่แล้ว

    @ what are you using for code completion? Your autocomplete plugins are way too far from what standard intellisense can offer

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      Probably copilot?

    • @qwertyntarantino1937
      @qwertyntarantino1937 2 ปีที่แล้ว

      @@WebDevCody nice, thank you, never saw it at work

  • @jf3594
    @jf3594 7 หลายเดือนก่อน

    This is how those things should be explained

  • @SachinYadav-yx1rc
    @SachinYadav-yx1rc 2 ปีที่แล้ว

    Would be great to know clean architecture in React app since its so unopinionated

  • @br3nto
    @br3nto ปีที่แล้ว +1

    Clean Architecture has been going out of favour in the .Net world for a few key reasons. It requires a lot of code duplication and mapping to communicate between layers; functionality is hard to find because of abstract structuring; the previous two make refactoring extremely time consuming and tedious. A better architecture is vertical slices.

  • @donnyroufs551
    @donnyroufs551 2 ปีที่แล้ว +1

    Clean architecture is yechnically not an architecture. There is a reason that you cannot find a proper example by uncle bob himself. Its simply onion architecture with a few added principles.
    You missed the fact that dtos play a very important role since technically you are still coupled.
    I kind of dislike that people mention decoupling for ease of switching stuff because it barely happens... the biggest win imo is testability.
    Overall a great introduction to tease people their brains! We should collab someday.

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      Thanks for the comment! I think it’s good for both unit testing and swapping out modules in integration tests. Often when dealing with AWS there are a lot of services we interact with, so when running out integration tests in CI/CD or locally, we want to swap out the implementations to save files to local s3 mock services, mock dynamodb services etc. clean arch helps. But you’re right, often we don’t care to swap out our databases in the future.
      When you say dto, are you referring to the data Id be sending to the repository layer? Like in my example, createUserPersustence technically shouldn’t be passed the UserEntity, but instead a subset of necessary properties. But everything in JavaScript is an implied interface so it doesn’t matter too much as long as the object passed has the necessary properties on it.
      Send me any resource you have related to DTO. I think we might call the serializers or deserializers in our project. The things you run yours data through before you send it back from the API to the UI right?

    • @donnyroufs551
      @donnyroufs551 2 ปีที่แล้ว

      @@WebDevCody Hey 👋
      Technically a repository is supposed to get the domain entity and from there on map it to a model that it needs to persist it to the database, so that part is completely fine! What I'm referring to is that you are using POJOs (simple objects) and just passing it the data, instead, it should be a DTO imho.
      Having a DTO makes it that an interactor doesn't have to know about the mapping, and gives the user / dev a contract to work with which allows for independence. I guess for sending an email you probably would create a factory E.g. emailFactory.createEmailResponse(User) which would return you a dto to pass to the email service.
      Khalil has very good stuff:
      khalilstemmler.com/articles/typescript-domain-driven-design/repository-dto-mapper/
      he's also in the middle of writing a book called "SOLID" not to mistake with the famous principles, its not done yet but it's to a point where it's worth a read. Anyway, mad respect for doing this kind of vids man! More developers need to get into this.
      edit:
      Should mention, after all its about trade-offs anyway. In a real world app we just pass a dto from controller to the repo and dont even care about entities because the amount of domain logic or need for completely being decoupled is not there.
      one of my projects that I stopped working on:
      github.com/donnyroufs/cofey/tree/develop/libs

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      @@donnyroufs551 Im not sure if I’m fully understanding. I could see the value when making a public api and you don’t have access to the clients consuming your api. In that case you’d want a contract to prevent your consumers to get changed data. (Unit and integration, or contract testing can verify this). On the project I’m on, if we update our entity, we also just go into the UI code and updates whatever might have changed. Usually the most that has changed is we added an additional property to an entity, so we don’t care if the ui is coupled to the api response because it’s just additional data coming back. I don’t see a value just yet for using a DTO between the business layer and persistence layer (or email function like you mentioned). The function is defined via an interface so the arguments are already coded to a contract.
      But yes, I agree most of all of this is a trade off. Adding all this “clean” design and abstraction makes the code a lot more complex to understand. We as engineers just need to know the pro and cons and apply these principles where we think they’d work best

    • @donnyroufs551
      @donnyroufs551 2 ปีที่แล้ว

      @@WebDevCody Well a DTO is not just for your infra layer, it's to make sure you decouple layers and to move mapping to another place. Having a simple dto at controller level isnt enough if you want proper versioning either.

  • @nikk1019
    @nikk1019 11 หลายเดือนก่อน

    This could be also done with CQRS pattern, i find it pretty convenient

  • @AdibPeerzade
    @AdibPeerzade ปีที่แล้ว

    Hi,Can you build a beginner tutorial of this?

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว

      Maybe if I have time

  • @shyraccoon4322
    @shyraccoon4322 2 ปีที่แล้ว

    i think beginners should start with nest js ... it's hard for them to apply clean architecture in express and nest js forces them to do it

  • @Cognitoman
    @Cognitoman 2 ปีที่แล้ว

    I wanna try to do this with nest.js, I'm gonna use repository interface and have a repository class that implements that interface and uses typeorm, then for inside my service class I inject that repository by the interface, then later I can switch out the orm with another orm if I need too or use both it dont matter

  • @jggabayno
    @jggabayno 2 ปีที่แล้ว

    can u update this concept but with using prisma? :)

  • @canmertinyo
    @canmertinyo 9 หลายเดือนก่อน

    thanks

  • @jggabayno
    @jggabayno 2 ปีที่แล้ว

    I have question, what if i want to validate if the user exist from database before calling a post request? how and where i can put the code, since in user entity it has validation without db code stuff.
    I wonder if its good to validate if user exist in createuserpersistence like this? hope it make sense.
    connection.query("SELECT * FROM users WHERE psid = '"+ psid +"'", (err : any, res : any) => {
    if(res.length === 0){
    // connection.query(`INSERT INTO users ....
    }
    })

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      I’d make a persistence method called getUser and you’d call it from the interactor. If getUser returns null, you can call the createUser persistence method. If the user exists, have the interactor throw an exception and your api layer would catch and return a 4xx status code

    • @jggabayno
      @jggabayno 2 ปีที่แล้ว

      @@WebDevCody Got it. btw thank you for this awesome content. Also for your quick response. Appreciated

    • @jggabayno
      @jggabayno 2 ปีที่แล้ว

      @@WebDevCody

      i dont know if its good to pass the res and getUser persistence to validate and send status in user entity. is it okay or bad?
      // user interactor
      await user.validate(getUserPersistence, res);

      const newUser = await createUserPersistence(user)
      return newUser
      // user entity
      async validate(getUserPersistence : any, res: any){
      const user: any = await getUserPersistence({psid: this.psid})
      if (user.length) {
      return res.status(409).send('User already Exist')
      } else if (!this.psid || !this.name || !this.email){
      return res.status(409).send('Improper Values')
      } else {
      return this;
      }
      }

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      @@jggabayno the interactor should not know about res at all. Also, I wouldn’t have your entity call the persistence method. Take the data returned from getUser and construct a new user entity using it, then validate the user entity

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว +1

      Join my discord for more help

  • @codiction
    @codiction 2 ปีที่แล้ว

    I need that theme!!!

  • @pourlamouvance
    @pourlamouvance 2 ปีที่แล้ว

    In your experience is it worth it?

    • @WebDevCody
      @WebDevCody  2 ปีที่แล้ว

      I think at least keeping your code split into separate layers and the dependency inversion / injection can help keep your project more maintainable and easier to test.

  • @sigitwasissubekti788
    @sigitwasissubekti788 11 หลายเดือนก่อน

    get you share source code in github?

  • @shuruzer
    @shuruzer ปีที่แล้ว

    Great video, but not so far from what you criticize in the beginning. Every single video on this topic turn around the same things "abstract", "here is the theory", "oh here is some (a lot of) code i already wrote, im gonna create a new file to make the project clean. and blabla theory", and you show up some code, but "oh i m not gonna run this. This is pseudo" but when someone will drop a full walkthrought with a real app (even just a simple CRUD) but from scratch, and show the whole project from the very begining to the execution ?

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว

      sorry to disappoint with this video.

    • @shuruzer
      @shuruzer ปีที่แล้ว

      @@WebDevCody it is still helpful for mid/confirmed devs, but its kinda frustrating when i think about headaches my beginner self from past years had when trying to create a simple project using this architecture because there was no real walkthrough, like from "npm init". If you can do this, iam sure it'll help someone or two 😏

  • @adrianmouzinho9615
    @adrianmouzinho9615 2 ปีที่แล้ว +1

    Muito bom 🇧🇷