• Keine Ergebnisse gefunden

Dependency Injection

N/A
N/A
Protected

Academic year: 2022

Aktie "Dependency Injection"

Copied!
11
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Dependency Injection

(2)

Hochschule Konstanz

Ausgangssituation

public class DocManager {

private LatexDoc doc ; public void newDoc ( ) {

doc = new LatexDoc ( ) ; }

… }

Document

(3)

Hochschule Konstanz

Nächster Schritt

public class DocManager {

private Document doc ;

public void newDoc ( Document inject ) { this.doc = inject;

}

public openDoc(){

this.doc.open();

(4)

Hochschule Konstanz

Eigenschaften

Anwendung des Prinzips IoC (Inversion of Control) Hollywood-Prinzip („don‘t call us, we‘ll call you“) Dokumente zur Laufzeit beliebig austauschbar

Einfaches testen z.B. mit Mocks möglich

Verwalter kann z.B. Konfigurationsdatei sein.

Komponenten lose gekoppelt

(5)

Hochschule Konstanz

Eigenschaften

Verwalter

<<creates>>

(6)

Hochschule Konstanz

Test mit dem Factory-Pattern

public class testDocManager {

Document realOne= docFactory.getInstance();

try{

WordDocMock mock = new WordDocMock();

docFactory.setInstance(mock);

DocManager doc = new DocManager();

doc.open();

asserttrue(mock.isOpen ());

}finally{

docFactory.setInstance(realOne);

(7)

Hochschule Konstanz

Test mit Dependency Injection

public class testDocManager {

WordDocMock mock = new WordDocMock();

DocManager doc = new DocManager(mock);

doc.open();

asserttrue(mock.isOpen());

}

(wenn man davon ausgeht, dass der Konstruktor des DocManagers den Mock an die Methode

„newDoc()“ weiterleitet)

(8)

Hochschule Konstanz

DI Frameworks

Grundsätzlich 3 Kategorien von Dependecy Injection Frameworks:

- Interface Injection ( Typ1 IoC ) - Setter Injection ( Typ2 IoC )

- Constructor Injection ( Typ3 IoC )

(9)

Hochschule Konstanz

DI Frameworks

Hier einige Dependecy Injection Framewoks:

-Google Guice

-PicoContainer (PicoContainer.Net) -Spring (Spring.Net)

-Avalon -…

(10)

Hochschule Konstanz

Contra

- Schlechtere Lesbarkeit des Codes

- Verwaltung der Klassen kann komplex werden

(11)

Hochschule Konstanz

Danke für die Aufmerksamkeit!

Referenzen

ÄHNLICHE DOKUMENTE

ZIEL 4 Erhöhte Transparenz über klimabezogene Finanzrisiken kann Klimaschutzmaßnahmen unterstützen sowie zu mehr Kompetenzen und Fachwissen über nachhaltige Entwicklung

Martin, Studies in the Life and Ministry of the Early Paul and Related Issues, Lewiston/Queenston/Lampeter: Mellen Biblical Press 1993, who claims that it was only after

The core of the work programme of a consortium centres on the execution of interrelated research and development as well as pilot and demonstration (P+D) and further

SWEET (SWiss Energy research for the Energy Transition) is a funding program 1 owned and man- aged by the Swiss Federal Office of Energy (SFOE).. The purpose of SWEET is to

SWEET 2 (SWiss Energy research for the Energy Transition) is a funding programme owned and managed by the Swiss Federal Office of Energy (SFOE).. The purpose of SWEET

As a new funding program, SWEET complements existing SFOE programs and differs in important aspects from the established Swiss Competency Centers for Energy Research.. The

Es ging um Barrierefreiheit (Baudisch et al. 2015), um Urban Gardening (Schumann 2016), Videospiele in Öffentlichen Bibliotheken (Schultze 2016), um

A) The regulation stigmatizes the use of traditional binary pronouns as potentially offensive, which can be re- garded as a form of harassment leading to disciplinary