Lodge
Lodge is a namespacing library for node/io js. It let you register modules inside your directories under a given namespace.
Why namespacing ?
As Es6 is taking place into our repositories and javascript on the server needs to be more organized , we should namespace our es6 classes to maintain isolated references to each module inside the application.
How it works
Simply autoload es6 classes inside your directories, give them a unique namespace and while consuming do not require them as node modules instead resolve them from Ioc container
Example ( project structure )
├── app/
│ ├── controllers/
│ │ ├── UserController.js
│── server.js
Namespacing controllers
const Lodge = ;// register controllers under App/Controllers namespaceLodge; // get UserController instacelet UserController = Lodge
In above example we autoloaded all files inside app/controllers
method and registered them with a unique namespace to avoid name collisions.
Next we can get the instance of the class as a es6 generator and can call any methods registered inside class.
DI ( Dependency Injection )
DI auto resolves your classes dependencies and inject them into your constructors.
*{ return } static { return "UsersService"; } { thisusersService = UsersService } *{ return thisusersService }
DI is combination of constructor
method and inject
property. First we inject our dependencies using a static method and next we consume them inside our constructor in same order.
Not only it makes things more readable and object oriented , it makes testing easier by mocking constructor arguments.
Using identifiers
Namespacing identifiers gives identifiers to a directory, making resolve process a little easier. Let's take our first example of registering controllers under a namespace but this time with the help of identifiers
const Lodge = ;// register controllers under App/Controllers namespaceLodge; // get UserController instacelet UserController = Lodge
I hope you can notice the change , now while resolving module/class we do not have to enter the full namespace, as we can easily resolve the class name directly using identifiers.
Api methods
const Lodge = ;Lodge // give identifier to directory going to be registered // use a unique namespace // directory to register Lodge // identifier namespace to use // resolve binding from Ioc container Lodge // dump list of all bindings Lodge clear // clear all old bindings