项目作者: bertilxi

项目描述 :
Simple Dependency Injection
高级语言: TypeScript
项目地址: git://github.com/bertilxi/Phreatic.git
创建时间: 2018-05-04T18:07:59Z
项目社区:https://github.com/bertilxi/Phreatic

开源协议:MIT License

下载


Phreatic Dependency Injector

In many frameworks across languages, exists the implementation of this well known design pattern, like Spring MVC, Angular, .NET, etc.

Well, the idea of this was not to create just another IoC based dependency injector. The main motivation was the lack of a DI that resolves circular dependencies without messing too much with runtime, reflection, or overcomplicating the solution.

Installation

Greenkeeper badge

  1. npm install --save phreatic
  2. # OR
  3. yarn add phreatic

Objectives

  • Be Awesome.
  • Be super simple.
  • Well tested. Dependency Injection is the base of well a architectured system.

Inspiration

  • Spring framework
  • Angular
  • InversifyJS
  • typescript-ioc

Features

  • Solves circular dependencies.
  • Zero dependencies.
  • Isomorphic/Universal.
  • Inspired by KISS and SOLID principles.
  • Lazy dependency resolution.

Caveats

  • We do not deal with the circular dependencies made by you and your imports. Please avoid doing the following kind of imports A => B => C => A. Just use @Inject("className").

Example

  1. import { Inject, Injectable, get, createInjectable } from "phreatic";
  2. @Injectable
  3. export class User {
  4. public name = "user pepito";
  5. public password;
  6. @Inject("HttpService") public http;
  7. }
  8. export class Role {
  9. public name = "role pepito";
  10. public password;
  11. }
  12. @Injectable
  13. export class HttpService {
  14. public name = "http service";
  15. @Inject("User") public user: User;
  16. @Inject("Role") public role: Role;
  17. }
  18. createInjectable(new Role());
  19. const http = get(HttpService);
  20. const user = get<any>("User");
  21. // the same as
  22. // const http = new HttpService();
  23. // const user = new User();
  24. // Valid uses
  25. console.log(http.role.name);
  26. console.log(http.user.name);
  27. console.log(user.http.user.http.user.http.user.name);
  28. console.log(user.http.user.http.user.http.name);

Development

Just the usual.

  1. # Install deps
  2. yarn
  3. # Change the code
  4. # Build it
  5. yarn build
  6. # Test it
  7. yarn test
  8. # submit your Pull Request or Open an issue