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

prismy-di #5

Open
Rokt33r opened this issue Aug 6, 2019 · 1 comment
Open

prismy-di #5

Rokt33r opened this issue Aug 6, 2019 · 1 comment

Comments

@Rokt33r
Copy link
Member

Rokt33r commented Aug 6, 2019

Implement dependency injection decorator

import { BaseInject, createInjector } from 'prismy-di'

const PRODUCTION = process.env.NODE_ENV === 'production'

const ServiceTypes = {
  MailService: 'MailService',
  NotificationService: 'NotificationService'
}

abstract class MailService {
}

class LiveMailService implements MailService {
}

class MockMailService implements MailService {
}

class NotificationService {
  constructor(@BaseInject(ServiceTypes.MailService) mailer: MailService) {
    ...
  }
}

const container = {
  [ServiceTypes.MailService]: PRODUCTION ? LiveMailService : MockMailService,
  [ServiceTypes.NotificationService]: NotificationService
}

const {
  Inject,
  injector
} = createInjector(container)

class MyHandler {
  handle(@Inject(ServiceTypes.NotificationService) notifier: NotificationService) {
    ...
    // Or
    const notifier = injector.inject(ServiceTypes.NotificationService)
    // Or
    const notifier = injector.instantiate(NotificationService)
  }
}
@Rokt33r Rokt33r added this to 🌊Next Up in Make Prismy land great! Aug 6, 2019
@Rokt33r
Copy link
Member Author

Rokt33r commented Aug 14, 2019

Maybe we don't need decorators.

See https://github.com/jeffijoe/awilix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Make Prismy land great!
  
🌊Next Up
Development

No branches or pull requests

1 participant