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

Per-module resolve config using loadModules #34

Closed
dgrubelic opened this issue Aug 7, 2017 · 1 comment
Closed

Per-module resolve config using loadModules #34

dgrubelic opened this issue Aug 7, 2017 · 1 comment
Assignees

Comments

@dgrubelic
Copy link

Hey there,

i love your library. Great work! :)

I don't know is something like this is possible (but it would be great, maybe i just missed it in docs) to do when using loadModules so you can setup lifetime, resolutionMode or register per imported module.

For example, something like this:

class FooService {
  // Either this solution
  static resolveConfig() {
    return {
      lifetime: awilix.Lifetime.SINGLETON,
      resolutionMode: awilix.ResolutionMode.CLASSIC
    }
  }

  barMethod() {}
}

// ... or this solution
// (probably this one because it can be applied on both functions and classes)
FooService.lifetime = awilix.Lifetime.SINGLETON;
FooService.resolutionMode: awilix.ResolutionMode.CLASSIC;

This way you would have total flexibility to setup imports when using automatic module loading.

@jeffijoe
Copy link
Owner

jeffijoe commented Aug 7, 2017

Thank you for the kind words! 😄

You're not the first one to request this, so maybe it's worth implementing.

The counter argument though, is that now you're introducing a hard dependency on Awilix in userland code, which is pretty ironic for a DI container if you ask me; your services should not know about any DI framework, but I guess in the end it's up to the consumer to draw that line.

Now, we need to make sure we don't hog keys that may be ambiguous with something you might otherwise use for your own code, so maybe using a symbol is a good approach.

My proposed API:

import { Lifetime, ResolutionMode, REGISTRATION } from 'awilix'

// Classes
export default class FooService {
  static [REGISTRATION] = {
    // All of these are optional
    lifetime: Lifetime.SCOPED,
    resolutionMode: ResolutionMode.CLASSIC,
    register: asClass
  }
}

// Functions
export default function makeFooService {
  // ...
}

makeFooService[REGISTRATION] = {
  // All of these are optional
  lifetime: Lifetime.TRANSIENT,
  resolutionMode: ResolutionMode.PROXY,
  register: asFunction
}

What do you think?

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