Solid Goggles
- Install
- Import
- Basic Usage
- Decorators
- Methods
- Properties
- Clone Injector
- Factory
- Events
- Symbols
- Injection Hooks
- Overwrite
- Other Models
- Change Dependencies
Install
Import
Browser
<script src="node_modules/solid-goggles/dist/browser.js"></script>
<script src="https://unpkg.com/solid-goggles/dist/browser.js"></script>
Common JS
const Sg = require('solid-goggles');
ES6 Modules
import * as Sg from 'solid-goggles';
Basic Usage
import { Injector } from 'solid-goggles';
class Implementation {
method() {}
}
const injector = Injector.of(Implementation);
class Interface {
method() {}
}
const instance = injector.get(Interface);
Decorators
Connect Dependencies
import { connect } from 'solid-goggles';
class OtherImplementation {}
class Implementation {
@connect(OtherImplementation) property;
constructor(
@connect(OtherImplementation) parameter
) {}
method(@connect(OtherImplementation) parameter) {}
}
import 'reflect-metadata';
import { connect } from 'solid-goggles';
class OtherImplementation {}
class Implementation {
@connect() property: OtherImplementation;
constructor(
@connect() parameter: OtherImplementation
) {}
method(@connect() parameter: OtherImplementation) {}
}
Register Implementation
import { Injector, register } from 'solid-goggles';
class Implementation {}
@register(Implementation)
class Interface {}
const injector = new Injector;
injector.get(Interface);
Implement Interface
import { Injector, implement } from 'solid-goggles';
class Interface {}
@implement(Interface)
class Implementation {}
const injector = new Injector;
injector.get(Interface);
Methods
import { Injector } from 'solid-goggles';
class Interface {}
class Implementation {}
const injector = new Injector;
injector.setImplementation(Implementation);
injector.tryGet(Implementation);
const instance = injector.get(Interface);
injector.set(Interface, instance);
injector.delete(Interface);
injector.link(Interface, Implementation);
injector.tryLink(Interface, Implementation);
injector.factory(Implementation, (implementation, args, injector) => new implementation(...args));
injector.onEvery(Implementation, (instance, next) => next(instance));
injector.onGet(Implementation, (instance, next) => next(instance));
injector.onSet(Implementation, (instance, next) => next(instance));
injector.onDelete(Implementation, (instance, next) => next(instance));
injector.onInstantiate(Implementation, (instance, next) => next(instance));
injector.generate(Implementation);
injector.instantiate(Implementation);
injector.parameters(Implementation);
injector.properties(instance);
injector.methods(instance);
injector.clear();
injector.findInterface(Interface || Implementation || instance);
injector.findImplementation(Interface || Implementation || instance);
injector.findInstance(Interface || Implementation || instance);
injector.canImplement(Interface, Implementation);
injector.getFactory(Implementation);
injector.getCanImplement(Interface, Implementation);
Properties
import { Injector } from 'solid-goggles';
import { Container } from 'solid-goggles';
import { InjectorEmitter } from 'solid-goggles';
const injector = new Injector({
container: new Container,
emitter: new InjectorEmitter,
factories: new Map
});
Clone Injector
const injector = new Injector;
const clone = new Injector(injector);
Factory
import { Injector } from 'solid-goggles';
class Implementation {}
const injector = new Injector;
injector.factory((implementation, args, injector) => new implementation(...args));
injector.factory(Implementation, (implementation, args, injector) => new implementation(...args));
Events
import { Injector } from 'solid-goggles';
class Implementation {}
const injector = new Injector;
injector.onEvery((instance, next) => {
return next(instance);
});
injector.onGet((instance, next) => {
return next(instance);
});
injector.onGet(Implementation, (instance, next) => {
return next(instance);
});
Symbols
import { canImplement, findImplementation, factory, parameters, properties, methods } from 'solid-goggles';
Injection Hooks
Parameters
import { parameters } from 'solid-goggles';
class OtherImplementation {}
class Implementation {
static [parameters]() { return [ OtherImplementation ]; }
constructor(otherInstance) {
this.otherInstance = otherInstance;
}
}
Properties
import { properties } from 'solid-goggles';
class Implementation {
[properties](injector) {
return {
instance: Implementation
};
}
}
Methods
import { methods } from 'solid-goggles';
class Implementation {
[methods](injector) {
return { method: [ Implementation ] };
}
method(instance) {}
}
Registration
import { findImplementation, Injector } from 'solid-goggles';
class Implementation {}
class Interface {
static get [findImplementation]() { return Implementation; }
}
const injector = new Injector;
injector.get(Inteface);
Overwrite
import { Injector, canImplement, factory } from 'solid-goggles';
Injector.baseCanImplement = (interface, implementation, injector) => true;
Injector.baseFactory = (implementation, args, injector) => new implementation(...args);
const injector = new Injector;
injector.baseCanImplement = (interface, implementation, injector) => true;
injector.baseFactory = (implementation, args, injector) => new implementation(...args);
class Interface {
static [canImplement](interface, implementation, injector) {
return true;
}
}
class Implementation {
static [canImplement](interface, implementation, injector) {
return true;
}
static [factory](constructor, args, injector) {
return new constructor(...args);
}
}
Other Models
Container
import { Container } from 'solid-goggles';
class Interface {}
class Implementation {}
const instance = new Implementation;
const container = new Container([
[Interface, Implementation, instance]
]);
container.parent = new Container;
container.getInterface(Interface || Implementation || instance);
container.getImplementation(Interface || Implementation || instance);
container.getInstance(Interface || Implementation || instance);
container.setInterface(Implementation, Interface);
container.setImplementation(Implementation);
container.setInstance(Implementation, instance);
container.deleteInterface(Interface || Implementation || instance);
container.deleteImplementation(Interface || Implementation || instance);
container.deleteInstance(Interface || Implementation || instance);
container.clearInterfaces();
container.clearImplementations();
container.clearInstances();
Injector Emitter
import { InjectorEmitter } from 'solid-goggles';
const emitter = new InjectorEmitter;
class Implementation {}
const instance = new Implementation;
const everyListener = emitter.onEvery(Implementation, (instance, next) => next(instance));
const getListener = emitter.onGet(Implementation, (instance, next) => next(instance));
const setListener = emitter.onSet(Implementation, (instance, next) => next(instance));
const deleteListener = emitter.onDelete(Implementation, (instance, next) => next(instance));
const instantiateListener = emitter.onInstantiate(Implementation, (instance, next) => next(instance));
emitter.emitEvery(Implementation, instance);
emitter.emtiGet(Implementation, instance);
emitter.emitSet(Implementation, instance);
emitter.emitDelete(Implementation, instance);
emitter.emitInstantiate(Implementation, instance);
emitter.removeEvery(Implementation, everyListener);
emitter.removeGet(Implementation, getListener);
emitter.removeSet(Implementation, setListener);
emitter.removeDelete(Implementation, deleteListener);
emitter.removeInstantiate(Implementation, instantiateListener);
Injector Error
import { InjectorError } from 'solid-goggles';
class Interface {}
class Implementation {}
const injectorError = new InjectorError(Interface, Implementation, 'message');
Change Dependencies
import { Injector, Container, InjectorEmitter } from 'solid-goggles';
class OtherContainer implements Container {}
class OtherInjectorEmitter implements InjectorEmitter {}
const injector = new Injector(undefined, OtherContainer, OtherInjectorEmitter);