Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwolniewicz committed May 18, 2024
2 parents 4ca14c5 + b0cd014 commit 9d30416
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 99 deletions.
2 changes: 1 addition & 1 deletion CheckOutExamples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion Decorator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion DepenDance-Full/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion Injector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Injector</artifactId>
Expand Down
225 changes: 133 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupid>com.github.jwdeveloper.DepenDance</groupid>
<artifactid>DepenDance-Full</artifactid>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</dependency>
```

Expand Down Expand Up @@ -94,114 +94,45 @@ Lightweight dependency injection container that is both small and performance ef
<h1>Examples</h1>


### Events
### Fields

```java
public class _4_Events {
public static void main(String[] args) {
DependanceContainer container = Dependance.newContainer()
.registerSingleton(Shop.class, LocalShop.class)
.registerSingleton(Shop.class, OnlineShop.class)
.configure(config ->
{
config.onInjection(_4_Events::onInjection);
config.onRegistration(_4_Events::onRegistration);
})
.build();

Object shops = container.find(Shop.class, String.class);
}


private static Boolean onRegistration(OnRegistrationEvent event) {
System.out.println("onRegistration event: " + event.registrationInfo().implementation().getSimpleName());
return true; //If false `container.find` injection is not registered to container
}

private static Object onInjection(OnInjectionEvent event) {
var inputType = event.input();// searched class type provided as first parameters
var genericTypes = event.inputGenericParameters(); //list of generic types provided as second parameter
var outputObject = event.output(); //Target object instance type has not been found then output value is null
var container = event.container(); //access to DI container
var injectonMetadata = event.injectionInfo();

System.out.println("OnInjection input class: " + inputType.getSimpleName());
System.out.println("OnInjection output class: " + outputObject.getClass().getSimpleName());
System.out.println("OnInjection genericTypes: " + genericTypes.length);
System.out.println("OnInjection metadata: " + injectonMetadata.toString());
return outputObject;
}
}
```
### Overriding
public class _9_Fields {
/**
* In case you don't want to use constructor, you can just tag fields with attribute @inject
*/

```java
public class _7_Overriding {
public static void main(String[] args) {

DependanceContainer container = Dependance.newContainer()
.registerTransient(Shop.class, OnlineShop.class)
.registerTransient(Shop.class, OfflineShop.class)
/**
* By again declaring Shop but with different implementation (OnlineShop)
* We are telling container to Override (OfflineShop) and always returns (OnlineShop)
*/
.registerTransient(_9_Fields.ExampleClass1.class)
.registerTransient(_9_Fields.ExampleClass2.class)
.registerTransient(_9_Fields.FieldsExample.class)
.build();

Shop shop = container.find(Shop.class);
Assert.assertEquals(OfflineShop.class, shop.getClass());
System.out.println("shop object is instance of OfflineShop Class");
_9_Fields.FieldsExample example = container.find(_9_Fields.FieldsExample.class);
Assert.assertNotNull(example);
Assert.assertNotNull(example.getExampleClass1());
Assert.assertNotNull(example.getExampleClass2());
}
}
```
### Generic Types

```java
public class _5_Generic_Types {

public static void main(String[] args) {

var container = Dependance.newContainer()
.configure(configuration ->
{
/*
* Since java is not storing information about generic type after compilation
* we can not assign class with generic type to variable, so Repository<MyGenericType>.class is not possible
* Therefor all cases with generic types (besides lists) must be handled manually in onInjection event
*/

configuration.onInjection(injection ->
{
if(!injection.input().isAssignableFrom(Repository.class))
{
return injection.output();
}

var genericParameter =injection.inputGenericParameters()[0];
if(genericParameter.equals(OnlineShop.class))
{
return new Repository<OnlineShop>();
}
if(genericParameter.equals(LocalShop.class))
{
return new Repository<LocalShop>();
}
return new Repository();
});
})
@Getter
public static class FieldsExample {

.build();
@Inject
private ExampleClass1 exampleClass1;

@Inject
private ExampleClass2 exampleClass2;
}

//first parameter is class, second one is its generic parameter
var onlineShopRepo = container.find(Repository.class, OnlineShop.class);
var localShopRepo = container.find(Repository.class, LocalShop.class);
public static class ExampleClass1 {

Assert.assertNotNull(onlineShopRepo);
Assert.assertNotNull(localShopRepo);
}

public static class ExampleClass2 {

}
}
```
### Basic
Expand Down Expand Up @@ -241,6 +172,45 @@ public class _1_Basic {
}
}
```
### Events

```java
public class _4_Events {
public static void main(String[] args) {
DependanceContainer container = Dependance.newContainer()
.registerSingleton(Shop.class, LocalShop.class)
.registerSingleton(Shop.class, OnlineShop.class)
.configure(config ->
{
config.onInjection(_4_Events::onInjection);
config.onRegistration(_4_Events::onRegistration);
})
.build();

Object shops = container.find(Shop.class, String.class);
}


private static Boolean onRegistration(OnRegistrationEvent event) {
System.out.println("onRegistration event: " + event.registrationInfo().implementation().getSimpleName());
return true; //If false `container.find` injection is not registered to container
}

private static Object onInjection(OnInjectionEvent event) {
var inputType = event.input();// searched class type provided as first parameters
var genericTypes = event.inputGenericParameters(); //list of generic types provided as second parameter
var outputObject = event.output(); //Target object instance type has not been found then output value is null
var container = event.container(); //access to DI container
var injectonMetadata = event.injectionInfo();

System.out.println("OnInjection input class: " + inputType.getSimpleName());
System.out.println("OnInjection output class: " + outputObject.getClass().getSimpleName());
System.out.println("OnInjection genericTypes: " + genericTypes.length);
System.out.println("OnInjection metadata: " + injectonMetadata.toString());
return outputObject;
}
}
```
### Lists

```java
Expand Down Expand Up @@ -377,6 +347,56 @@ public class _6_AutoScan {
System.out.println("Hello world!");
}
}
}
```
### Generic Types

```java
public class _5_Generic_Types {

public static void main(String[] args) {

var container = Dependance.newContainer()
.configure(configuration ->
{
/*
* Since java is not storing information about generic type after compilation
* we can not assign class with generic type to variable, so Repository<MyGenericType>.class is not possible
* Therefor all cases with generic types (besides lists) must be handled manually in onInjection event
*/

configuration.onInjection(injection ->
{
if(!injection.input().isAssignableFrom(Repository.class))
{
return injection.output();
}

var genericParameter =injection.inputGenericParameters()[0];
if(genericParameter.equals(OnlineShop.class))
{
return new Repository<OnlineShop>();
}
if(genericParameter.equals(LocalShop.class))
{
return new Repository<LocalShop>();
}
return new Repository();
});
})

.build();


//first parameter is class, second one is its generic parameter
var onlineShopRepo = container.find(Repository.class, OnlineShop.class);
var localShopRepo = container.find(Repository.class, LocalShop.class);

Assert.assertNotNull(onlineShopRepo);
Assert.assertNotNull(localShopRepo);
}


}
```
### ManyConstructors
Expand Down Expand Up @@ -426,3 +446,24 @@ public class _8_ManyConstructors
}
}
```
### Overriding

```java
public class _7_Overriding {
public static void main(String[] args) {

DependanceContainer container = Dependance.newContainer()
.registerTransient(Shop.class, OnlineShop.class)
.registerTransient(Shop.class, OfflineShop.class)
/**
* By again declaring Shop but with different implementation (OnlineShop)
* We are telling container to Override (OfflineShop) and always returns (OnlineShop)
*/
.build();

Shop shop = container.find(Shop.class);
Assert.assertEquals(OfflineShop.class, shop.getClass());
System.out.println("shop object is instance of OfflineShop Class");
}
}
```
2 changes: 1 addition & 1 deletion Tools-Benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion Tools-Readme/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>DepenDance</artifactId>
<groupId>io.github.jwdeveloper.dependance</groupId>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>io.github.jwdeveloper.dependance</groupId>
<artifactId>DepenDance</artifactId>
<packaging>pom</packaging>
<version>0.0.10-Release</version>
<version>0.0.11-Release</version>
<modules>
<module>Injector</module>
<module>Decorator</module>
Expand Down

0 comments on commit 9d30416

Please sign in to comment.