1.0.16 • Published 5 years ago

conditional-injector v1.0.16

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

conditional-injector

npm version Build Status Maintainability Greenkeeper badge codecov

It's a mix of dependency injection and factory method. It gives you an instance of one subclasses of a class based on an optional predicate.

    import {Container, Injectable, Scope} from "conditional-injector";
        
    class ParentClass {
        public argumentReceived: string = "";
        constructor(argument: string) {
            this.argumentReceived = argument;
        }
    };
    
    @Injectable({predicate: (argument: string) => argument == "first"})
    class FirstSubClass extends ParentClass {
        constructor(argument: string) {
            super(argument)
        }
    }
    
    @Injectable({predicate: (argument: string) => argument == "second"})
    class SecondSubClass extends ParentClass {
        constructor(argument: string) {
            super(argument)
        }
    }
    
    console.log(Container.subclassesOf(ParentClass).create("first").argumentReceived); //"first"
    console.log(Container.subclassesOf(ParentClass).create("second").argumentReceived); //"second" 

'predicate' value it's not required and the class will be instantiated every time that no other 'predicate' is satisfied. It fits like a glove to achieve NullObject implementation:

    class NoPredicateParentClass {}
    
    @Injectable()
    class NoPredicateSubClass extends NoPredicateParentClass {
        constructor() {
            super();
            console.log("NoPredicateSubClass was instantiated");
        }
    }
    
    @Injectable({predicate: () => false})
    class UnsatisfiedPredicateSubClass extends NoPredicateParentClass {}
    
    Container.subclassesOf(NoPredicateParentClass).create(); // "NoPredicateSubClass was instantiated"

Other than this, you can define instantiation scope. You can get a new instance in every Request or use the same instance throughout the entire Application.

    class ScopeParentClass {
        public scopeDemonstrationAttribute = "originalValue";
    }
    
    @Injectable({scope: Scope.Request, predicate: (value) => value.name == "request"})
    class RequestScopeSubClass extends ScopeParentClass {}
    
    Container.subclassesOf(ScopeParentClass).create({name: "request"}).scopeDemonstrationAttribute = "requestValue";
    console.log(Container.subclassesOf(ScopeParentClass).create({name: "request"}).scopeDemonstrationAttribute); // "originalValue"
    
    @Injectable({scope: Scope.Application, predicate: (value) => value.name == "application"})
    class ApplicationScopeSubClass extends ScopeParentClass {}
    
    Container.subclassesOf(ScopeParentClass).create({name: "application"}).scopeDemonstrationAttribute = "applicationValue";
    console.log(Container.subclassesOf(ScopeParentClass).create({name: "application"}).scopeDemonstrationAttribute); // "applicationValue"

If you want to, you can get every subclass instance of a given class: It includes those with no predicate.

    Container.subclassesOf(ScopeParentClass)
             .createAll()
             .map(scopeSubclass => console.log(scopeSubclass.scopeDemonstrationAttribute));
     // "originalValue" 
     // "applicationValue"
Notes:
  • You need to enable typescript flags 'experimentalDecorators' and 'emitDecoratorMetadata'.
  • Due to one decoration property, if a decorated class never gets imported, the decoration function never gets called. Therefore the class never gets registered to the container. You have to explicitly import the file that contains the class at least one time in order to make sure that that class is able to be injected. That's the reason for having a script. Although it's not mandatory, it makes your life easier creating an auto-generated source file from this script. You can use it like this:
    generate-injectables-list.sh src/injectable-files-list.ts src

And then using the auto-generated file src/injectable-files-list.ts in your project.

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago