Dependency injection Marko Tošić

Slides:



Advertisements
Similar presentations
Welcome to CODE SPREAD Simple Concepts of Coding | Programming.
Advertisements

Design Principles & Patterns
Real world TDD + Dependency Injection Rob Fonseca-Ensor Datacom Systems.
Spring, Hibernate and Web Services 13 th September 2014.
St Louis Day of.NET 2011 Taming Dependency Chaos with Inversion of Control Containers Steve Bohlen Senior Software Engineer SpringSource/VMware
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Class diagram refactoring example P3 practical example T120B pavasario semestras.
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
? Data-Driven apps and LightSwitch TechEd sessions Domain-Driven Design,.NET and Loosely-coupled approaches TechEd sessions Windows Apps Modernization.
CZ Biz. Auto. System & Test-Driven Development Teoman Soygul (Sept 24, 2012).
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
.NET Database Technologies: Entity Framework additional notes – part 2.
Introduction to AOP.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
Domain Driven Design. Set of blog posts spanning 10 months – building an app Fefactored along the way code to Patterns eg repository.
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua.NET/Cloud Computing UG Lead Blog:
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
PicoContainer Presented by: Jim O’Hara Ed Kausmeyer Jingming Zhang.
Anti Orgla, Nortal AS Spring Framework
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Sander Hoogendoorn Principal Technology Officer Capgemini The Netherlands SESSION CODE: ARC303.
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework,
Introduction to Web Dimitar Nenchev Ivan Nakov
Interno funkcioniranje modernog GUI-ja (korisničkog sučelja) na primjeru Unity 3D GUI frameworka Autor: Danko Kozar,
SOLID Principles in Software Design
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Using Mock Objects with Test Driven Development Justin Kohlhepp
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
2006/2007 Licence Apache 2.0 Castle.Igloo. Castle Igloo Basics Pre-require Concept Scopes PageFlow Configuration Controller View Exemple Castle.Igloo.
Daniel Hölbling
#SPSSAN June 30, 2012 San Diego Convention Center WRITING TESTABLE CODE In SharePoint.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
Development of the EDEX plug-in Ingest overview Manual Endpoint LDM DistributionSrv Plugin decoder Plugin Data Object PersistIndexSrv NotificationSrv.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Interface Segregation / Dependency Inversion
Structure of a web application
Designing software applications
Mark Seemann - Dependency Injection in .NET
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Managed Extensibility Framework
Unit Testing in a Team Sparkhound Presents by Steve Schaneville
By Ru Shen Department of Computer Science UAlbany, 2008
Java Hello world !.
Intro to Spring CJUG - January 2013.
Tech·Ed North America /18/ :26 AM
Titel Dependency Injection.
CS 350 – Software Design Singleton – Chapter 21
Graph 2x + 4y = 16 by finding the x and y intercepts.
Dependency Injection Carolyn Schroeder May 16, 2017.
Leveraging ColdSpring To Make Better Applications
European conference.
Hour of Code Coding Jetpack Jumper Mateja Hržica, MSP.
Dependency Injection Mechanism
Presentation transcript:

Dependency injection Marko Tošić

Š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).

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

Što je dependency injection?

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

Zašto DI?

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

3-layer primjer

3-layer primjer (DI)

Tko ima kontrolu?

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

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

DI process (primjer)

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

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

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

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

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

Lifetime management Singleton Transient Per Graph Web request context Pooled

Popularni DI/IoC containeri Autofac Castle Windsor nInject StructureMap Unity

Resursi Dependency Injection in.NET, Mark Seeman - originalan članak Martina Fowlera benchmark-performance-comparison - usporedba IoC containera benchmark-performance-comparison