Small framework on top of InversifyJS to enable component based dependency injection. Each component describes its interfaces and bindings with the help of descriptors, and never accesses the dependency injection container directly. This enables you to:
- Develop loosely coupled and independent components, without exploiting your whole dependency injection container
- Enable / disable / mock whole components in your application
- Use scoped child containers, for example bind dependencies only to a http request
- Implement the extension point pattern to "plug-in" extensions of components from other components
Install inversify-components and set it as an dependency in your local package.json:
npm i --save inversify-components
- Create the inversify-components container:
import{ContainerImpl}from"inversify-components";letcontainer=newContainerImpl();// Also supports options:// let container = new ContainerImpl({ defaultScope: "Singleton" });- Create your main application, which acts as the composition root:
import{MainApplication}from"inversify-components";classAppimplementsMainApplication{execute(container: Container){// Start your application using the container!}}- Register all components (see below) you would like to use:
import{descriptor}from"my-component-descriptor";container.componentRegistry.addFromDescriptor(descriptor);- Register all bindings of all registered component descriptors
container.componentRegistry.autobind(container.inversifyInstance);- Optional: Configure some of your components:
container.componentRegistry.lookup(nameOfComponent).addConfiguration({configurationKey: "configurationValue"});- Start your application
container.setMainApplication(newApp());container.runMain();inversify-components allows you to basically split your applications into independent components. To achieve this, each component exports
a component descriptor:
import{ComponentDescriptor}from"inversify-components";exportconstdescriptor: ComponentDescriptor={name: "name-of-component",// This must be unique for all registered componentsbindings: {root: (bindService,lookupService)=>{// Binding of services is very similar to inversifyJS:bindService.bindGlobalService<TypeOfService>("service-name").to(MyServiceClass);// MyServiceClass is now bound to "name-of-component:service-name" and available in all other components.}}};Notice that each binding gets the unique component name as a prefix. This guarantees that there are not duplicate service bindings across all registered components.
You are also able to grab the inversify coontainer in a ComponentDescriptor:
import{ComponentDescriptor}from"inversify-components";exportconstdescriptor: ComponentDescriptor={name: "name-of-component",// This must be unique for all registered componentsbindings: {root: (bindService,lookupService,inversifyContainer)=>{// Unbind something..inversifyContainer.unbind("service");bindService.bindGlobalService<TypeOfService>("service-name").to(MyServiceClass);}}};So if needed, you are always in full control inside your dependency descriptors.
The above component descriptor executes bindings for the root scope. This is the default scope for inversify-components, which
is executed automatically at autobind. But you could also register bindings for a specific scope, and execute this scope
at application runtime to a specific point in time:
import{ComponentDescriptor}from"inversify-components";exportconstdescriptor: ComponentDescriptor={name: "name-of-component",bindings: {root: (bindService,lookupService)=>{bindService.bindGlobalService<TypeOfService>("service-name").to(MyServiceClass);}request: (bindService,lookupService)=>{// Is not available at application start, but as soon as you open your "request" scope:bindService.bindGlobalService<TypeOfService>("current-session").toDynamicValue(....);}}};// In your MainApplication / App, as soon as you would like to open the above "request" scope:// 1) Create inversify child containerletscopedRequestContainer=container.inversifyInstance.createChild();// 2) Possibly bind some dependent values to this container, e. g. the current request headers and body:scopedRequestContainer.bind("request-body").toConstantValue(currentRequestBody);// 3) Execute scoped "request" bindings in this containercontainer.componentRegistry.autobind(scopedRequestContainer,[],"request");// 4) Go on in your compoisiton root with child containerscopedRequestContainer.get(...)// Maybe your request handler?To enable plugging into your component, you can define extension points. This is done using symbols.
Component A: The component which owns the extension point and wants to load plugins:
import{ComponentDescriptor}from"inversify-components";import{injectable,multiInject,optional}from"inversify";constmyExtensionPoints={"firstExtensionPoint": Symbol("first-extension-point")}exportconstdescriptor: ComponentDescriptor={name: "component-a",// Register all available extension pointsinterfaces: myExtensionPoints};
@injectable()classClassUsingPlugins{// Now you can just inject all plugins registered at firstExtensionPoint and use them:constructor(@optional() @multiInject(myExtensionPoints.firstExtensionPoint)plugins){this.plugins=plugins;}}Component B: The component which adds a plugin to extension point firstExtensionPoint:
exportconstdescriptor: ComponentDescriptor={name: "component-b",bindings: {root: (bindService,lookupService)=>{letextensionPoint=lookupService.lookup("component-a").getInterface("firstExtensionPoint");bindService.bindExtension<ExtensionType>(extensionPoint).to(MyPluginClass);}}};The basic style of configuring components is described in this gist. This style enables you to define default and required configurations without hassle.
You can set a default configuration for your component by adding it to your descriptor:
constconfiguration: Configuration.Default={"configurationKey": "configurationValue";};exportconstdescriptor: ComponentDescriptor<Configuration.Default>={name: "my-component-name",defaultConfiguration: configuration}In all of your classes, you can inject your component meta data, which includes the components configuration:
import{inject,injectable}from"inversify";import{Component}from"inversify-components";
@injectable()classMyClass{constrcutor(@inject("meta:component//my-component-name")component: Component<Configuration.Runtime>)this.configuration=this.component.configuration;}}