Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dependency injection Marko Tošić

Similar presentations


Presentation on theme: "Dependency injection Marko Tošić"— Presentation transcript:

1 Dependency injection Marko Tošić marko.tosic@proteron.hr

2 Što je dependency injection? Dependency injection is a software design pattern that implements inversion of control and allows a program design to follow the dependency inversion principle. The term was coined by Martin Fowler. An injection is the passing of a dependency (a service) to a dependent object (a client).

3 Dependency injection to a 5 year old When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired. What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat. John Munsch - Stackoverflow

4 Što je dependency injection?

5 bez DI public SomeClass() { myObject = new Myclass(); } DI public SomeClass (MyClass myObject) { this.myObject = myObject; }

6 Zašto DI?

7

8 Smanjuje povezanost klasa Povećava modularnost i ponovnu iskoristivost koda Kod je lakši za održavanje Omogućava lakše testiranje

9 3-layer primjer

10 3-layer primjer (DI)

11 Tko ima kontrolu?

12 Composition root Centralno mjesto u aplikaciji gdje se upravlja kompozicijom aplikacije (njenim modulima) Treba biti što bliže ulaznoj točki aplikacije ‐ Console app – Main metoda ‐ MVC app – Application_Start() u Global.asax-u ‐ WebForms – teško integrirati DI

13 DI proces Register – registracija implementacije s apstrakcijom, događa se jednom pri instanciranju aplikacije Resolve – određivanje ovisnosti, treba biti u jednoj liniji koda Release – otpuštanje ovisnosti, treba biti u jednoj liniji koda

14 DI process (primjer)

15 DI patterns Constructor injection – koristi se u većini slučajeva, u konstruktoru se navode sve ovisnosti Method injection – kada moramo ubaciti različite ovisnosti za različite operacije, koristi se npr. kod add-in arhitekture Property injection – setter injection, opcionalni DI, sa lokalnim defaultom

16 Constructor injection private readonly EngineRepository repository; public CarMaker(EngineRepository repository) { if (repository == null) { throw new ArgumentNullException("repository"); } this.repository = repository; }

17 DI anti-patterns / refactorings Control Freak Constructor Over-injection – SRP? public MyClass(IUnitOfWorkFactory uowFactory, LoggingProvider loggingProvider, IFooPolicy fooPolicy, IBarService barService, IDoSomeStuff doSomeStuff, IDoMoreStuff doMoreStuff)

18 DI konfiguracija XML – bez rekompajliranja Code as configuration - najfleksibilniji Auto registration (convention) – ušteda vremena, nije podržan u svim DI containerima

19 DI funkcionalnosti Object composition Lifetime management Interception (cross-cutting concerns)

20 Lifetime management Singleton Transient Per Graph Web request context Pooled

21 Popularni DI/IoC containeri Autofac Castle Windsor nInject StructureMap Unity

22 Resursi Dependency Injection in.NET, Mark Seeman http://martinfowler.com/articles/injection.html - originalan članak Martina Fowlera http://martinfowler.com/articles/injection.html http://www.palmmedia.de/Blog/2011/8/30/ioc-container- benchmark-performance-comparison - usporedba IoC containera http://www.palmmedia.de/Blog/2011/8/30/ioc-container- benchmark-performance-comparison


Download ppt "Dependency injection Marko Tošić"

Similar presentations


Ads by Google