Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel modules #79

Closed
ktutnik opened this issue Feb 25, 2016 · 2 comments
Closed

Kernel modules #79

ktutnik opened this issue Feb 25, 2016 · 2 comments

Comments

@ktutnik
Copy link
Member

ktutnik commented Feb 25, 2016

Sometime there is a logic on registering Types:

if(config.mode == "easy"){
   kernel.bind(new TypeBinding<MyType>("MyType", EasyMyType);
   kernel.bind(new TypeBinding<MyOtherType>("MyType", EasyMyOtherType);
}
else {
   kernel.bind(new TypeBinding<MyType>("MyType", HardMyType);
   kernel.bind(new TypeBinding<MyOtherType>("MyType", HardMyOtherType);
}

You don't want to repeat those logic on the Unit Test and in your apps. even you need to unit test those logic.

will be better if Inversify can provide a predefined (template) registration. in Castle Windsor its called Installer

@remojansen
Copy link
Member

I need to think about this but I think this would be possible thanks to the parent Kernel feature in the roadmap:

var kernel: kernel = null;
if(config.mode == "easy"){
   kernel = new Kernel(easyKernel);
}
else {
   kernel = new Kernel(hardKernel);
}

The Kernel chain will behave in a similar way to the JavaScript prototype chain. If a binding cannot be resolved the kernel will search in the kernel chain. This idea was inspired by child injectors in Angular DI.

@remojansen remojansen added this to the 2.0 milestone Feb 25, 2016
@remojansen remojansen changed the title Predefined (template) registration Kernel modules Feb 25, 2016
@remojansen remojansen modified the milestones: 2.0.0-alpha.1, 2.0 Feb 26, 2016
remojansen added a commit that referenced this issue Mar 5, 2016
@remojansen
Copy link
Member

This is now done by 052dcaf

At the end I decided to leave our the parent kernel feature, we can still add it in the future but I see small real-world use cases so I decided to leave it out until some people ask for it (A.K.A. KISS).

The way it works is the following:

let someModule: IKernelModule = (kernel: IKernel) => {
    kernel.bind<INinja>("INinja").to(Ninja);
    kernel.bind<IKatana>("IKatana").to(Katana);
    kernel.bind<IShuriken>("IShuriken").to(Shuriken).inSingletonScope();
};

let options: IKernelOptions = {
    modules: [
        someModule
    ]
};

let kernel = new Kernel(options);
let ninja = kernel.get<INinja>("INinja");
console.log(ninja2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants