An Android java workshop about the toothpick library. It contains an extensive list of usages of the toothpick library and some examples with instrumentation tests.
First things first:
Thank you Stéphane Nicolas (@stephanenicolas) and Daniel Molinero (@dlemures) for this awesome library.
These are the following examples exposed in the project:
Simple binding of interfaces Name.class
and Surname.class
using toInstance
to new NameEnglishImpl()
and new SurnameEnglishImpl()
.
Simple binding of interfaces Name.class
and Surname.class
using to
with implementations that have @Inject
in the default constructor.
Similar than Simple2Activity but using on demand injection with Scope#getInstance()
Using a second module to inject a class FullNameInjectionInsideImpl.class
that opens the scope inside of the constructor.
Binding the interface FullName.class
with ConstructorParamsFullNameImpl.class
that contains injected parameters in the constructor. Important to realize that the order of the binding does not matter.
A simple module with params that binds a Provider<FullName.class>
using toProviderInstance
.
A simple usage of Provider<FullName.class>
that has the members injected in InjectedFullNameProvider
without using a constructor and binding the provider using toProvider
.
Same as Advanced4Activity but now the injections happens in the constructor of Injected2FullNameProvider.java
.
Example of using named bindings (more than one class type in the scope) with @Named("name")
and using custom annotations (type @Qualifier)
Example of the creation of a child scope on the top of the parent defined in another module to override one binding. It also shows how the child activity injects variable members from the parent activity.
Using a scope defined in the libraryDemo
module, the scope is created by calling Singleton.getInstance()
Using a scope defined in the libraryDemo
module and installing a child scope in the application that overrides the binding defined in the library.
testWithTestModule()
uses installTestModule
to override all bindings.
testWithTestModuleOnlyName()
uses installTestModule
to override only one binding.
testNormal()
proves that the shared scope between library and application works.
testNormal()
same as the one above.
testChildScope()
proves that child scope implementation overrides the bindings of the parent.
To override a binding there are two different approuches
- ✅ Use the same scope and a new module with
#installTestModule(...)
that redefines a new existing binding. - ✅ Open a child scope on the top of the parent that redefines a new existing binding.
- 🚫 WHAT DOES NOT WORK: installing a module in the existing scope that redefines a previous binding. This will not override anything.