https://github.com/gustavohennig/simpledependencymanager
Simple Dependency Manager for Java, useful for small projects, Android Apps and Integration Tests
https://github.com/gustavohennig/simpledependencymanager
android dependency-injection inject java service-locator unit-testing
Last synced: 7 months ago
JSON representation
Simple Dependency Manager for Java, useful for small projects, Android Apps and Integration Tests
- Host: GitHub
- URL: https://github.com/gustavohennig/simpledependencymanager
- Owner: GustavoHennig
- License: apache-2.0
- Created: 2019-11-21T19:35:16.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-18T15:21:02.000Z (over 5 years ago)
- Last Synced: 2025-01-18T03:24:33.877Z (9 months ago)
- Topics: android, dependency-injection, inject, java, service-locator, unit-testing
- Language: Java
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Simply Dependency Manager
This project contains two different implementations of Dependency Injection.
##### DependencyManagerTyped
This implementation is more like a service locator. It is more performative because it is typed, without the use of reflection.
Works passing the `Manager` to the constructor of every Business Logic Service.
Example:
https://github.com/GustavoHennig/SimpleDependencyManager/blob/master/src/test/java/simpledependecymanager/DependencyManagerTypedTest.javaI found a post that explains the concept better:
https://proandroiddev.com/why-service-locator-is-so-unpopular-bbe8678be72c##### DependencyManagerReflection
It uses the well known `@Inject` annotation,
the injector will try to create automatically on every attribute
with this annotation when the type has constructor without arguments.
This solution was created for simple integration testes, but can be used in large project.
#### ExamplesExamples of how to use (Reflection version).
```java
public class ServiceExample {@Inject
private ServiceToInject serviceToInject;
private String injectByRule;
}
``````java
public class ServiceToInject {
public int foo() {
return 1 + 1;
}
}
```Creates the instances automatically
```java
public class DependencyManagerReflectionTest {
@Test
public void test() {
DependencyManagerReflection dependencyManager = new DependencyManagerReflection();// Configure an injection rule exception
String existingInstance = "existingInstance";
dependencyManager.addRule("injectByRule", String.class, existingInstance);
ServiceExample serviceExample = dependencyManager.get(ServiceExample.class);
assertEquals("must call the method of injected class", 2, serviceExample.callFoo());
assertSame("must be same instance", existingInstance, serviceExample.getInjectByRule());
}
}
```Please, see the unit tests of this project for more examples:
https://github.com/GustavoHennig/SimpleDependencyManager/tree/master/src/test/java/simpledependecymanager