Git Product home page Git Product logo

injex's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ”ญ Iโ€™m currently working on Injex - a dependency injection framework for TypeScript applications
  • ๐Ÿ’ฌ Ask me about JavaScript, TypeScript, Node JS, React, CSS and basically anything webish

profile for udidu at Stack Overflow, Q&A for professional and enthusiast programmers

injex's People

Contributors

dependabot[bot] avatar lgtm-com[bot] avatar roeibh avatar uditalias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

injex's Issues

Question/Help needed: Basic setup

Hi @uditalias,
First of all, great work, I really like the idea of auto-scanning all the classes/modules annotated with @define.

I've created this small demo https://github.com/flowck/injex-demo where I attempt to implement injex/node, but I can't make it work, and the run() method from the bootstrap class doesn't quite run. I probably made a basic mistake somewhere but I can't quite figure it out.

When I try to run the application after being compiled, I get this error:

Screenshot 2021-03-15 at 21 55 22

Sorry for this issue.

Kind regards,
Firmino.

Feature: Add the container instance as a dependency of itself

There should be an option to inject the container into itself, so a module can require it using the @inject() decorator.

For example:

@define()
export class MyModule {
    // injecting the runtime container
    @inject() private $injex: Injex;

    public addItem(item, name) {
        // using the runtime container API
        this.$injex.addObject(item, name);
    }
}

Today, when creating a runtime container, its reference is not accessible from the ecosystem of Injex.

Parameter Decorator alternative for `@inject`

Is your feature request related to a problem? Please describe.

Wondering if it were possible in this library to do parameter dependency injection into the constructor instead of property dependency injection. Various articles seem to be against the latter though I am learning DI at the moment so I have no idea what the correct answer is.

Describe the solution you'd like

I have not tested this, but I ended up writing a small wrapper to implement this.

// src/lib/runtime/injex.ts
export const injectParam = (shadowedKey: string, dependencyNameOrType?: any) => {
  return function (target: any, _key: string | symbol, _index: number) {
    // _key is `undefined` in the constructor, I think, and _index doesn't
    // seem to be useful in guessing the value of `shadowedKey`
    inject(dependencyNameOrType)(target.prototype, shadowedKey);
  };
}

// Usage
import { Dependency } from "somewhere/in/my/project"

export class SomeService {
  constructor(
    // Here `dep` is required as I couldn't find a way to 
    // get the name of the paramter in the constructor using 
    // `reflect-metadata`
    @injectParam("dep", Dependency) private dep: Dependency) {}
  
  public someMethod() {
    const someResult = this.dep.someResource();
    // ...
  }
}

It's not great as two function parameters in the decorator go unused, but I'm sure someone more qualified can come up with a solution to that - maybe even detect the parameter names in the constructor.

Describe alternatives you've considered
InversifyJS includes the desired functionality with the caveat that it requires explicit registration of dependencies which I am too lazy to consider (I am only building a small app at the moment, no need for overkill, methinks).

Additional context
What prompted this feature request:

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.