Jgamer1y
Jgamer1y12mo ago

Is there a recommended library to do dependency injection in Deno?

I'm creating a Rest API with the repository and controllers pattern but my code in the main.ts file keeps getting bigger and bigger
// Repositories
const animeFavoritesRepo = new MongoAnimeFavoritesRepository(mongoDatabase);
const animesRepo = new MongoAnimesRepository(mongoDatabase);
const usersRepo = new MongoUsersRepository(mongoDatabase);
// .... more repos

// Routes
// I'm injectin the controllers and repos
const auth = new AuthRoutes(new AuthController(usersRepo), usersRepo).router();
app.use(auth.routes());
app.use(auth.allowedMethods());
// ... more routes
// Repositories
const animeFavoritesRepo = new MongoAnimeFavoritesRepository(mongoDatabase);
const animesRepo = new MongoAnimesRepository(mongoDatabase);
const usersRepo = new MongoUsersRepository(mongoDatabase);
// .... more repos

// Routes
// I'm injectin the controllers and repos
const auth = new AuthRoutes(new AuthController(usersRepo), usersRepo).router();
app.use(auth.routes());
app.use(auth.allowedMethods());
// ... more routes
11 Replies
Antonio Sampaio
Antonio Sampaio12mo ago
Have you tried any packages that are popular in nodejs ecosystem? Maybe inversify, tsyringe
Jgamer1y
Jgamer1yOP12mo ago
I tried with npm:tsyringe and ¡ it works ! Thanks for the response 👍 I will use it in my project
fro.profesional
fro.profesional12mo ago
@Joaquín Buendía do you know if it works on deno deploy? Did you have to do anything to make it work? Also ant specific Deno version?
Jgamer1y
Jgamer1yOP11mo ago
I don't know if it works in deno deploy, my project runs on a docker image I just followed the tsyringe documentation but using the npm: tag
Jgamer1y
Jgamer1yOP11mo ago
GitHub
GitHub - microsoft/tsyringe: Lightweight dependency injection conta...
Lightweight dependency injection container for JavaScript/TypeScript - GitHub - microsoft/tsyringe: Lightweight dependency injection container for JavaScript/TypeScript
Jgamer1y
Jgamer1yOP11mo ago
My Deno version: 1.38.5. Docker image: denoland/deno:alpine-1.38.5
fro.profesional
fro.profesional11mo ago
Thanks!
manodhya.o
manodhya.o9mo ago
hey, can i know any 3rd party package that runs on Deno environment that do DI? I tried many packages but didn't work, or giving the error of "InvalidWorkerResponse"
fro.profesional
fro.profesional9mo ago
Yes they work but bundle is needed On deno cli works normally, on deploy bundle is needed
manodhya.o
manodhya.o9mo ago
can i have more context on this as i'm new for DI. i'm using https://deno.land/x/alosaur@v0.9.0/src/injection/README.md?source= and followed everything as they mentioned. but still getting the "InvalidWorkerCreation" error
Sorikairo
Sorikairo9mo ago
You can use Danet and leverage its custom DI system. It even provides a CLI to bundle for Deploy, a guide on how to do so manually, and a github action ready to use.