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

Allow toDynamicValue to access the current context #351

Closed
Offirmo opened this issue Aug 29, 2016 · 5 comments
Closed

Allow toDynamicValue to access the current context #351

Offirmo opened this issue Aug 29, 2016 · 5 comments

Comments

@Offirmo
Copy link
Contributor

Offirmo commented Aug 29, 2016

Expected Behavior

Still using Inversify. Works great so far, but I feel something is missing in the range of possible services.

I'm trying to do functional programming, using pure functions and not using classes (no state + considered harmful in javascript)

However, my modules still need injected dependencies.

With inversify, we can declare a range of ressources, from the simplest to the more complex

  • constant value
  • dynamic value
  • class
  • factory

Current Behavior

I'm forced to use a factory:

bind<interfaces.Factory<WeaponComponentModel>>(RSRCIDS.factory)
    .toFactory<WeaponComponentModel>((context: interfaces.Context) => () => module_factory({
        schema: context.kernel.get<IJsonSchema>(RSRCIDS.schema)
    }))

// usage:
const module = context.kernel.get<() => WeaponComponentModel>(RSRCIDS.factory)()

Possible Solution

A simpler "module" service, half-way between dynamic values and factories, but not a class:

bind<interfaces.Module<WeaponComponentModel>>(RSRCIDS.module)
    .toModule<WeaponComponentModel>((context: interfaces.Context) => module_factory({
        schema: context.kernel.get<IJsonSchema>(RSRCIDS.schema)
    }))

// usage:
const module = context.kernel.get<WeaponComponentModel>(RSRCIDS.module)

I'm still using a factory, but calling code doesn't have to be aware of it.

Steps to Reproduce (for bugs)

n/a

Context

trying to do functional programming, using pure functions and not using classes (no state + considered harmful in javascript)

@Offirmo
Copy link
Contributor Author

Offirmo commented Aug 29, 2016

Note: I'm not sure of the "module" terminology. I made it up, but a more experienced programmer may use a more fitting name.

@remojansen
Copy link
Member

remojansen commented Aug 29, 2016

I think we can achieve this in an easier way:

kernel.bind<WeaponComponentModel>(RSRCIDS.module).toDynamicValue((context: interfaces.Context) => {
    return module_factory({
        schema: context.kernel.get<IJsonSchema>(RSRCIDS.schema)
    });
});

// usage:
const module = context.kernel.get<WeaponComponentModel>(RSRCIDS.module);

Notice how we used:

.toDynamicValue((context: interfaces.Context) => { /**/ });

Instead of:

.toDynamicValue(() => { /**/ });

This is not possible at the moment but it is easy to implement 😄 I will try to implement it as soon as I can but might take me over a week because I have too much work at the moment with other stuff...

Also check the recipe for Injecting dependencies into a function.

@remojansen remojansen mentioned this issue Aug 29, 2016
11 tasks
@remojansen
Copy link
Member

This has been implemented by #352 and will be released soon

remojansen added a commit that referenced this issue Aug 29, 2016
* Update interfaces.ts

* Update binding.ts

* Update resolver.ts

* Update inversify.test.ts

* Update resolver.test.ts

* Update binding_to_syntax.test.ts

* Update binding_to_syntax.ts

* Update value_injection.md

* Update binding.ts

* fixed issues with dynamic value injection and context support
@Offirmo
Copy link
Contributor Author

Offirmo commented Aug 30, 2016

This is perfect ! You are working so fast !

@remojansen
Copy link
Member

Thanks! I will try to do another release tonight I'm closing this issue now 😄

@remojansen remojansen changed the title Feature: module Allow toDynamicValue to access the current contex Aug 30, 2016
@remojansen remojansen changed the title Allow toDynamicValue to access the current contex Allow toDynamicValue to access the current context Aug 30, 2016
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