Skip to content

Easy to use android dependency injection, to help you quickly organize your singletons, components ...

License

Notifications You must be signed in to change notification settings

Ekito/android-injector

Repository files navigation

A tiny dependency injector

Easy to use android dependency injection, to help you quickly organize your singletons, components ...

No annotation, not intrusive, no bullshit, just simple piece of java ... You are free to use it where you want !

Get it with gradle

repositories {
    jcenter()
}


compile 'android-injector:injector:1.0.0'

Add a component

Just declare a component in the injector to inject it later

Cat felix = new Cat("felix",3);
Injector.add(felix);

The Injector.add() operator add/replace a component, thanks to it class

Retrieve it

Get your object back by asking the Injector.get() operator, with the given object class:

Cat felix = Injector.get(Cat.class)

Make a module, for gathering components

You can gather object definitions in module. Extends the fr.ekito.injector.Module class, implements the load method.

public class PetsModule extends Module {
    @Override
    public void load() {
        Cat felix = new Cat("felix",3);
        Dog wolfy = new Dog("wolfy",5);
        provide(felix);
        provide(wolfy);
        provide(new PetsHouse(felix,wolfy));
    }
}

`provide()` help you provides any object to the injector

For any object dependency, just use directly in the modules or use Injector in your component !

Load a module

The load method is called when loading your module :

Injector.load(PetsModule.class)

Now you can get any object where you want !

Reuse modules

You can reuse any existing module with the extend operator, in your module.

public class ZooModule extends Module {
    @Override
    public void load() {
        // use PetsModule
        extend(PetsModule.class);
        ...
    }
}

I Have proxies, How can I do ?

You can specify a target class when adding your object :

Cat felix = ... make proxy
Injector.add(felix,Cat.class);
Cat felix = Injector.get(Cat.class)

In your android app

start it at Application level

The best way to load module is from your Application component :

public class MainApplication extends Application {

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

        // load my module
        Injector.load(MyModule.class);
    }
}

But use it, where you want !

get it there ...

Then inject your components where you want !

public class MainActivity extends AppCompatActivity {

    GitHubService gitHubService = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        gitHubService = Injector.get(GitHubService.class);

Also easy to use in Kotlin

You can also use it with kotlin. Beware to use java classes :

class WebModule : Module() {
    override fun load() {
        val httpClient = httpClient()

        provide(httpClient, OkHttpClient::class.java)
        provide(githubWS(httpClient), GitHubService::class.java)
    }

    private fun httpClient(): OkHttpClient {
        ...
    }

    private fun githubWS(httpClient: OkHttpClient): GitHubService {
        ...
    }
}
// use web java module
Injector.load(WebModule::class.java)
val service = Injector.get(GitHubService::class.java)
val response = service.listRepos("octocat").execute()


# That's it !

About

Easy to use android dependency injection, to help you quickly organize your singletons, components ...

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published