🌀
type-chef-di Docs
GitHub
  • 👋Welcome to type-chef-di documentation
  • Overview
    • ✨Getting started
  • Fundamentals
    • 🍎Container Options, extendability
    • 👨‍🍳Injection
      • 🍕Type injection
      • 🍪Token registration
      • ☕Token Injection
      • 🌮Mixed injection
    • 🛠️Decorators
      • ✅@Injectable({instantiation: "singleton" | "prototype"})
      • 🎯@Inject<T>(key: string | Type<T>)
      • 🍍@InjectProperty<T>(key: string | Type<T>)
      • 🍭@Setter()
      • 🔆@InitMethod()
      • 🏭@FactoryMethod()
      • ⚡@AddTags(tags)
      • 🪃@RunBefore(key: string | Type<IRunBefore>)
      • 🛴@RunAfter(key: string | Type<IRunAfter>)
      • 🌕@MethodWrapper(key: string | Type<IMethodWrapper>)
  • Use Cases
    • 🎨Plans
    • 🖥️For Developers
  • Other
    • 💡DI / IoC
Powered by GitBook
On this page
Edit on GitHub
  1. Fundamentals
  2. Injection

Token registration

register, registerTypes, asSingleton, asFactory

Value can be a class definition, primitive, or a function.

container.register('key', value)

New Instance

container.register('key2', value).asPrototype()

Use the same instance (default)

container.register('key3', value).asSingleton()

Register with context: @Inject('paramKey') ... it will replace injection keys (this enables to create different variants)

container.register('key4', value)
    .asSingleton()
    .withContext({'paramKey': 'otherKey', 'paramKey2': 'otherKey2'})

Register constant (can be primitive, function, etc)

container.register('key4', 'some constant').asConstant()

Registration by type with a random key (if you don't want to enable auto-creation you can still use this)

container.registerTypes([MyClass, Myclass2])

Factory

container.register('factoryKey', FactoryClass).asFactory();
container.register('factoryResult', String).asFactoryResult('factoryKey');
PreviousType injectionNextToken Injection

Last updated 2 years ago

👨‍🍳
🍪