DEV Community

Cover image for Dependency Injection With Deno 1.29.1 (Node Compatibility)
bronifty
bronifty

Posted on

Dependency Injection With Deno 1.29.1 (Node Compatibility)

Clean Architecture With Deno

Youtube Link
Github Repo

Leveraging Uncle Bob principles of Clean Code and Architecture to design some inversion of control and dependency injection in Deno's newest version with Node compatibility. The source uses classes instead of functions, because classes are cleaner (they house all properties and methods with access using a dot operator, instead of fishing around files and gathering exports).

To run this:

-make sure latest version of deno is installed
(deno 1.29.1) with npm: compatibility

deno run src/server.ts --allow-all
Enter fullscreen mode Exit fullscreen mode
  • answer 'y' to all prompts

There are 2 endpoints

1 GET /
2 POST /users -d {name, age}

curl localhost:8080/

curl -X POST -H "Content-Type: application/json" \
 -d '{"name":"abc","age": 123}' \
 localhost:8080/users
Enter fullscreen mode Exit fullscreen mode

Note

  • I did have an error in the node code because the post route kept creating duplicates despite the logic checking for that. For some reason, deno version runs much quicker and it doesn't have the bug.

Todo

  • add some tests

Top comments (2)

Collapse
 
reggi profile image
Thomas Reggi

Interesting use of a constructor and declaring this methods within it. I like the idea of dependency injection like this. I do just feel like the database can be passed into a function directly like this. I've been trying to tinker with a simpler routing system that works with Request and Response and URLPattern.

gist.github.com/reggi/6bb464619468...

Collapse
 
bronifty profile image
bronifty

Will check