from imagination.decorator.service import registered
Let's take a look at this example.
Suppose we have two services, DataService and DataAdapter, where DataService requires DataAdapter.
class DataAdapter:
...
class DataService:
def __init__(self, adapter: DataAdapter):
...
...Traditionally, to instantiate DataService, you need to do this somewhere in your code.
adapter = DataAdapter()
service = DataService(adapter)Now, with Imagination, first you will need to annotate/decorate the classes.
from imagination.decorator.service import registered
@registered()
class DataAdapter:
...
@registered()
class DataService:
...Here is what just happened.
- Imagination understands how to instantiate an instance of both classes.
- However, it DOES NOT instantiate any registered classes.
- By design, it instantiates when needed.
And then, you can fetch the reference DataService by running this.
##############
# Since v3.3 #
##############
from imagination.standalone import container
container.get(DataService).do_something(...)##############
# Since v3.4 #
##############
from imagination.standalone import use
use(DataService).do_something(...)Where the framework will instantiate DataAdapter first and then DataService as DataService requires DataAdapter.
While both
container.getanduseyield the same result, theusemethod is preferable if you are using an IDE.
The documentation is available at http://readthedocs.org/docs/imagination/.