L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile

Slides:



Advertisements
Similar presentations
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
Advertisements

©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development.
Spring, Hibernate and Web Services 13 th September 2014.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB JavaForum.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
ASPECT ORIENTED SOFTWARE DEVELOPMENT Prepared By: Ebru Doğan.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
Spring.Net Steinar Dragsnes steinar.dragsnes at viz.no.
Aspect Oriented Programming Written by Michael Beder.
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Outline Introduction Problem Statement Object-Oriented Design Aspect-Oriented Design Conclusion Demo.
Copyright 2006, Codeguild, Inc Spring Framework Fundamentals March, 2006 Larry Hamel Codeguild, Inc.
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.
Introduction to AOP.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
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.
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Design Design and Software Architecture. The design phase The analysis phase describes what the system should be doing The design phase describes how.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development 1.
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.
Modularizing Web Services Management with AOP María Agustina Cibrán, Bart Verheecke { Maria.Cibran, System and Software Engineering.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Aspect Oriented Programming Gülşah KARADUMAN.
Joel Phinney March 31, ◦ Concerns  Separation of Concerns, Tangled and Scattered Concerns, Cross-Cutting Concerns, Aspects ◦ Aspect-Oriented Software.
Methodology: The AOP Refactoring Process Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton Advisor:
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Introducing Allors Applications, Tools & Platform.
Chapter 8: Aspect Oriented Programming Omar Meqdadi SE 3860 Lecture 8 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Aspect Oriented Development Alex Beatty.  Purpose  Cross-cutting Concerns  Join Points, Pointcuts, and Advices  Weaving  Invasive vs. Non-Invasive.
Java EE Patterns Dan Bugariu.  What is Java EE ?  What is a Pattern ?
1 Spring Framework April, 2012 Lam Ho Lam To. © 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2 1.Spring Overview 2.Framework.
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Introduction to Yan Cui Aspect Oriented Programming by
Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.
Aspect-Oriented Software Development (AOSD)
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Inspections on Testing Aspect-Oriented Programs Rodrigo Manuel Lopes de Matos Moreira 4 th Doctoral Symposium on Informatics Engineering Porto, 6 th February.
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
 Is a programming paradigm  Extends OOP  Enables modularization of cross cutting concerns.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
Interface Segregation / Dependency Inversion
Designing software applications
Chengyu Sun California State University, Los Angeles
Software Design and Architecture
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
By Ru Shen Department of Computer Science UAlbany, 2008
Present by Andie Saizan, MCP
ARCH-1: Application Architecture made Simple
Tuesday Brown Bag Inversion of Control with Howard Abrams
Structuring Adaptive Applications using AspectJ and AOM
CS520 Web Programming Spring – Aspect Oriented Programming
AspectAda Aspect-Oriented Programming for Ada95
Leveraging ColdSpring To Make Better Applications
Aspect Oriented Software Design
Aspect Oriented Programming
Dependency Injection Mechanism
Presentation transcript:

L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile

Software Dependencies Software Dependencies Service Locator Service Locator Inversion of Control Inversion of Control Dependency Injection Dependency Injection Aspect Oriented Programming Aspect Oriented Programming Agenda

What is a Dependency?

ClassA +RequestService() ClassB public RequestService() { ClassB b = new ClassB() b.DoService(); } Dependencies dependent +DoService()

Layer Dependencies Presentation Layer Business Layer Data Access Layer Depends on High-level modules should not depend on low-level modules. Both should depend on abstractions Robert C. Martin DB Depends on Depends on

Why dependencies are evil? Tight coupling of software components Tight coupling of software components hard to change because every change affects too many other parts of the system (Rigidity) hard to change because every change affects too many other parts of the system (Rigidity) When you make a change, unexpected parts of the system break. (Fragility) When you make a change, unexpected parts of the system break. (Fragility) hard to reuse in another application. (Immobility) hard to reuse in another application. (Immobility) Software becomes hard to maintain Software becomes hard to maintain Difficult to isolate when testing Difficult to isolate when testing

Scenario

Copier Example Copier +PerformCopy() Keyboard +ReadFromKB(c : char) Video + WriteToVideo () : char

class Copier { Keyboard _reader; Keyboard _reader; Video _writer; Video _writer; public Copier() public Copier() { _reader = new Keyboard(); _reader = new Keyboard(); _writer = new Video(); _writer = new Video(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); _writer.WriteToVideo(chr); }} class Keyboard { public int ReadFromKeyboard() public int ReadFromKeyboard() { return Console.ReadKey(true).KeyChar; return Console.ReadKey(true).KeyChar; }} class Video { public void WriteToVideo(int chr) public void WriteToVideo(int chr) { Console.Write((char)chr); Console.Write((char)chr); }} Copier Example

class Copier { Keyboard _reader; Keyboard _reader; Video _writer; Video _writer; public Copier() public Copier() { _reader = new Keyboard(); _reader = new Keyboard(); _writer = new Video(); _writer = new Video(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); _writer.WriteToVideo(chr); }} class Keyboard { public int ReadFromKeyboard() public int ReadFromKeyboard() { return Console.ReadKey(true).KeyChar; return Console.ReadKey(true).KeyChar; }} class Video { public void WriteToVideo(int chr) public void WriteToVideo(int chr) { Console.Write((char)chr); Console.Write((char)chr); }} Copier Example Problem

Program to an interface, not an implementation

Copier Example > IWriterIReader Copier +PerformCopy() Keyboard +Read(c : char) Video + Write () : char Concrete class should depend on abstraction Robert Martin

class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier() public Copier() { _reader = new Keyboard(); _reader = new Keyboard(); _writer = new Video(); _writer = new Video(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} class Keyboard : IReader { public int Read() public int Read() { return Console.ReadKey(true).KeyChar; return Console.ReadKey(true).KeyChar; }} class Video : IWriter { public void Write(int chr) public void Write(int chr) { Console.Write((char)chr); Console.Write((char)chr); }} Copier Example

class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier() public Copier() { _reader = new Keyboard(); _reader = new Keyboard(); _writer = new Video(); _writer = new Video(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} class Keyboard : IReader { public int Read() public int Read() { return Console.ReadKey(true).KeyChar; return Console.ReadKey(true).KeyChar; }} class Video : IWriter { public void Write(int chr) public void Write(int chr) { Console.Write((char)chr); Console.Write((char)chr); }} Copier Example Problem Dependencies resolution is still here!!!

TowardsDecoupling

> Using a Factory > IWriterIReader Copier Keyboard +Read(c : char) Video + Write () : char ReaderFactory +GetInstance() : IReader +Copier(r : IReader, w : IWriter) +PerformCopy() WriterFactory +GetInstance() : IWriter

class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier() public Copier() { _reader = ReaderFactory.GetInstance(); _reader = ReaderFactory.GetInstance(); _writer = WriterFactory.GetInstance(); _writer = WriterFactory.GetInstance(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} class ReaderFactory { public static IReader GetInstance() public static IReader GetInstance() { return new Keyboard(); return new Keyboard(); }} class WriterFactory { public static IWriter GetInstance() public static IWriter GetInstance() { return new Video(); return new Video(); }} Using a Factory

class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier() public Copier() { _reader = ReaderFactory.GetInstance(); _reader = ReaderFactory.GetInstance(); _writer = WriterFactory.GetInstance(); _writer = WriterFactory.GetInstance(); } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} class ReaderFactory { public static IReader GetInstance() public static IReader GetInstance() { return new Keyboard(); return new Keyboard(); }} class WriterFactory { public static IWriter GetInstance() public static IWriter GetInstance() { return new Video(); return new Video(); }} Using a Factory We have just move the problem!!! Problem

> IWriterIReader Copier Keyboard +Read(c : char) Video + Write () : char +PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator Service Locator …

> IWriterIReader Copier Keyboard +Read(c : char) Video + Write () : char +PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator Service Locator … Assembler

Service Locator class ServiceLocator { /* Singleton instance */ /* Singleton instance */ private static ServiceLocator _instance; private static ServiceLocator _instance; public static void Load(ServiceLocator arg) public static void Load(ServiceLocator arg) { _instance = arg; _instance = arg; } /* Storing and Retrieve services */ /* Storing and Retrieve services */ private Dictionary _services = new Dictionary (); private Dictionary _services = new Dictionary (); public Object RegisterService(String key) public Object RegisterService(String key) { return _instance._services[key]; return _instance._services[key]; } public static void Lookup(String key, Object service) public static void Lookup(String key, Object service) { _services.Add(key, service); _services.Add(key, service); }}

Service Locator class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier() public Copier() { _reader = (IReader)ServiceLocator.Lookup("reader"); _reader = (IReader)ServiceLocator.Lookup("reader"); _writer = (IWriter)ServiceLocator.Lookup("writer"); ; _writer = (IWriter)ServiceLocator.Lookup("writer"); ; } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} /* Configure Service Locator method*/ /* Configure Service Locator method*/ private void configureLocator() { ServiceLocator locator = new ServiceLocator(); ServiceLocator locator = new ServiceLocator(); locator.LoadService("reader", new Keyboard()); locator.LoadService("reader", new Keyboard()); locator.LoadService("writer", new Video()); locator.LoadService("writer", new Video()); ServiceLocator.Load(locator); ServiceLocator.Load(locator);}

Pro Pro Help to avoid coupling Help to avoid coupling Centralize dependencies resolution Centralize dependencies resolution Cons Cons Introduce dependencies with the locator Introduce dependencies with the locator Difficult to test Difficult to test Service Locator

Inversion of Control

Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to traditional programming. Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to traditional programming. Hollywood Principle Hollywood Principle “don't call us, we'll call you.” There are many implementations of IoC, Dependency Injections is one of there. There are many implementations of IoC, Dependency Injections is one of there. What is IoC?

Technique for supplying an external dependency to a software component. Technique for supplying an external dependency to a software component. Implemented by a Container (IoC) Implemented by a Container (IoC) Creates and assembles component/objects and manages their lifecycle Creates and assembles component/objects and manages their lifecycle Generally configured by coding or external file Generally configured by coding or external file Three forms of injection Three forms of injection Constructor Injection Constructor Injection Setter Injection Setter Injection Interface Injection Interface Injection What is Dependency Injection?

IoC Container IWriterIReader Copier Keyboard +Read(c : char) Video + Write () : char +Copier(r : IReader, w : IWriter) +PerformCopy()

> IoC Container > IWriterIReader Copier Keyboard +Read(c : char) Video + Write () : char IoCContainer … +Copier(r : IReader, w : IWriter) +PerformCopy() XML Config

DI: Constructor Injection class Copier { IReader _reader; IReader _reader; IWriter _writer; IWriter _writer; public Copier(IReader reader, IWriter writer) public Copier(IReader reader, IWriter writer) { _reader = reader; _reader = reader; _writer = writer; _writer = writer; } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ( (chr = _reader.Read() ) != '\r') while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); _writer.Write(chr); }} To prefer in case of Mandatory Dependencies

DI: Setter Injection class Copier { private IReader _reader; private IReader _reader; public IReader Reader public IReader Reader { set{_reader = value;} set{_reader = value;} } private IWriter _writer; private IWriter _writer; public IWriter Writer public IWriter Writer { set{_writer = value;} set{_writer = value;} } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ((chr = _reader.Read()) != '\r') while ((chr = _reader.Read()) != '\r') _writer.Write(chr); _writer.Write(chr); }} To prefer in case of Optional Dependencies

DI: Interface Injection class Copier { private IReader _reader; private IReader _reader; public void InjectReader(IReader reader) public void InjectReader(IReader reader) { _reader = reader; _reader = reader; } private IWriter _writer; private IWriter _writer; public void InjectWriter(IWriter writer) public void InjectWriter(IWriter writer) { _writer = writer; _writer = writer; } public void PerformCopy() public void PerformCopy() { int chr; int chr; while ((chr = _reader.Read()) != '\r') while ((chr = _reader.Read()) != '\r') _writer.Write(chr); _writer.Write(chr); }} interface IReaderInject { void injectReader(IReader reader); void injectReader(IReader reader);} interface IWriterInject { void injectWriter(IWriter reader); void injectWriter(IWriter reader);} Quite Never Used

Pro Pro High Decoupling level High Decoupling level Completely transparent to the Domain Model Completely transparent to the Domain Model High Configurable High Configurable Integrated with lots of framework Integrated with lots of framework Cons Cons Difficult to understand by newbie Difficult to understand by newbie Complex to debug Complex to debug DI: Consideration

Aspect Oriented Programming

AOP is a programming paradigm AOP is a programming paradigm AOP is a new way of thinking about software design AOP is a new way of thinking about software design Enhance OOP in separating concerns to improve modularization Enhance OOP in separating concerns to improve modularization OOP modularizes concerns OOP modularizes concerns AOP addresses cross-cutting concerns AOP addresses cross-cutting concerns What is AOP?

Separation of Concern Searching Booking Payment

Separation of Concern BookingPaymentSearching OOP Searching Booking Payment Reduce software complexity Reduce software complexity Limit the impact of change Limit the impact of change Facilitate reuse Facilitate reuse Simplify components integration Simplify components integration

Crosscutting Concern Booking Security Logging Payment Security Logging Searching Security Logging OOP Crosscutting concerns are functionalities that span multiple modules Crosscutting concerns are functionalities that span multiple modules Security Security Logging Logging Transaction Management Transaction Management … Hard to model with traditional OOP approach Hard to model with traditional OOP approach Code Scattering Code Scattering Code Tangling Code Tangling Crosscutting Concerns Searching Booking Payment

Crosscutting Concern Booking Payment Searching Security Logging AOP Booking Security Logging Payment Security Logging Searching Security Logging OOP Crosscutting Concerns Searching Booking Payment

Object_BObject_A How it works… method_Amethod_B Object Oriented Flow

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Aspect Aspect Equivalent to class in OOP Equivalent to class in OOP Used to describe a Crosscutting Concern Used to describe a Crosscutting Concern

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Join Point Join Point Well-defined point during the execution of a program (e.g. method invocation, attribute access, exception handling,…) Well-defined point during the execution of a program (e.g. method invocation, attribute access, exception handling,…)

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Advice Advice action taken by an aspect at a particular join point action taken by an aspect at a particular join point

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Pointcut Pointcut Predicate that matches join points Predicate that matches join points Advice is associated with a pointcut expression and runs at any join point matched by the pointcut Advice is associated with a pointcut expression and runs at any join point matched by the pointcut

Aspect Object_BObject_A How it works… method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Target Object Target Object Also called Advised Object Also called Advised Object Object being advised by one or more aspects Object being advised by one or more aspects

Aspect Object_BObject_A method_Amethod_B advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Weaving Weaving Weaving Run-Time Weaving Run-Time Weaving Compile-Time Weaving Compile-Time Weaving … behind the scenes

References Martin Fowler Martin Fowler Inversion of Control Containers and the Dependency Injection pattern Inversion of Control Containers and the Dependency Injection pattern Robert C. Martin Robert C. Martin The Dependency Inversion Principle The Dependency Inversion Principle AOP Alliance AOP Alliance

Questions?

Slide and Materials Grazie!