🌀
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. Decorators

@Injectable({instantiation: "singleton" | "prototype"})

@Injectable()
class Something {

    public getValue() {
        return "more";
    }
}

@Injectable()
class SomethingElse {
    constructor(private readonly something: Something, private readonly str: string = "pizza") {
    }

    public getValue() {
        return `need ${this.something.getValue()} ${this.str}`;
    }
}

@Injectable()
class Client {
    constructor(private readonly something: Something, private readonly somethingElse: SomethingElse) {
    }

    public say() {
        return `I ${this.somethingElse.getValue()} and ${this.something.getValue()} coffee.`;
    }
}

@Injectable()
class Service {
    constructor(private readonly client: Client) {
    }

    public check() {
        return `client says: ${this.client.say()}`;
    }
}

// create the container
const container = new Container({enableAutoCreate: true});

// resolve without registration any @Injectable class
const service = await container.resolveByType<Service>(Service);

console.log(service.check());  // it will be: "client says: I need more pizza and more coffee."
PreviousDecoratorsNext@Inject<T>(key: string | Type<T>)

Last updated 2 years ago

🛠️
✅