Inject all the things
How to use MobileUI Inject to create and inject dependent objects
MobileUI Inject PURPLE is the dependency injection framework for MobileUI. If you have not configured MobileUI Inject for your project, please follow the MobileUI Inject setup guide first.
The following is a quick start on how to create beans for injection and then inject them into unmanaged and managed objects. MobileUI Inject is based on Micronaut Inject. Therefore, it is a wise idea, to read Micronaut's Inversion of Control Documentation first. You will get an overall introduction into dependency injection and the features of Micronaut, that will help you to work with it in the mobile context.
If you already know Micronaut: Just proceed with this short guide on the specialties that are added by MobileUI Inject.
Defining a Bean
To define a Singleton bean, you can simply annotate a class (e.g. in your app-common module) with the @Singleton
annotation as shown below. The MobileUI Inject Compiler will automatically generate all necessary meta information to register this Singleton for your app.
import javax.inject.Singleton;
@Singletonpublic class GreetingService {
public String getGreeting() {
return "Rock on!";
}
}
Injecting a Bean into an unmanaged object
Once you have defined a bean as shown above, you can inject it into other objects. The following code snippet shows how to inject into objects, that are not managed by the dependency injection container. In this case, you have to call MobileUI.inject(this)
. After the call, the injected bean is available.
import javax.inject.Inject;
public class MainController {
@Inject GreetingService greetingService;
public MainController() {
// Manually trigger injection as this is not a managed object
MobileUI.inject(this); }
public String getGreeting() {
return greetingService.getGreeting();
}
}
This is the preferred way, when you need injection in your Android Activities, Fragments or iOS UIViewControllers. Typically, either the Android Runtime or the iOS App code creates new instances of these classes, so that manual injection is required.
Alternatively, you can sub class InjectActivity or InjectFragmet on Android and the InjectUIViewController on iOS to have automatic injection taking place. It's your choice.
Injecting a Bean into a managed object
A managed object is itself a bean and created by the dependency injection container. Therefore, you don't need to manually call the injection method as discussed above. However, you have to make sure to annotate your class with an annotation that enables the DI metadata generation. In the example below, we are using the @Prototype
annotation to configure a bean that is re-created every time it is injected. The latter makes sense, if you have controllers that have the same lifecycle as your Fragments, Activities or UIViewControllers.
import javax.inject.Inject;
import io.micronaut.context.annotation.Prototype;
@Prototypepublic class MainController {
@Inject GreetingService greetingService;
public String getGreeting() {
return greetingService.getGreeting();
}
}
An alternative method of injecting dependencies into managed objects is to use constructor injection. Micronaut automatically analyzes the constructor of managed beans to find dependent objects. Especially for Kotlin, this is way more elegant as it automatically solves non-nullability requirements:
import javax.inject.Inject;
import io.micronaut.context.annotation.Prototype;
@Prototypepublic class MainController {
private GreetingService greetingService;
public MainController(GreetingService greetingService){ this.greetingService = greetingService;
}
public String getGreeting() {
return greetingService.getGreeting();
}
}
Get the Micronaut Application Context at Runtime
You can access the Micronaut ApplicationContext as needed with a call to:
MobileUI.getApplicationContext();
The ApplicationContext offers many additional functionalities such as messaging and more.
Happy Injecting!