Skip to content

Advanced Custom Bindings

Stéphane Nicolas edited this page Jan 13, 2015 · 3 revisions

Your Second Custom Binding

This document proposes a more fine grained approach to the creation of custom bindings than Your First Custom Binding.

Overview

A custom binding allows you to specify which implementation of an interface should be used when injecting that interface. This allows you to easily change implementations of an interface throughout the codebase from a single location.

To create a custom binding you need to take the following steps:

  1. Create a custom application class
  2. Create a Module that binds the interface to a specific class
  3. Register this module with Roboguice
  4. (Optional) Using a Provider that provides the dependency

Create a custom application class

This should be familiar to you, if not read this first. Note that your application class does not have to extend a Roboguice base class.

Create a module

The module's sole purpose here is to link the interface annotated with @Inject (IFoo) to a specific implementation of that interface (in this case SimpleFoo). The module also implements an interface which is (surprisingly) called Module.

public class MyModule implements Module {
    @Override
    public void configure(Binder binder) {
        binder.bind(IFoo.class).to(SimpleFoo.class);
    }
}

This tells RoboGuice: If we want to Inject IFoo; use SimpleFoo.

Register the module with RoboGuice

Up until now RoboGuice doesn't know about our module. The best place to do this is in the Application class onCreate method.

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, 
            RoboGuice.newDefaultRoboModule(this), new MyModule());
    }
}

Note that we need to reregister the DefaultRoboModule. If we don't do this all the default Android magic will be gone that is provided by RoboGuice.

That's it. When we now @Inject IFoo foo the IFoo instance will be SimpleFoo. You can add as many modules as you want.

Optional using a Provider for the dependency

If you want to use special logic for constructing your dependency than you will need a Provider.

Your provider should implement the Provider interface which just contains one method get. This method should return an instance of you class. Basically this works just like a normal Factory.

public class FooProvider implements Provider<IFoo> {
    @Override
    public IFoo get() {
        return new ComplexFoo(new Bar());
    }
}

Now instead of just binding IFoo to SimpleFoo we need to tell RoboGuice about our FooProvider. This is done inside our module.

public class MyModule implements Module {
    @Override
    public void configure(Binder binder) {
        binder.bind(IFoo.class).toProvider(FooProvider.class);
    }
}

Now every time we do @Inject IFoo foo it will use the provider to give us an instance of IFoo. This allows you to do more complex object creation.

Clone this wiki locally