This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Use dependency injection with Google Guice

  • Loading branch information...
jmewes committed Nov 12, 2017
1 parent 4f5d64a commit be93f58768772e97e6ad06da98fcf4df4fbdf000
View
@@ -17,5 +17,9 @@ The following resources are useful to understand the Why and How for the source
### JavaFX
- [[Video] JavaFX Programming, Part I -- Paul Anderson](https://www.safaribooksonline.com/library/view/javafx-programming-part/9780133409864/)
### Guice
- [[Book] Google Guice: Agile Lightweight Dependency Injection Framework -- Robbie Vanbrabant](https://www.safaribooksonline.com/library/view/google-guice-agile/9781590599976/)
- [[Blog] Creating Google Guice Modules with Java 6’s ServiceLoader -- Matthias Wessendorf](https://matthiaswessendorf.wordpress.com/2008/01/21/creating-google-guice-modules-with-java-6s-serviceloader/)
### Gradle
- [[Tutorial] Getting Started With Gradle: Creating a Multi-Project Build -- Petri Kainulainen](https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/)
- [[Tutorial] Getting Started With Gradle: Creating a Multi-Project Build -- Petri Kainulainen](https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/)
View
@@ -12,6 +12,8 @@ buildscript {
dependencies {
compile project(':openmrs-adapter')
compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
}
@@ -0,0 +1 @@
io.github.kschworkflows.patient.PatientModule
@@ -1,19 +1,25 @@
package io.github.kschworkflows;
import io.github.kschworkflows.openmrs.CustomizedOpenMRS;
import com.google.inject.Guice;
import com.google.inject.Injector;
import io.github.kschworkflows.common.ui.Page;
import io.github.kschworkflows.login.HomePage;
import io.github.kschworkflows.openmrs.CustomizedOpenMRS;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ServiceLoader;
public class ClinicalServiceApplication extends Application {
public static final Color BACKGROUND_COLOR = Color.LIGHTGRAY;
public static final double INITIAL_STAGE_WIDTH = 1000;
public static final double INITIAL_STAGE_HEIGTH = 500;
private static Injector injector;
private Stage primaryStage;
private Scene currentScene;
@@ -26,12 +32,21 @@ public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
setLoginCredentials();
initializeApplicationContext();
primaryStage.setTitle("K.S.C.H. Workflows - Clinical Service");
setScene(new HomePage(this));
primaryStage.show();
}
public static Injector getApplicationContext() {
if (injector != null) {
return injector;
} else {
throw new IllegalStateException("Application context is not initialized yet.");
}
}
public void setScene(Page page) {
if (currentScene == null) {
currentScene = new Scene(page.getContents(), INITIAL_STAGE_WIDTH, INITIAL_STAGE_HEIGTH, BACKGROUND_COLOR);
@@ -47,4 +62,8 @@ public void setScene(Page page) {
private void setLoginCredentials() {
CustomizedOpenMRS.init("https://ksch/openmrs", "superman", "Admin123");
}
private void initializeApplicationContext() {
injector = Guice.createInjector(ServiceLoader.load(com.google.inject.Module.class));
}
}
@@ -0,0 +1,12 @@
package io.github.kschworkflows.patient;
import com.google.inject.AbstractModule;
import io.github.kschworkflows.openmrs.CustomizedOpenMRS;
public class PatientModule extends AbstractModule {
@Override
protected void configure() {
bind(PatientService.class).toInstance(new PatientServiceImpl(CustomizedOpenMRS.getInstance()));
}
}
@@ -1,7 +1,5 @@
package io.github.kschworkflows.patient;
import io.github.kschworkflows.patient.Patient;
import java.util.List;
public interface PatientService {
@@ -1,35 +1,10 @@
package io.github.kschworkflows.patient;
import io.github.kschworkflows.openmrs.CustomizedOpenMRS;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import static io.github.kschworkflows.ClinicalServiceApplication.getApplicationContext;
public class PatientServiceFactory {
private static Map<String, Callable<PatientService>> implementations = new HashMap<>();
static {
registerImplementation("production", () -> new PatientServiceImpl(CustomizedOpenMRS.getInstance()));
}
public static void registerImplementation(String type, Callable<PatientService> factoryMethod) {
implementations.put(type, factoryMethod);
}
public static PatientService getPatientService() {
return getPatientService("production");
}
public static PatientService getPatientService(String type) {
if (!implementations.containsKey(type)) {
throw new IllegalArgumentException(String.format("There is no implementation for type '%s' present.", type));
}
try {
return implementations.get(type).call();
} catch (Exception e) {
throw new RuntimeException(e);
}
return getApplicationContext().getInstance(PatientService.class);
}
}
@@ -0,0 +1 @@
io.github.kschworkflows.patient.PatientModule
@@ -0,0 +1,16 @@
package io.github.kschworkflows.patient;
import org.junit.Test;
public class PatientModuleTest {
@Test
public void should_call_retrieve_patientservice_from_guice() {
}
}

0 comments on commit be93f58

Please sign in to comment.