Welcome to CODE SPREAD Simple Concepts of Coding | Programming.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Written by: Dr. JJ Shepherd
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Object-Oriented PHP (1)
Where Do the 7 layers “fit”? Or, where is the dividing line between hdw & s/w? ? ?
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
ASP.NET Programming with C# and SQL Server First Edition
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
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.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Introduction to TypeScript Sergey Barskiy Architect Level: Introductory.
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Introducing Allors Applications, Tools & Platform.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
ARCH-11: Building your Presentation with Classes John Sadd Fellow and OpenEdge Evangelist Sasha Kraljevic Principal TSE.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Chapter 1 Introducing Ant. What is ant? Ant is a build tool  Automate the tasks of compiling code, running test, and packaging the results for redistribution.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Classes, Interfaces and Packages
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
CSCE 240 – Intro to Software Engineering Lecture 3.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
TK1924 Program Design & Problem Solving Session 2011/2012
Static data members Constructors and Destructors
Review What is an object? What is a class?
Low Budget Productions, LLC
Software Design and Architecture
About the Presentations
PHP Classes and Objects
Designing for Inheritance
Java Programming Language
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Interfaces.
CS 350 – Software Design Singleton – Chapter 21
European conference.
Classes and Objects CGS3416 Spring 2019.
Dependency Inversion principle
Dependency Injection Mechanism
Presentation transcript:

Welcome to CODE SPREAD Simple Concepts of Coding | Programming

DI is Dependency Injection. It is defined as Dependency injection is a software design pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile- time.. There is one major point we should always remember, Inversion of Control is a principle and Dependency Injection is the implementation

Q: How DI can be implemented? A: In Dependency Injection each component must declare a list of dependencies required to perform the task. At run-time a special component (generally) called an IoC Container performs binding between these components.

Q: How DI can be implemented? (contd.) A: There are three ways to implement Dependency Injection. Constructor Injection Setter Injection Interface-based injection

Example: We define an interface first and the classes which inherits this interface Interface IAccount { //Definition } public class SavingsAccount : IAccount { //Implementation } public class CurrentAccount : IAccount { //implementation }

Constructor Injection : We define a class CalculateInterest and Interface define above will be passed to the constructor of the CalculateInterest class. public class CalculateInterest { private IAccount account; public CalculateInterest(IAccount account) { this.account=account; } }

Constructor Injection : (contd.) Next, we will check our code. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateSavingsInterest = new CalculateInterest(savingsAccount); // when using CalculateInterest class for SavingsAccount CalculateInterest calculateCurrentInterest = new CalculateInterest(currentAccount); // when using CalculateInterest class for CurrentAccount

Constructor Injection : (contd.) Drawback One of the major drawback of constructor injection is that once the class is instantiated, it is difficult to change the objects dependency This drawback is corrected in setter injection.

Setter Injection : To resolve problem faced in constructor injection, We will not pass the interface as parameter but we will assign it to a property of the class.

Setter Injection : (contd.) Create public property of Interface in the CalculateInterest class. public class CalculateInterest { private IAccount _account; public IAccount Account { get { return _account; } set { _account= value; }

Setter Injection : (contd.) Now instantiate the class and assign the required property. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateInterest = new CalculateInterest(); calculateInterest.Account = savingsAccount; // when using CalculateInterest class for SavingsAccount calculateInterest.Account = currentAccount; // when using CalculateInterest class for CurrentAccount

Setter Injection : (contd.) Its quite clear and provides more flexibility even after creation of the object.

Interface Injection: We can provide more flexibility by creating a method in the class CalculateInterest which will have interface as a parameter. public class CalculateInterest { private IAccount account; public SetAccount(IAccount account) { this.account=account; } }

Interface Injection: (contd.) Lets see the call also. IAccount savingsAccount = new SavingsAccount(); IAccount currentAccount = new CurrentAccount(); CalculateInterest calculateInterest = new CalculateInterest(); calculateInterest.SetAccount(savingsAccount); // when using CalculateInterest class for SavingsAccount calculateInterest.SetAccount(currentAccount); // when using CalculateInterest class for CurrentAccount

There is one more way to apply IoC, Service Locator. Service Locator: In Service Locator, dependency is not injected through any medium but retrieved through the medium of a static method.

Service Locator: (contd.) Declare a static class and a static method inside it. static class AccountType { public static IAccount getAccountType() { //Implementation }

Service Locator: (contd.) Now call this method from your class public class CalculateInterest { private IAccount account; public CalculateInterest(IAccount account) { this.account=AccountType.getAccountType(); } }

We have learned basic concepts, importance of DI and ways of implementation. I hope after viewing this article, many complexities revolving around DI and IOC will get resolved. Please mail your comments or articles at

Thanks!! CODE SPREAD Simple Concepts of Coding | Programming