Dependency Modules
By using Brandi, you can organize bindings in dependency modules. In this section we will see how to create, organize and use your modules.
DependencyModule is a logical space to help you organize your bindings.
It is similar to Container, but it can only store dependencies, but not resolve or inject them.
DependencyModule Methods#
DependencyModule repeats the following Container methods:
Using Dependency Modules#
Simple Example#
Let's take an simple example, with using a binding from a module by the token:
Container will resolve the apiKey dependency from the module.
More Complex Example#
In this example, we have the ApiService that depends on a apiKey, API dependency module, and the App that depends on an ApiService.
Let's start with the token declaration:
Then we will create the ApiService with a dependency on a apiKey:
Then we will create the dependency module to which we will bind all the dependencies necessary for the API module:
Creating our App that depends on ApiService:
And finally configure the container:
Let's run:
note
Some of your dependency modules may use bindings from other modules. If there are bindings of the same token in the module chain, the highest-level binding in the hierarchy will be used.
createDependencyModule()#
createDependencyModule() โ is alias for new DependencyModule().