All Projects → BrianDGLS → hypo-container

BrianDGLS / hypo-container

Licence: MIT license
A dependency injection container.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to hypo-container

Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+16843.75%)
Mutual labels:  ioc, dependency-injection
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+900%)
Mutual labels:  ioc, dependency-injection
Dry Auto inject
Container-agnostic constructor injection mixin
Stars: ✭ 149 (+831.25%)
Mutual labels:  ioc, dependency-injection
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+17156.25%)
Mutual labels:  ioc, dependency-injection
Dry System
Organize your code into reusable components
Stars: ✭ 228 (+1325%)
Mutual labels:  ioc, dependency-injection
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (+743.75%)
Mutual labels:  ioc, dependency-injection
Qframework
Unity3D System Design Architecture
Stars: ✭ 2,326 (+14437.5%)
Mutual labels:  ioc, dependency-injection
Unity
This repository contains all relevant information about Unity Container suit
Stars: ✭ 1,513 (+9356.25%)
Mutual labels:  ioc, dependency-injection
Typedi
Simple yet powerful dependency injection tool for JavaScript and TypeScript.
Stars: ✭ 2,832 (+17600%)
Mutual labels:  ioc, dependency-injection
Testdeck
Object oriented testing
Stars: ✭ 206 (+1187.5%)
Mutual labels:  ioc, dependency-injection
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+968.75%)
Mutual labels:  ioc, dependency-injection
DpdtInject
Highly efficient compile-time general purpose DI container based on C# source generators.
Stars: ✭ 25 (+56.25%)
Mutual labels:  ioc, di-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+731.25%)
Mutual labels:  ioc, dependency-injection
Node Dependency Injection
The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
Stars: ✭ 140 (+775%)
Mutual labels:  ioc, dependency-injection
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+12031.25%)
Mutual labels:  ioc, dependency-injection
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (+837.5%)
Mutual labels:  ioc, dependency-injection
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+7418.75%)
Mutual labels:  ioc, dependency-injection
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (+456.25%)
Mutual labels:  ioc, dependency-injection
Awilix
Extremely powerful Inversion of Control (IoC) container for Node.JS
Stars: ✭ 2,269 (+14081.25%)
Mutual labels:  ioc, dependency-injection
DI-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Stars: ✭ 62 (+287.5%)
Mutual labels:  ioc, dependency-injection

hypo-container

A dependency injection container. Supports NodeJS and browser development.

Install

NPM:

npm install --save hypo-container

Yarn:

yarn add hypo-container

Usage

To create a container create a new instance of the Container class.

import { Container } from 'hypo-container'

const container = new Container()

A container has two types of dependencies. These are known as services and parameters.

Defining a service

A service is an object that does something as part of a larger system. Examples of services: a database connection, a templating engine, or a mailer. Almost any global object can be a service.

Services are defined using a callback function that returns an instance of an object.

const container = new Container()

container.register('owner', c => {
  return new Person('Brian', 26)
})

container.register('cat', c => {
  return new Cat('Garfield', 4, c.get('owner'))
})

Alternatively calls to the register() method can be chained, as demonstrated below.

container
  .register('owner', c => new Person('Brian', 26))
  .register('cat', c => new Cat('Garfield', 4, c.get('owner')))

Note that the callback has access to the current container's instance, via it's first argument. This allows you to reference other contained services and parameters when defining a new service.

Objects are created only when they are first accessed via the get() method, so order is not important. Using a defined service is very easy as demonstrated below.

const cat = container.get('cat')

The above is roughly equivalent to the following.

const cat = new Cat('Garfield', 4, new Person('Brian', 26))

Defining a factory service

By default the same instance of a service is returned when calling the container's get() method. If you want to return a new instance of the service you can make use of the container's factory() method as demonstrated below.

container.register('uniqueRobot', container.factory(c => {
  return new Robot()
}))

const uniqueRobot = container.get('uniqueRobot')

Defining parameters

Defining a parameter allows for easy configuration of your container from the outside and to store global values.

container.myName = 'Brian'
container.myAge = 26

container.register('me', c => {
  return new Person(c.myName, c.myAge)
})

Protecting parameters

To use an anonymous function to define a parameter use the protect() container method.

container['prop'] = 42

container.register('protected_prop', container.protect(c => {
  return c.prop * 2
}))

Update a registered service

In some cases you may want to modify a service definition after it has been defined. You can use the extend() method to define additional code to be run on your service just after it is created.

container.register('cat', c => {
  return new Cat('Garfield', 4, c.get('owner'))
})

container.extend('cat', (storage, c) => {
  storage.details = () => `${storage.name} - ${storage.age}`
  return storage
})

The first argument is the name of the service to extend, the second a function that gets access to the object instance and the container.

Extend a container

If you use the same libraries over and over, you might want to reuse some services from one project to the next one. You can easily extend a container by registering it to another.

const app = new Container()

app['name'] = 'My Awesome App'

const serviceContainer = new Container()

serviceContainer.register('magicNumberService', c => {
  return new MagicNumber()
})

app.register(serviceContainer)

app.get('magicNumberService')

Get the service creation function

When accessing a service, the container automatically calls the function used to supply the service. This creates an instance of that service. If you want to get access to this function, use the raw() method.

container.register('robot', c => new Robot())

// wraps previously defined robot method to create a factory method
container.register('uniqueRobot', container.factory(c => c.raw('robot')))

Deleting a registered service

You may want to delete a service at some point. To do this you use the delete() method. Supply the name of a registered service as the first argument in order to delete the service.

container.delete('cat')

Deleting all services

To delete all services call the deleteAll() method.

container.deleteAll()

Deleting a parameter

As parameters are set directly on the container object they can be deleted using the built in delete keyword.

container.myNumber = 0987654321

delete container.myNumber

Thanks

Special thanks to the creators and maintainers of Pimple. Hypo attempts to follow the Pimple api for ease of use and familiarity. The Hypo docs also attempt to match that of the Pimple docs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].