From 5a085e1c171821359dd7e8205595ee6860687680 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 27 May 2024 20:00:55 +0200 Subject: [PATCH] Add some text on DI --- docs/code-howtos/index.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/code-howtos/index.md b/docs/code-howtos/index.md index ce306d9721f..1d913a3c294 100644 --- a/docs/code-howtos/index.md +++ b/docs/code-howtos/index.md @@ -11,12 +11,22 @@ See also [High Level Documentation](../getting-into-the-code/high-level-document We really recommend reading the book [Java by Comparison](http://java.by-comparison.com). -Please read [https://github.com/cxxr/better-java](https://github.com/cxxr/better-java) +Please read . * try not to abbreviate names of variables, classes or methods * use lowerCamelCase instead of snake\_case * name enums in singular, e.g. `Weekday` instead of `Weekdays` (except if they represent flags) +## Dependency injection + +JabRef uses a [fork](https://github.com/JabRef/afterburner.fx) of the [afterburner.fx framework](https://github.com/AdamBien/afterburner.fx) by [Adam Bien](https://adam-bien.com/). + +The main idea is to get instances by using `Injector.instantiateModelOrService(X.class)`, where `X` is the instance one needs. +The method `instantiateModelOrService` checks if there is already an instance of the given class. If yes, it returns it. If not, it creates a new one. + +In case a class needs parameters in the constructor, one needs to modify `org.jabref.gui.DefaultInjector#createDependency` to call the constructor with the appropriate parameters. +In case this way is not applicable, one can use `com.airhacks.afterburner.injection.Injector#registerExistingAndInject(T)` to register an existing instance with the injector. + ## Cleanup and Formatters We try to build a cleanup mechanism based on formatters. The idea is that we can register these actions in arbitrary places, e.g., onSave, onImport, onExport, cleanup, etc. and apply them to different fields. The formatters themselves are independent of any logic and therefore easy to test.