Services

Ivy includes a great and easy way of manipulating your services, together with performing some tasks like dependency injection.

Binding

Registering a service is done by using bind method.
First parameter should be a namespace of a given service, which will be binded to it, while second is a closure which is suppose to return a new instance of service.

bind('Ivy/CoolService',function(){returnnewCoolService();});

Singleton

You can also directly bind a singleton object, by using singleton method.

Namespace

Javascript doesn't come with the namespaces, however, Ivy does, and they provide you a handy way of working with your classes and objects.
Global function namespace, requires 2 arguments, one is the desired namespace, while the second is the class binding.

Alias

Note that this is available by editing the services config file, more about it comes later.

Use

So, until now, we were talking about binding a namespace to the closure, but how are we suppose to get the binded object?

let obj =use('Ivy/Model');

and thats pretty much it. However, make sure that you understand few things.
First, in case that we have used a bind or singleton function to bind a given namespace, obj will contain the instantiated object of a service.
Second, when namespace is registered with namespace function, it will return only a class, so you would still need to do the new part.
And third, if theres an alias for that namespace, you can use it as a parameter, so use('Ivy/Alias'); is just fine.

Injection

By combining use and bind / singleton functions, we are able to perform the dependency injection in the binded class.