InversifyJS README

The project's README from github.com/inversify/InversifyJS. As one markdown file: README.md.

[!NOTE]
InversifyJS will be moving to the monorepo. Please, consider creating issues and pull requests there.

Next releases will be published in the monorepo, so consider starring it to be notified.

NPM version NPM Downloads Docs Codecov

GitHub stars Discord Server

InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.

📕 Documentation

Documentation is available at https://inversify.io

About

InversifyJS is a lightweight inversion of control (IoC) container for TypeScript and JavaScript apps. An IoC container uses a class constructor to identify and inject its dependencies. InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices.

Motivation

JavaScript now supports object oriented (OO) programming with class based inheritance. These features are great but the truth is that they are also dangerous.

We need a good OO design (SOLID, Composite Reuse, etc.) to protect ourselves from these threats. The problem is that OO design is difficult and that is exactly why we created InversifyJS.

InversifyJS is a tool that helps JavaScript developers write code with good OO design.

Philosophy

InversifyJS has been developed with 4 main goals:

  1. Allow JavaScript developers to write code that adheres to the SOLID principles.

  2. Facilitate and encourage the adherence to the best OOP and IoC practices.

  3. Add as little runtime overhead as possible.

  4. Provide a state of the art development experience.

Testimonies

Nate Kohari - Author of Ninject

"Nice work! I've taken a couple shots at creating DI frameworks for JavaScript and TypeScript, but the lack of RTTI really hinders things. The ES7 metadata gets us part of the way there (as you've discovered). Keep up the great work!"

Michel Weststrate - Author of MobX

Dependency injection like InversifyJS works nicely

Some companies using InversifyJS

Acknowledgements

Thanks a lot to all the contributors, all the developers out there using InversifyJS and all those that help us to spread the word by sharing content about InversifyJS online. Without your feedback and support this project would not be possible.

License

License under the MIT License (MIT)

Copyright © 2015-2017 Remo H. Jansen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

InversifyJS (inversify/InversifyJS)

Searching Database…

这是什么?InversifyJS 是一个面向 TypeScript 和 JavaScript 应用的轻量级控制反转(IoC)容器——一个决定你的类如何获取所依赖对象的库。
IoC 容器实际是干什么的?它通过类的构造函数来识别并注入该类的依赖,也就是说,类所需的对象是由外部交给它的,而不是自己在内部创建。这个 API 的设计目标就是引导大家遵循最佳的面向对象(OOP)和 IoC 实践。
怎么把它加到我的项目里?从 npm 安装 inversify 包,然后按照 README 中链接的文档网站去声明绑定关系并解析类。该项目同时提供 CommonJS 和 ESM 两种构建版本。
必须用 TypeScript 吗,还是纯 JavaScript 也能用?它面向 JavaScript 和 Node.js 应用,底层用 TypeScript 编写,所以纯 JavaScript 也能用,但用 TypeScript 能发挥最大价值。它是一个应用内库——不需要部署任何服务器或额外的运行时。
在深入了解之前,这个仓库有什么需要知道的吗?有——该项目正在迁移到 inversify monorepo:新的 issue、pull request 和版本发布都会在那里进行,所以请关注 github.com/inversify/monorepo,而不是这个仓库。
它是给框架开发者用的,还是给普通应用开发者用的?面向普通 JavaScript 开发者:README 把它定位为一个帮助他们用良好的面向对象设计写代码的工具,API 友好,运行时开销也很小。
可以免费使用吗,包括商业用途?可以。它采用 MIT 许可证——可以免费使用、复制、修改、合并、发布和再授权,没有任何付费档位。
为什么要做一个 DI 容器,而不是在需要的地方直接实例化类?因为 JavaScript 里基于类的面向对象编程很容易写坏:README 认为,如果没有良好的面向对象设计(SOLID、组合复用),这些特性反而很危险。而且面向对象设计本身就很难,所以作者们创建了 InversifyJS 来提供帮助。
作者们对这个库的优化目标是什么?四个目标:帮助开发者编写符合 SOLID 原则的代码,引导大家遵循最佳的 OOP 和 IoC 实践,尽可能减少运行时开销,以及提供一流的的开发体验。
它经过实战检验吗,还是只是个业余项目?它从 2015 年就存在了(版权 2015-2017 归创建者 Remo H. Jansen 所有),在 GitHub 上约有 12,000 颗星,README 还列出了在生产环境中使用它的公司,不过这些公司只以标志形式出现,没有具体名字。
其他框架作者怎么评价它?Ninject DI 框架的作者 Nate Kohari 称赞了它的 ES7 元数据方案,认为它巧妙地绕过了 JavaScript 缺乏 RTTI 的问题;MobX 的创建者 Michel Weststrate 也写道,像 InversifyJS 这样的依赖注入用起来很顺手。
Creator
inversify
Size
3.4 MB
Stars
12.1K
Age
11y
Commits · 90 Days