npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dependi

v0.5.4

Published

A dependency injection framework for ioc and not service location in in javascript

Downloads

2

Readme

Dependi

A simple dependency container which allows for construction injection, meaning you can have your cake and eat it.

Warning

This is a prototype and is designed to work in a classical way so will require named function constructors to work.

This was designed for ES3/5 systems in a world without decorators, it does have decorators for TS and ES6/7 users, however going forward more use cases could be supported for injection chains rather than simple instantiations.

Features to come

Ideally before I start using it anywhere I will need it to:

  • Allow lifecycle binding options (singleton, transient etc) [DONE]
  • Allow named/alias bindings [DONE]
  • Cache instance factories for performance boost
  • Add decorators for ES7 style use-cases [PARTIALLY DONE]

Ideally if it is possible (I think it is) and I get time:

  • Proxying for AoP style wrappers

It would be nice if there was a way to infer arguments some way, so you dont need to manually tie the arg name to the dependency, however will see how it evolves over time.

Usage

To do something basic you will need to new up an instance of Dependi, you really should just have one instance, although you can make multiple instances. Each one is isolated and has its own binding details.

To bind something you would do:

// Browser
var container = new Dependi.Container();
container.bind(YourObjectConstructor);

// NodeJs
var container = require("dependi").Container;

Then to get it you would do:

var myInstance = container.get(YourObjectConstructor);

If you have parameters in your constructor then tell the binding what to put in there:

container.bind(YourObjectConstructor).withArgument("argumentName", argumentValue);

Sometimes you will have a dependency on another object that has been bound, so in that case you will need to indicate to the container that you require a dependency injected into your constructor.

var container = new Dependi.Container();
container.bind(DependencyConstructor);
container.bind(DependantConstructor).withDependency("argumentName", DependencyConstructor);

You can chain together your bindings so if you have multiple arguments:

container.bind(YourObjectConstructor).withArgument("argument1Name", someValue).withArgument("argument2Name", someOtherValue).withDependency("argument3Name", SomeDependencyConstructor);

Finally you can manage the lifetime of your objects, in some cases you will want to share the same instance between all components within the dependency tree, in other cases you will want a new instance in each use. By default each get or dependency requirement will create a new instance of the dependency, however you can infer the lifetime like so:

container.bind(YourObjectConstructor).asSingleton();

Example

A simple example of using IoC:

function Foo(bar, message) // requires bar instance and a message
{
	this.doSomething = function() {
		bar.alertUser(message);
	}
}

function Bar()
{
	this.alertUser = function(message) {
		alert(message);
	};
}

var container = new Dependi.Container();
container.bind(Bar);
container.bind(Foo)
	  .withDependency("bar", Bar)	// withDependency(argName, constructorForDependency);
	  .withArgument("message", "DANGER WILL ROBINSON"); // withArgument(argName, argValue);
		
var foo = container.get(Foo);
foo.doSomething(); // Alerts DANGER WILL ROBINSON

Here is an example of what it does, check source code. View Example