Dependency Injection Andres Käver, IT College 2016/2017 Spring.

Slides:



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

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Programming Methodology (1). Implementing Methods main.
Design Principles & Patterns
INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Spring, Hibernate and Web Services 13 th September 2014.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
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.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
Programming Progamz pls. Importance VERY IMPORTANT.
Anti Orgla, Nortal AS Spring Framework
Abstract Factory Design Pattern making abstract things.
DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer You may like to write these down now...
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.
Test Driven Development Arrange, Act, Assert… Awesome Jason Offutt Software Engineer Central Christian Church
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise
Using Mock Objects with Test Driven Development Justin Kohlhepp
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Introducing Allors Applications, Tools & Platform.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
 A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined Data type and is Known as Reference.
SOLID Principles in Software Design
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
#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
Dependency Injection with Guice Technion – Institute of Technology Author: Gal Lalouche - Technion 2016 ©
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 -
Comp1004: Building Better Objects II Encapsulation and Constructors.
Staples are our staple Building upon our solution.
Andres Käver, IT Kolledž public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.
Building Web Applications with Microsoft ASP
Mocking tools for easier unit testing
TESTING TEST DRIVEN DEVELOPMENT
Mocking Tool for easier unit testing
using System; namespace Demo01 { class Program
Design Patterns C++ Java C#.
Mark Seemann - Dependency Injection in .NET
Spring Boot Introduction
Interface.
Design Patterns C++ Java C#.
Building Web Applications with Microsoft ASP
Abstract Class, Interface, Package
Factory pattern Unit of Work
Encapsulation and Constructors
Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring.
Advanced Java Programming
class PrintOnetoTen { public static void main(String args[]) {
Week 5 Review & Announcement
Dependency Injection Carolyn Schroeder May 16, 2017.
European conference.
Dependency Inversion principle
Dependency Injection Mechanism
Presentation transcript:

Dependency Injection Andres Käver, IT College 2016/2017 Spring

Dependency Inversion Principle Theory was first formalized by Robert C. Martin, 1996 High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions Example from life – myriad of chargers and small scale electronic devices

Dependency Inversion Every module is with its own interface Higher module has to support them all separately Modules are not reusable Higher module Interface Interface Interface Lower module Lower module Lower module

Dependency Inversion Higher module specifies the required interface All modules depend of the same interface Modules are reusable Interface Higher module Lower module Lower module Lower module

Inversion of Control Software development pattern Implements the Dependency Inversion principle Control over how different systems interface with each other Control over program events (command line vs graphical UI) Control over dependency creation and binding

Dependency Injection Implementation of IoC (DIP -> IoC -> DI) Dependency creation and resolving of them is moved outside of dependent class Injector Class Interface Class Interface Dependency Dependency

DI - Caution Internal principles of class behavior will be exposed Dependencies are created before they are actually needed Don’t overuse – is it needed for everything?

DI – Meta example Using constructor IRepo xmlrepo = new XMLRepo(); Kontroller knt = new Kontroller(xmlrepo); public class Kontroller{ private readonly IRepo repo; public Kontroller(Irepo repo){ this.repo = repo; }

DI – Meta example MVC During testing you can supply different implementation of repository In regular use default constructor is used and dependency is created public class PersonController:Controller{ private readonly IRepo _repo; public PersonController (Irepo repo){ _repo = repo ?? new PersonRepository(); } public PersonController() : this.PersonController(null) { }

Dependency Injection Container Framework Registers interfaces and their implementations Resolves dependencies and provides (injects) correct implementation Lifecycle management of created objects Several implementations, widely used are Ninject – Open Source Unity - MS official solution ASP.NET Core – has built-in lightweight solution out-of-the-box

Demo – console application public class PetOwner { private readonly IDog _dog; public PetOwner(IDog dog) _dog = dog; } public int Walkings() return _dog.WalkingCount; public void Walk() Console.WriteLine(_dog.Walk()); public interface IDog { string Walk(); int WalkingCount { get; set; } } public class DogPoodle : IDog public string Walk() WalkingCount++; return "Walking with Poodle "+WalkingCount.ToString()+" times!"; public int WalkingCount { get; set; }

Demo – console application static void Main(string[] args) { var kernel = new StandardKernel(); kernel.Bind<IDog>().To<DogPoodle>(); var dogKeeper = kernel.Get<PetOwner>(); dogKeeper.Walk(); Console.ReadLine(); }

Demo Asp.Net MVC Nuget Ninject.MVC5 Register your dependencies here \App_Start\NinjectWebCommon.cs private static void RegisterServices(IKernel kernel) { kernel.Bind<IPersonRepository>(). To<PersonRepository>(). WithConstructorArgument("context", new ContactsContext()); }

Demo Asp.Net MVC Controller public class PersonController : Controller { private readonly IPersonRepository _repo; public PersonController(IPersonRepository repo) _repo = repo; }