Skip to content
Stéphane Nicolas edited this page May 8, 2016 · 30 revisions

A scope is one of the most important concept in ToothPick. It is actually important in Dependency Injection (DI) at large but Toothpick clearly exposes it to developers.

Scopes, instances creation and injections

In ToothPick, injections and instances creation always take place within a given scope.

Injecting within a scope

Let's consider the following example of injection :

//a class using field and method injections
class Foo {
  @Inject Bar bar;
  @Inject void setQurtz(Qurtz qurtz) {...}
}

//injecting an object in a scope
Toothpick.inject(new Foo(), scope);

The statement above is an entry point of a dependency graph in ToothPick. It will inject new Foo(), which means that :

  • all @Inject annotated fields of the instance of Foo will be assigned;
  • all @Inject annotated methods of the instance of Foo will be called;

In both cases, the values of the injected field and the parameters of the injected methods will be created inside the scope scope and its parents. Note that all transitive dependencies of Bar and Qurtz will also be created if needed, also in the scope scope and its parents.

Creating an instance within a scope

Let's consider the following example of instance creation :

//a class using field and method injections
class Foo {
  @Inject Foo() {...}

  @Inject Bar bar;
  @Inject void setQurtz(Qurtz qurtz) {...}
}

//injecting an object in a scope
Toothpick.getInstance(Foo.class, scope);

The statement above is also an entry point of a dependency graph in ToothPick. It will create an instance of the class Foo and inject all its members as in the previous injection example. The creation of the instance of Foo will use the @Inject annotated constructor and all required parameters, if any, will also be created within the scope scope.

If ToothPick creates an instance, it will always inject its dependencies.

Toothpick scopes

A scope contains bindings & scoped instances :

  • a binding : is way to express that a class Foo is associated to an implementation Bar, which we denote Foo --> Bar. It means that writing @Inject Foo a; will return a Bar. Bindings are valid for the scope where there are defined, and are inherited by children scopes. Children scopes can also override any binding inherited from of a parent.
  • scoped instances : a scoped instance is an instance that is reused for all injections of a given class. Scoped instances are "singletons" in their scope, and can be accessed from children scopes. Not all bindings create scoped instances. Binding Foo.class to an instance or to a Provider instance means that those instances will be recycled every time we inject {@code Foo} from this scope and children scopes. Note that in the case of the provider, the provider itself is recycled, not the instances it produces.

The scope Tree and the ToothPick class

In Toothpick, scopes create a tree (actually a disjoint forest, a.k.a a directed acyclic graph - DAG-). Each scope can have children scopes.

//Example of scopes during the life of an Android application
     Application Scope
       /         \    \
      /           \    \
Activity Scope     \   Service 2 Scope   
     /              \
    /          Service1 Scope
   /
Fragment Scope

With the scope tree, ToothPick is a DI framework that exposes 2 DAGs (which makes it a square dag :P) :

  • the DAG of injections (class A uses an injected dependency B)
  • the DAG of scopes (Fragment Scope is a child of Activity Scope)

Being a DAG has a direct impact on the ToothPick scope tree :

ToothPick will always bubble up the scope tree. It will never go down the scope tree.

Operations on the scope tree (adding / removing children, etc.) should be performed via the ToothPick class that wraps these operations.

Scopes have a name, which can be any object. Here is the code to create the scope tree above.

ToothPick.openScopes(application, activity, fragment);
ToothPick.openScopes(application, service1);
ToothPick.openScopes(application, service2);

It's intuitive to use the objects having a life cycle as names. When the object starts its life cycle, create a scope ToothPick.openScope(object) and when the object dies, close its associated scope ToothPick.closeScope(object). These object are generally not created by a DI framework, but can be injected and be an entry point of a dependency injection graph.

Opening multiple Scopes is possible, the opened scopes are then the children from each other, in order. This method will return the last open scope.

Links

Clone this wiki locally