Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript IoC / DI frameworks [closed]

For strongly typed static languages like Java, IoC / DI frameworks / toolkits are invaluable to decouple the complex systems thus make the sub-components unit testable and their composition fluently.

Most of us cite that Javascript as loosely typed dynamic language already offers the major benefits of IoC / DI in spirit, while some insist that higher level abstraction / implementation should be in place.

For the latter, what are those common IoC / DI implementations in Javascript realm?

like image 206
sof Avatar asked Mar 11 '13 10:03

sof


People also ask

Is IoC same as DI?

Inversion of Control(IoC) is also known as Dependency injection (DI). The Spring container uses Dependency Injection (DI) to manage the components that build up an application and these objects are called Spring Beans. Spring implements DI by either an XML configuration file or annotations.

Is IoC is a framework?

IoC frameworks implement common, low-level and problem-prone activities. This allows the developer to focus on custom business logic, instead of struggling with tedious infrastructure or configuration tasks.

What is IoC and DI in Java?

IOC is technique where you let someone else to create the object for you. And the someone else in case of spring is IOC container. Dependency Injection is a technique where one object supplies the dependency of another object.

Is dependency injection part of Dependency Inversion principle?

Dependency Injection is a programming technique where dependencies are not created by function or classes themselves but are provided via injections. It's one of the implementations of the Inversion of Control principle where the framework controls the app's flow and up to the developer is to provide the custom logic.


2 Answers

Please have a look into wire.js. This is my choice and it works great.

The main features are:

  • Simple, declarative dependency injection
  • A flexible, non-invasive connection infrastructure
  • Application lifecycle management
  • Powerful core tools and plugin architecture for integrating popular frameworks and existing code.
  • Support for both browser and server environments

Also please check some alternatives npm modules for DI

like image 193
Anashaka Avatar answered Oct 03 '22 11:10

Anashaka


Javascript's Duck-Typing allows us to easily mock dependencies.

Here is a quick example of how you could do this:

We have a simple function which represents an item being added to the database. DB is a dependency which we will want to mock.

function SaveItem(item){

    var db = new DB();

    db.insert(item);

};

If we change the DB dependency to be a parameter, then we can inject whatever we want. When the function is called it checks to see if the dataStore parameter exists. When the item exists the value is assigned to db. When dataStore is null or undefined, then a new DB object is created instead. This allows you to inject in your dependency with the confidence that db will always have a value.

function SaveItem(item, dataStore){

    var db = dataStore || new DB();

    db.insert(item);

};

Now we can mock our dependency like so:

function mockDb(){

    return{
        insert: function(){ 
            assert(true);  
        }
    }

};

And to bring it all together in a test:

function SaveItemShouldCallInsertOnDb = function(){

    var testDb = new mockDb();
    var someTestItem = { name: "test item" };

    SaveItem(someTestItem, testDb);
};
like image 28
BentOnCoding Avatar answered Oct 03 '22 11:10

BentOnCoding