Skip to content

Latest commit

 

History

History
131 lines (103 loc) · 2.66 KB

module.md

File metadata and controls

131 lines (103 loc) · 2.66 KB

Module

Bindings

Binding is an assignment between the interface and its implementation.

So ideally the actual Java interface and its implementing class, like this:

interface User
{
    String getName();
}
class UserImpl implements User
{
    @Override
    String getName() {
        return "John Doe";
    }
}

Defining bindings

Bindings can be defined in multiple ways:

  • Manually in the module (or directly with the factory)
  • Using @Provides annotation in module
  • Using @Provides annotation in any other class

Manual bindings

class MyModule extends Module
{
    @Override
    protected void defineBindings() {
        whenRequestedInstanceOf(User.class).thenInstantiate(UserImpl.class);
    }
}

With multiple classes

The binding below will inject user implementation when either the user interface or the user implementation is requested from the factory:

class MyModule extends Module
{
    @Override
    protected void defineBindings() {
        whenRequestedInstanceOf(User.class).addForClass(UserImpl.class).thenInstantiate(UserImpl.class);
    }
}

Annotated bindings in module

class MyModule extends Module
{
    @Provides User createUser() {
        return new UserImpl();
    }
}

Note: The provided objects are NOT automatically injected with field and method dependencies!

Use Factory#inject() to inject it manually if this is what you need:

class MyModule extends Module
{
    @Provides User createUser() {
        final User user = new UserImpl();
        getFactory().inject(user);
        return user;
    }
}

Annotated bindings in any other objects

class MyProviders
{
    @Provides User createUser() {
        return new UserImpl();
    }
}
class MyModule extends Module
{
    @Override
    protected void defineBindings() {
        addProviderObject(new MyProviders());
    }
}

Note 1: The provider is also subject of injection when being registered to the factory.

Note 2: As with the module providers, also here the provided objects are NOT automatically injected with field and method dependencies!

Use Factory#inject() to inject it manually if this is what you need (note that factory can be injected too):

class MyProviders
{
    @Inject private Factory mFactory;

    @Provides User createUser() {
        final User user = new UserImpl();
        mFactory.inject(user);
        return user;
    }
}

Multiple modules

Multiple modules can be registered into a factory.

Bindings defined by the later registered modules override former ones. This allows to inject mock instead of real dependencies for testing.