Mandarine.TS
Latest
Latest
  • Mandarine.TS
  • Getting started
    • Installing Deno Runtime
    • Setting up Environment
    • Quick Example
  • Concepts
  • Mandarine Project Structure
  • Mandarine CLI
    • CLI Reference
      • mandarine new
      • mandarine generate
      • mandarine run
  • Mandarine Core
    • Core Initialization
    • Mandarine.json
    • Dot env file
    • Properties
      • The @Value Decorator
    • Components
      • Controller
      • Middleware
      • Repository
      • Component
      • Service
      • Configuration
      • Manual Component
    • Dependency Injection
      • Accessing DI container
    • Resource Handlers
      • Resource Handler Registry
      • Resource Handler
      • Resource Resolver
  • Mandarine MVC
    • Web MVC
    • Controllers
      • Routes
        • Parameterized Routes
      • HTTP Handlers
      • @ResponseStatus
    • Custom Middleware
    • Session Middleware
    • CORS Middleware
    • Template Engine
      • @Render Decorator
      • Accessing data from template
    • Launching web-app
    • Serving Static Content
    • Multipart Form Data
  • Mandarine Security
    • Sessions
  • Mandarine Data
    • ORM
      • Data source
      • Models
      • Repositories
        • Interacting with data
        • Updating data
    • Mandarine Query Language
  • Resources
    • Changelog
  • Plugins
    • Optional
    • Promise Repeater
Powered by GitBook
On this page
  • Concepts
  • Main
  • Usage

Was this helpful?

  1. Mandarine Core
  2. Dependency Injection

Accessing DI container

This page summarizes how to get a injectable object from the DI Container programatically.

PreviousDependency InjectionNextResource Handlers

Last updated 4 years ago

Was this helpful?

Concepts

  • Resolved component

    • A component that has been initialized by the DI Factory, and all dependencies in it are ready to use.

Main

Mandarine's DI Container serves as the main source for your components to ask for dependencies. This means, all the resolved dependencies are found inside the DI container.

Usually, accessing a resolved component is achieved by using injection But some use cases may require something different such as accessing the dependency programmatically.

Accessing a dependency programmatically allows you to connect a non-mandarine-powered application with a Mandarine-powered application. This means, you can mix features such as native javascript/typescript and dependency injection in your application. Even though the use cases for this may vary, it is always recommended to develop Mandarine-powered applications in order to get full access to all its features.

Usage

import { Component, ApplicationContext } from "https://deno.land/x/mandarinets/mod.ts";

@Component()
export class MyComponent {

    public pi() {
        return 3.14;
    }

}

class TSNativeClass {

    public getPiFromDIContainer() {
        return ApplicationContext.getInstance()
                                 .getDIFactory()
                                 .getDependency(MyComponent)
                                 .pi();
    }

}

// Let Mandarine's core resolve dependencies and initialize the DI engine
new MandarineCore();

let pi = new TSNativeClass().getPiFromDIContainer();

console.log(pi);

/**
 * RESULT:
 * 3.14;
 */

In the example above we are looking how a regular typescript class that has no relation with a mandarine-powered class whatsoever is getting the resolved component of the class MyComponent and we can see how it is able to use it & call its properties.

Note that in order to accomplish the shown example, it is necessary to use new MandarineCore() as it is responsible for resolving Mandarine-powered components as well as the needed resources for a Mandarine application or partial Mandarine application to work. For more information about this,

by construction or manual injection.
click here.