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

How to use it with packages? #7

Open
rottmann opened this issue Apr 17, 2024 · 2 comments
Open

How to use it with packages? #7

rottmann opened this issue Apr 17, 2024 · 2 comments

Comments

@rottmann
Copy link

Example:
package A uses classes from package B.

// package A
import { inject, Scopes } from 'dioma'
import LoggerService from '../../package-b/src/logger'
class SomeClass {
  static scope = Scopes.Singleton()
  constructor(private log = inject(LoggerService)) {
    // ----------------------------^^^^^^^^^^^^^
    // TS2345 Argument of type 'typeof LoggerService' is not assignable to parameter of type 'TokenOrClass'.
  }
}

// package B
import { Scopes } from 'dioma' // this is an other namespace?
export default class LoggerService {
  static scope = Scopes.Singleton()
}

Is it correct to use token-based injections?

Like

// package A - services.js
import { inject, Token, globalContainer } from 'dioma'
import LoggerService from '../../package-b/src/logger'

const services = globalContainer.childContainer('Services')

export const loggerServiceToken = new Token<LoggerService>('LoggerService')
services.register({ token: loggerServiceToken, class: LoggerService })
export const logService = services.inject(loggerServiceToken)

// package A - someclass.js
import { inject, Scopes } from 'dioma'
import LoggerService from './services'
class SomeClass {
  static scope = Scopes.Singleton()
  constructor(private log = inject(loggerServiceToken)) {}
}

Or could it be done easier?

But how could a class of package B inject the Logger of package B?
package B classes did not know the tokens.
Or must I build a wrapper to pass these vars? Sounds really complicated / bloated.

@rottmann
Copy link
Author

Curios, my first example works with 0.4.3, not with 0.4.5.

@zheksoon
Copy link
Owner

Hello!
As far as I understand your problem, you have two packages in a project that can import each other through ../.../.
I haven't really done this setup, if you mind, could you please provide sample repo, or something reproducible?
Many thanks for reporting!

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