Git Product home page Git Product logo

servicemanager's Introduction

๐Ÿ”Œ ServiceManager

build status npm version npm download dependencies coverage status license

Update (July 2022)

DEPRECATED in favor of hex. See https://github.com/eserozvataf/hex for details.

What is the ServiceManager?

ServiceManager is probably the most basic implementation of dependency injection container for JavaScript.

Quick start

Execute npm install servicemanager or yarn add servicemanager to install servicemanager and its dependencies into your project directory.

Usage

Basics (Hooks Way)

To register objects to service manager, create a file/module for your service context:

//
// serviceContext.js
//
import { createContext, factory, singleton } from 'servicemanager';

const context = createContext(
    [ 'ResourceManager', factory(DefaultResourceManager) ],
    [ 'CacheManager', factory(CustomCacheManager) ],
    [ 'SessionManager', singleton(mySessionManager) ]
);

To get objects back from service manager:

//
// anotherFile.js
//
import useServiceManager from 'servicemanager';

// get default service context
const context = useServiceManager();

// returns a new instance for DefaultResourceManager
const resourceManager = context.get('ResourceManager');

// returns a new instance for CustomCacheManager
const cacheManager = context.get('CacheManager');

// returns the same session manager object that referenced by mySessionManager
const sessionManager = context.get('SessionManager');

Alternatively, to get all needed instances at once:

//
// anotherFile2.js
//
import useServiceManager from 'servicemanager';

// get default service context
const context = useServiceManager();

const [ resourceManager, cacheManager, sessionManager ] = context.getRange('ResourceManager', 'CacheManager', 'SessionManager');

...Or, to have them in more promise-friendly way:

//
// anotherFile3.js
//
import useServiceManager from 'servicemanager';

// get default service context
const context = useServiceManager();

context.ensure([ 'ResourceManager', 'CacheManager', 'SessionManager' ], (resourceManager, cacheManager, sessionManager) => {
    // awaits promisified generator functions first,
    // then services dependencies as parameters
});

*** Note: Service names can be anything including objects, symbols or strings.

Basics (Functional Way)

To register objects to service manager, create a file/module for your service context:

//
// serviceContext.js
//
import { createContext, factory, singleton } from 'servicemanager';

const context = createContext(
    [ 'ResourceManager', factory(DefaultResourceManager) ],
    [ 'CacheManager', factory(CustomCacheManager) ],
    [ 'SessionManager', singleton(mySessionManager) ]
);

export {
    context as default,
};

To get objects back from service manager:

//
// anotherFile.js
//
import { get } from 'servicemanager';
import context from './serviceContext.js';

// returns a new instance for DefaultResourceManager
const resourceManager = get(context, 'ResourceManager');

// returns a new instance for CustomCacheManager
const cacheManager = get(context, 'CacheManager');

// returns the same session manager object that referenced by mySessionManager
const sessionManager = get(context, 'SessionManager');

Alternatively, to get all needed instances at once:

//
// anotherFile2.js
//
import { getRange } from 'servicemanager';
import context from './serviceContext.js';

const [ resourceManager, cacheManager, sessionManager ] = getRange(context, 'ResourceManager', 'CacheManager', 'SessionManager');

...Or, to have them in more promise-friendly way:

//
// anotherFile3.js
//
import { ensure } from 'servicemanager';
import context from './serviceContext.js';

ensure(context, [ 'ResourceManager', 'CacheManager', 'SessionManager' ], (resourceManager, cacheManager, sessionManager) => {
    // awaits promisified generator functions first,
    // then services dependencies as parameters
});

*** Note: Service names can be anything including objects, symbols or strings.

API

ServiceContext.prototype methods

constructor(...definitions: ServiceDefinitions)

get(dependency: any): any

getRange(...dependencies: Array<any>): Array<any>

ensure(dependencies: Array<any>, callback: (...services: Array<any>) => any): Promise<any>

all(): Array<string>

filter(predicate: FilterPredicate): Array<string>

filterByTag(tag: string): Array<string>

Mechanics

Factory services call generator/dependency target each time they are requested, whereas, Singleton services are registered when they are defined.

import createContext, { factory, singleton } from 'servicemanager';

const date1 = Symbol('date1');
const date2 = Symbol('date2');

const context = createContext(
    [ date1, factory(() => new Date()) ],
    [ date2, singleton(new Date()) ]
);

console.log(context.get(date1)); // calls and returns new Date()
console.log(context.get(date1)); // calls and returns new Date() again,
console.log(context.get(date2)); // no calls, returns stored date.

Todo List

See GitHub Projects for more.

Requirements

License

Apache 2.0, for further details, please see LICENSE file

Contributing

See contributors.md

It is publicly open for any contribution. Bugfixes, new features and extra modules are welcome.

  • To contribute to code: Fork the repo, push your changes to your fork, and submit a pull request.
  • To report a bug: If something does not work, please report it using GitHub Issues.

To Support

Visit my patreon profile at patreon.com/eserozvataf

servicemanager's People

Contributors

eser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

lgtm-migrator

servicemanager's Issues

Chain dependency construction

Need a new feature for passing current service context to service resolver.

const context = new ServiceContext(container => {
    container.set('mongodb', transient(MongoConnection));
    container.set('userRepository', transient(ctx => new UserRepository(ctx.get('mongodb'))));
});

Add .ensure method

Sample usage:

serviceManager.ensure([ 'stack', 'post' ], (stack, post) {
  stack.add(post);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.