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

A way to ask container to not instantiate a class and rather return it as is #2

Open
renatoargh opened this issue May 23, 2018 · 1 comment

Comments

@renatoargh
Copy link

Hi Slava, thank you for the great work.

Is there a way to ask to the container to not instantiate the class but to return it as is? For example;

class StrategyA { }
container.add('myStrategy', StrategyA)

// later...

const Strategy = container.get('myStrategy')
const strategy = new Strategy()
strategy.doThings()

Due to previous very bad design I will need the class itself instead of an instance of the class. Is that possible? Currently I have this ugly workaround;

class StrategyA { }
container.add('myStrategy', { class: StrategyA })

// later...

const Strategy = container.get('myStrategy').class
const strategy = new Strategy()
strategy.doThings()
@slavahatnuke
Copy link
Owner

@renatoargh

Seems you need a factory for your service.

class StrategyA { }
container.add('MyStrategy', function() {return StrategyA})
const MyStrategy = container.get('MyStrategy')
const myStrategy = new MyStrategy()

sorry for late response.

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

No branches or pull requests

2 participants