🌀
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
  • enableAutoCreate
  • Initializers
Edit on GitHub
  1. Fundamentals

Container Options, extendability

You can configure the Container with

{
    enableAutoCreate: boolean; // if dependency does not exist in the container, create it and register
    initializers?: Type<IInitializer>[]; // runs after instatnitation 
}

enableAutoCreate

When you call container.resolve / resolveByType etc. It will try to resolve with the registered dependencies and if the required dependency is not registered (.register, .registerByType..) it will throw an error, BUT you have the option to register types when resolution is happening (no need for registration). It will resolve Types automatically with the use of the @Injectable({instantiation: "singleton" | "prototype"}) decorator. Basically, it's like a strict mode you can choose.

Initializers

Initializers run after every instantiation. The container has some default initializatizers eg. @MethodWrapper(key: string | Type<IMethodWrapper>), @RunBefore(key: string | Type<IRunBefore>), @InitMethod() etc. You can create your own just implement the IInitializer interface:

class StatusInitializer implements IInitializer {
    constructor(readonly resolver: IResolver) {
    }

    run(resolvedInstance: any, definition: IBaseDefinition): Promise<any> {
        resolvedInstance.status = "your change";
        return resolvedInstance;
    }
}

Then add to the container:

new Container({
    initializers: [StatusInitializer]
})
PreviousGetting startedNextInjection

Last updated 2 years ago

🍎