Skip to content

adamcarr/forge-di-smithy

Repository files navigation

Smithy Build Status

An extension library to nkohari/forge inversion of control framework.

Table of contents

Intent

The intent of this framework is to provide a set of useful extensions on forge by allowing a user to register tools within an equipment object. The Blacksmith class takes a dependency on a forge instance and will read in equipment instances to add configured bindings.

This should allow you to separate your binding mappings and organize them as desired.

Getting started

You can install Smithy from npm:

$ npm install forge-di-smithy

Components

Equipment

An array of tools that can be ready by the Blacksmith. blacksmith.registerEquipment(equipment);

Blacksmith

The main container that will wrap a forge instance and register equipment to bindings on the forge instance. new Smithy.Blacksmith(forge);

Tools

A tool is a mapping for a binding. There are 3 different types of tools. Each tool requires an options argument to be provided. This options object follows the following contract.

{
  name: string;
  target: T;
  lifecycle?: Lifecycle;
  when?: Forge.IPredicate;
  hint?: string;
  bindingArguments?: Forge.IBindingArguments;
}

Type Tool

The type tool is a mapping to the forge.bind(...).to.type(...); registration.

Function Tool

The function tool is a mapping to the forge.bind(...).to.function(...); registration.

Instance Tool

The instance tool is a mapping to the forge.bind(...).to.instance(...); registration.

Example

Here is a brief example. Best example can be found by going to specs/lib/

TypeScript

import Forge = require('forge-di');
import Smithy = require('forge-di-smithy');

class Foo { }
class Foo2 extends Foo { }
class Bar {
  constructor(foo: Foo) {...}
}
class Blah {
  constructor(public dependency: Foo) {
    "dependency->foo2";
  }
}

var equipment: Smithy.IEquipment = [
  new Smithy.Tools.Type({name: 'foo', target: Foo}),
  new Smithy.Tools.Type({name:'foo2', target: Foo2}),
  new Smithy.Tools.Type({name: 'bar', target: Bar}),
  new Smithy.Tools.Type({name: 'blah', target: Blah})
];

...

var forge = new Forge();
var blacksmith = new Blacksmith(forge);

blacksmith.registerEquipment(equipment);

...

var boo = forge.get('foo');
var blah = forge.get('blah');

expect(boo).to.be.an.instanceOf(Foo);
expect(blah).to.be.an.instanceOf(Blah);
expect(blah.dependency).to.be.an.instanceOf(Foo2);

JavaScript

var Forge = require('forge-di');
var Smithy = require('forge-di-smithy');

function Foo () { }
function Foo2 () { }
function Bar (foo) {
  this.foo = foo;
}
function Blah (dependency) {
  "dependency->foo2";
  this.dependency = dependency;
}

var equipment: Smithy.IEquipment = [
  new Smithy.Tools.Type({name: 'foo', target: Foo}),
  new Smithy.Tools.Type({name:'foo2', target: Foo2}),
  new Smithy.Tools.Type({name: 'bar', target: Bar}),
  new Smithy.Tools.Type({name: 'blah', target: Blah})
];

...

var forge = new Forge();
var blacksmith = new Blacksmith(forge);

blacksmith.registerEquipment(equipment);

...

var boo = forge.get('foo');
var blah = forge.get('blah');

expect(boo).to.be.an.instanceOf(Foo);
expect(blah).to.be.an.instanceOf(Blah);
expect(blah.dependency).to.be.an.instanceOf(Foo2);

Tasks

  • Refactor overloads for tools into an options argument for named arguments
  • Add ability to configure a tool to auto register a func resolver based on the current binding name + 'Func' for factory.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published