Skip to content

dave-stevens-net/di-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Evaluation of Dependency Injection Frameworks

This repo shows a comparison between two dependency injection (DI) frameworks, namely TSyringe and TypeDI.

Evaluation of Frameworks

The following comments come from working knowledge of two other frameworks, Inversify and typed-inject.

  • I have been using typed-inject for a while in another project and I can’t stand it. The configuration is clumsy and bloated. It seems like you have to jump through a lot of hoops, just for type checking in your config.
  • TypeDI and TSyringe seem almost the same.
  • See https://npmtrends.com/inversify-vs-tsyringe-vs-typed-inject-vs-typedi for download counts.

Inversify

  • ❌ No auto injection making configuration more labor intensive and more code.
  • ✅ Docs are good.
  • ✅ Most popular.
  • ❌ Not evolving with newer features.

TSyringe

  • ✅ Auto injection makes for cleaner configuration.
  • ✅ Docs are good.
  • ✅ Singletons are more clearly marked and understood making code more readable.
  • ✅ Developed by Microsoft, which should give it some staying power.
  • ❌ Provider syntax is a little clunky but allows for customization

TypeDI

  • ✅ Auto injection makes for cleaner configuration.
  • ❌ Documentation was incomplete (see Service decorator and Container API).
  • ❌ Every dependency is a singleton by default and you must declare it as transient if you want it otherwise. This can lead to accidental singleton usage.

Conclusion

Me and another developer on my team settled onTSyringe ✨ as it seemed to have the most positives going for it.

Running the Demo

nvm node 18 # or whatever Node version management tool you use.
npm i
npm run build
npm run start:tsyringe # shows log messages
npm run start:typedi

Sample output:

{"level":30,"time":1669211464742,"pid":69308,"msg":"inside demo-tsyring.ts"}
{"level":30,"time":1669211464743,"pid":69308,"msg":"Service: hello tsyring"}
{"level":30,"time":1669211464743,"pid":69308,"msg":"ChildDependency: hello parent"}
{"level":30,"time":1669211464743,"pid":69308,"msg":"Singleton: hello. I have been called 1 times."}
{"level":30,"time":1669211464743,"pid":69308,"msg":"Singleton: hello. I have been called 2 times."}
{"level":30,"time":1669211464743,"pid":69308,"msg":"Service: env = dev"}

About

Dependency Injection Demo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published