• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Node.js Design Patterns: DDD and clean architectures in Node

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

One thing I always struggle with when I am coding node solutions is how to make a clean architecture separating things like domains, infrastructure, etc... I struggle on injecting dependencies (specially on the domain layer) and at the end I just end up importing them directly from other folder. I am thinking on dependencies such as the logger, event managers, etc...
I come from Java world where I find it much more intuitive and have serious problems when trying to replicate it in Node.

Thanks!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Albareto,

in my opinion, one of the features that make Node.js enjoyable to use is that dependencies are explicit. In Java is very common to use a dependency injection container, which adds a level of indirection, which somewhat complicates the readability of the code. In Node.js, if you want to import a singleton, you can just export an instance from a module (e.g. module.export = new MyClass()) and import the singleton with a simple call to require() or import. It's that simple.
To me, a "clean" architecture in Node.js, means creating modules that do only one thing and well (cohesion) and that expose a minimal interface (information hiding). Organizing those modules in a coherent directory structure is also important.

To be honest, in my early days of Node.js, I too struggled to apply the knowledge I had acquired from using Java and other strongly typed languages. In fact, one of my early Node.js projects was a dependency injection container. With time I learned to let go of my old mindset and accepted the fact that in Node.js certain things are just basic simple and in my opinion, that's  amazing.

PS: This is such a common question that in the book we have dedicated section called "Wiring modules" where we talk exactly about that.
 
Albareto McKenzie
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mario Casciaro wrote:Hi Albareto,

in my opinion, one of the features that make Node.js enjoyable to use is that dependencies are explicit. In Java is very common to use a dependency injection container, which adds a level of indirection, which somewhat complicates the readability of the code. In Node.js, if you want to import a singleton, you can just export an instance from a module (e.g. module.export = new MyClass()) and import the singleton with a simple call to require() or import. It's that simple.
To me, a "clean" architecture in Node.js, means creating modules that do only one thing and well (cohesion) and that expose a minimal interface (information hiding). Organizing those modules in a coherent directory structure is also important.

To be honest, in my early days of Node.js, I too struggled to apply the knowledge I had acquired from using Java and other strongly typed languages. In fact, one of my early Node.js projects was a dependency injection container. With time I learned to let go of my old mindset and accepted the fact that in Node.js certain things are just basic simple and in my opinion, that's  amazing.

PS: This is such a common question that in the book we have dedicated section called "Wiring modules" where we talk exactly about that.



Yes, I think the key is to let it go from other programming paradigms. I think (this is a personal self-assessment) that we tend also to overengineer solutions lots of times.

Thanks a lot Mario
BTW I have won a copy and I will read it very carefully when I receive it. Thanks again.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic