SlideShare a Scribd company logo
Manage software dependencies with IoC and AOP Stefano Leli 14° Workshop DotNetMarche Friday 16 th  April 2010 @sleli [email_address]
Software Dependencies Service Locator Inversion of Control Dependency Injection Aspect Oriented Programming Agenda
What is a Dependency?
Dependencies public RequestService() { ClassB b = new ClassB() b.DoService(); } dependent +DoService() ClassA +RequestService() ClassB
Layer Dependencies High-level modules should not depend on low-level modules. Both should depend on abstractions Robert C. Martin Presentation Layer Business Layer Data Access Layer Depends  on DB Depends  on Depends  on
Why dependencies are evil? Tight coupling of software components 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) hard to reuse in another application. (Immobility) Software becomes hard to maintain Difficult to isolate when testing
Scenario
Copier Example Copier + PerformCopy() Keyboard + ReadFromKB(c : char ) Video + WriteToVideo ()  : char
class Copier { Keyboard _reader; Video _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); } } Copier Example class Keyboard { public int ReadFromKeyboard() {  return Console.ReadKey(true).KeyChar; } } class Video { public void WriteToVideo(int chr) { Console.Write((char)chr); } }
class Copier { Keyboard _reader; Video _writer; public Copier() { _ reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); } } Copier Example class Keyboard { public int ReadFromKeyboard() {  return Console.ReadKey(true).KeyChar; } } class Video { public void WriteToVideo(int chr) { Console.Write((char)chr); } } Problem
Program to an interface, not an implementation
Copier Example <<create>> <<create>> Concrete class should depend on abstraction Robert Martin IWriter IReader Copier + PerformCopy() Keyboard + Read(c : char ) Video + Write ()  : char
class Copier { IReader  _reader; IWriter  _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Copier Example class Keyboard  : IReader { public int  Read () {  return Console.ReadKey(true).KeyChar; } } class Video  : IWriter { public void  Write (int chr) { Console.Write((char)chr); } }
class Copier { IReader  _reader; IWriter  _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Copier Example class Keyboard  : IReader { public int  Read () {  return Console.ReadKey(true).KeyChar; } } class Video  : IWriter { public void  Write (int chr) { Console.Write((char)chr); } } Problem Dependencies resolution is still here!!!
Towards  Decoupling
<<create>> Using a Factory <<create>> Copier + Copier(r : IReader, w : IWriter) + PerformCopy() IWriter IReader Keyboard + Read(c : char ) Video + Write ()  : char ReaderFactory + GetInstance() : IReader WriterFactory + GetInstance() : IWriter
class Copier { IReader _reader; IWriter _writer; public Copier() { _ reader = ReaderFactory. GetInstance (); _writer = WriterFactory. GetInstance (); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Using a Factory class ReaderFactory { public static IReader GetInstance() { return new Keyboard(); } } class WriterFactory { public static IWriter GetInstance() { return new Video(); } }
class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = ReaderFactory. GetInstance (); _writer = WriterFactory. GetInstance (); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Using a Factory class ReaderFactory { public static IReader GetInstance() { return new Keyboard(); } } class WriterFactory { public static IWriter GetInstance() { return new Video(); } } We have just moved the problem!!! Problem
Service Locator << create >> <<create>> Copier + PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator … IWriter IReader Keyboard + Read(c : char ) Video + Write ()  : char
Service Locator << create >> <<create>> Copier + PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator … IWriter IReader Keyboard + Read(c : char ) Video + Write ()  : char
Service Locator class ServiceLocator { /* Singleton instance */ private static ServiceLocator _instance;  public static void Load(ServiceLocator arg) { _instance = arg; } /* Storing and Retrieve services */ private Dictionary<string, Object> _services = new Dictionary<string, Object>(); public Object RegisterService(String key) { return _instance._services[key]; } public static void Lookup(String key, Object service) { _services.Add(key, service); } }
Service Locator class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = (IReader)ServiceLocator.Lookup(&quot;reader&quot;); _writer = (IWriter)ServiceLocator.Lookup(&quot;writer&quot;); ; } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } /*  Configure Service Locator method */ private void configureLocator() { ServiceLocator locator = new ServiceLocator();  locator.LoadService(&quot;reader&quot;, new Keyboard()); locator.LoadService(&quot;writer&quot;, new Video()); ServiceLocator.Load(locator); }
Pro Help to avoid coupling Centralize dependencies resolution Cons Introduce dependencies with the locator Difficult to test Service Locator
I nversion  o f  C ontrol
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 “ don't call us, we'll call you.” 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. Implemented by a Container (IoC) Creates and assembles component/objects and manages their lifecycle Generally configured by coding or external file Three forms of injection Constructor Injection Setter Injection Interface Injection What is Dependency Injection?
IoC Container Copier + Copier(r : IReader, w : IWriter) + PerformCopy() IWriter IReader Keyboard + Read(c : char ) Video + Write ()  : char
<< create >> <<create>> IoC Container <<create>> Copier + Copier(r : IReader, w : IWriter) + PerformCopy() XML Config IWriter IReader Keyboard + Read(c : char ) Video + Write ()  : char IoCContainer …
DI: Constructor Injection class Copier { IReader _reader; IWriter _writer; public Copier( IReader reader, IWriter writer ) { _reader = reader; _writer = writer; } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } To prefer in case of Mandatory Dependencies
DI: Setter Injection class Copier {  private IReader _reader; public IReader Reader { set{_reader = value;} } private IWriter _writer ; public IWriter Writer { set{_writer = value;} } public void PerformCopy() { int chr; while ((chr = _reader.Read()) != '\r') _writer.Write(chr); } } To prefer in case of Optional Dependencies
DI: Interface Injection class Copier {  private IReader _reader; public void InjectReader(IReader reader) {  _reader = reader; } private IWriter _writer; public void InjectWriter(IWriter writer) {  _writer = writer;  } public void PerformCopy() { int chr; while ((chr = _reader.Read()) != '\r') _writer.Write(chr); } } interface IReaderInject { void injectReader(IReader reader);  } interface IWriterInject { void injectWriter(IWriter reader);  } Quite Never Used
Pro High Decoupling level Completely transparent to the Domain Model High Configurable Integrated with lots of framework Cons Difficult to understand by newbie Complex to debug DI: Consideration
A spect O riented  P rogramming
AOP is a programming paradigm AOP is a new way of thinking about software design Enhance OOP in separating concerns to improve modularization OOP modularizes concerns AOP addresses  cross-cutting concerns What is AOP?
Separation of Concern Searching Booking Payment
Separation of Concern Reduce software complexity Limit the impact of change Facilitate reuse Simplify components integration Booking Payment Searching OOP Searching Booking Payment
Crosscutting Concern Crosscutting concerns are functionalities that span multiple modules Security Logging Transaction Management … Hard to model with traditional OOP approach Code Scattering  Code Tangling Booking Security Logging Payment Security Logging Searching Security Logging OOP 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
How it works… Object_B Object_A Object Oriented Flow
How it works… Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
How it works… Aspect Equivalent to class in OOP Used to describe a Crosscutting Concern Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
How it works… Join Point Well-defined point during the execution of a program (e.g. method invocation, attribute access, exception handling,…) Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
How it works… Advice action taken by an aspect at a particular join point Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
How it works… Pointcut Predicate that matches join points Advice is associated with a pointcut expression and runs at any join point matched by the pointcut Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
How it works… Target Object Also called Advised Object Object being advised by one or more aspects Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
Weaving Run-Time Weaving Compile-Time Weaving …  behind the scenes Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Weaving
References Martin Fowler Inversion of Control Containers and the Dependency Injection pattern Robert C. Martin The Dependency Inversion Principle AOP Alliance
Questions?
Slide and Materials www.dotnetmarche.org Grazie!

More Related Content

PDF
The Ring programming language version 1.5.2 book - Part 176 of 181
PDF
The Ring programming language version 1.5.4 book - Part 180 of 185
PDF
Kotlin: Challenges in JVM language design
PPT
Runtime Environment Of .Net Divya Rathore
PDF
Matteo Vaccari - TDD per Android | Codemotion Milan 2015
PPTX
Chapter i c#(console application and programming)
PDF
Doing the Impossible
PDF
EKON 25 Python4Delphi_mX4
The Ring programming language version 1.5.2 book - Part 176 of 181
The Ring programming language version 1.5.4 book - Part 180 of 185
Kotlin: Challenges in JVM language design
Runtime Environment Of .Net Divya Rathore
Matteo Vaccari - TDD per Android | Codemotion Milan 2015
Chapter i c#(console application and programming)
Doing the Impossible
EKON 25 Python4Delphi_mX4

What's hot (20)

PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PDF
64-bit Loki
PDF
Writing usableap isinpractice
PPTX
C# 6.0 - DotNetNotts
PPTX
Eval4j @ JVMLS 2014
PDF
IronSmalltalk
PPTX
Demonstration Of The Open Mi
PDF
Ekon 25 Python4Delphi_MX475
PDF
Introduction to cdi given at java one 2014
PPTX
An Overview of Project Jigsaw
PPT
The Kotlin Programming Language
PDF
Google Dart
PPT
Parm
PDF
Why Spring <3 Kotlin
PPTX
From code to pattern, part one
PDF
Bytecode manipulation with Javassist and ASM
PPT
PDF
Living With Legacy Code
PDF
RMI (Remote Method Invocation)
PDF
ADG Poznań - Kotlin for Android developers
JVM Mechanics: When Does the JVM JIT & Deoptimize?
64-bit Loki
Writing usableap isinpractice
C# 6.0 - DotNetNotts
Eval4j @ JVMLS 2014
IronSmalltalk
Demonstration Of The Open Mi
Ekon 25 Python4Delphi_MX475
Introduction to cdi given at java one 2014
An Overview of Project Jigsaw
The Kotlin Programming Language
Google Dart
Parm
Why Spring <3 Kotlin
From code to pattern, part one
Bytecode manipulation with Javassist and ASM
Living With Legacy Code
RMI (Remote Method Invocation)
ADG Poznań - Kotlin for Android developers
Ad

Similar to Manage software dependencies with ioc and aop (20)

PDF
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
PPTX
Functional programming in C#
PDF
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
PPTX
Intro to object oriented programming
PPTX
Most Useful Design Patterns
PPTX
Inversion of Control and Dependency Injection
PPTX
Polaris presentation ioc - code conference
PPTX
Oleksandr Valetskyy - DI vs. IoC
PPTX
Dependency injection with unity 2.0 Dmytro Mindra Lohika
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
PPT
Design poo my_jug_en_ppt
PPT
Software development effort reduction with Co-op
PPT
N depend & cql
PPTX
Presentation.pptx
PPTX
Common ASP.NET Design Patterns - Telerik India DevCon 2013
PPT
Padroes Projeto
PPTX
SOLID & IoC Principles
PDF
SOLID_WriteUp
PDF
Design Pattern Observations
Lego For Engineers - Dependency Injection for LIDNUG (2011-06-03)
Functional programming in C#
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Clean Code Part II - Dependency Injection at SoCal Code Camp
Intro to object oriented programming
Most Useful Design Patterns
Inversion of Control and Dependency Injection
Polaris presentation ioc - code conference
Oleksandr Valetskyy - DI vs. IoC
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Design poo my_jug_en_ppt
Software development effort reduction with Co-op
N depend & cql
Presentation.pptx
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Padroes Projeto
SOLID & IoC Principles
SOLID_WriteUp
Design Pattern Observations
Ad

More from Stefano Leli (17)

PDF
Agile quackery a brief history of the worst ways to cure everything
PDF
Agile goes Hollywood - Un approccio empirico alle trasformazioni agili
PDF
Succeding with feature teams
PDF
User Story Mapping
PDF
La tua prima kanban board
PDF
Dinosaur Carpaccio - How to implement valuable micro-requirements
PDF
From Vision To Product
PDF
Agile retrospective,an example
PDF
User stories writing - Codemotion 2013
PDF
User Stories Writing
PDF
Codice legacy, usciamo dal pantano! @iad11
PDF
XP Game
PDF
Il project manager e lo sviluppo agile. Separati in casa?
PDF
Codice legacy, usciamo dal pantano!
PDF
Design Pattern In Pratica
PDF
Workshop Su Refactoring
PDF
Intoduzione Alle Metodologie Agili
Agile quackery a brief history of the worst ways to cure everything
Agile goes Hollywood - Un approccio empirico alle trasformazioni agili
Succeding with feature teams
User Story Mapping
La tua prima kanban board
Dinosaur Carpaccio - How to implement valuable micro-requirements
From Vision To Product
Agile retrospective,an example
User stories writing - Codemotion 2013
User Stories Writing
Codice legacy, usciamo dal pantano! @iad11
XP Game
Il project manager e lo sviluppo agile. Separati in casa?
Codice legacy, usciamo dal pantano!
Design Pattern In Pratica
Workshop Su Refactoring
Intoduzione Alle Metodologie Agili

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Electronic commerce courselecture one. Pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Electronic commerce courselecture one. Pdf
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Manage software dependencies with ioc and aop

  • 1. Manage software dependencies with IoC and AOP Stefano Leli 14° Workshop DotNetMarche Friday 16 th April 2010 @sleli [email_address]
  • 2. Software Dependencies Service Locator Inversion of Control Dependency Injection Aspect Oriented Programming Agenda
  • 3. What is a Dependency?
  • 4. Dependencies public RequestService() { ClassB b = new ClassB() b.DoService(); } dependent +DoService() ClassA +RequestService() ClassB
  • 5. Layer Dependencies High-level modules should not depend on low-level modules. Both should depend on abstractions Robert C. Martin Presentation Layer Business Layer Data Access Layer Depends on DB Depends on Depends on
  • 6. Why dependencies are evil? Tight coupling of software components 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) hard to reuse in another application. (Immobility) Software becomes hard to maintain Difficult to isolate when testing
  • 8. Copier Example Copier + PerformCopy() Keyboard + ReadFromKB(c : char ) Video + WriteToVideo () : char
  • 9. class Copier { Keyboard _reader; Video _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); } } Copier Example class Keyboard { public int ReadFromKeyboard() { return Console.ReadKey(true).KeyChar; } } class Video { public void WriteToVideo(int chr) { Console.Write((char)chr); } }
  • 10. class Copier { Keyboard _reader; Video _writer; public Copier() { _ reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.ReadFromKeyboard() ) != '\r') _writer.WriteToVideo(chr); } } Copier Example class Keyboard { public int ReadFromKeyboard() { return Console.ReadKey(true).KeyChar; } } class Video { public void WriteToVideo(int chr) { Console.Write((char)chr); } } Problem
  • 11. Program to an interface, not an implementation
  • 12. Copier Example <<create>> <<create>> Concrete class should depend on abstraction Robert Martin IWriter IReader Copier + PerformCopy() Keyboard + Read(c : char ) Video + Write () : char
  • 13. class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Copier Example class Keyboard : IReader { public int Read () { return Console.ReadKey(true).KeyChar; } } class Video : IWriter { public void Write (int chr) { Console.Write((char)chr); } }
  • 14. class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = new Keyboard(); _writer = new Video(); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Copier Example class Keyboard : IReader { public int Read () { return Console.ReadKey(true).KeyChar; } } class Video : IWriter { public void Write (int chr) { Console.Write((char)chr); } } Problem Dependencies resolution is still here!!!
  • 16. <<create>> Using a Factory <<create>> Copier + Copier(r : IReader, w : IWriter) + PerformCopy() IWriter IReader Keyboard + Read(c : char ) Video + Write () : char ReaderFactory + GetInstance() : IReader WriterFactory + GetInstance() : IWriter
  • 17. class Copier { IReader _reader; IWriter _writer; public Copier() { _ reader = ReaderFactory. GetInstance (); _writer = WriterFactory. GetInstance (); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Using a Factory class ReaderFactory { public static IReader GetInstance() { return new Keyboard(); } } class WriterFactory { public static IWriter GetInstance() { return new Video(); } }
  • 18. class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = ReaderFactory. GetInstance (); _writer = WriterFactory. GetInstance (); } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } Using a Factory class ReaderFactory { public static IReader GetInstance() { return new Keyboard(); } } class WriterFactory { public static IWriter GetInstance() { return new Video(); } } We have just moved the problem!!! Problem
  • 19. Service Locator << create >> <<create>> Copier + PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator … IWriter IReader Keyboard + Read(c : char ) Video + Write () : char
  • 20. Service Locator << create >> <<create>> Copier + PerformCopy() ServiceLocator +Lookup() : Object +RegisterService(o : Object) _instance : ServiceLocator … IWriter IReader Keyboard + Read(c : char ) Video + Write () : char
  • 21. Service Locator class ServiceLocator { /* Singleton instance */ private static ServiceLocator _instance; public static void Load(ServiceLocator arg) { _instance = arg; } /* Storing and Retrieve services */ private Dictionary<string, Object> _services = new Dictionary<string, Object>(); public Object RegisterService(String key) { return _instance._services[key]; } public static void Lookup(String key, Object service) { _services.Add(key, service); } }
  • 22. Service Locator class Copier { IReader _reader; IWriter _writer; public Copier() { _reader = (IReader)ServiceLocator.Lookup(&quot;reader&quot;); _writer = (IWriter)ServiceLocator.Lookup(&quot;writer&quot;); ; } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } /* Configure Service Locator method */ private void configureLocator() { ServiceLocator locator = new ServiceLocator(); locator.LoadService(&quot;reader&quot;, new Keyboard()); locator.LoadService(&quot;writer&quot;, new Video()); ServiceLocator.Load(locator); }
  • 23. Pro Help to avoid coupling Centralize dependencies resolution Cons Introduce dependencies with the locator Difficult to test Service Locator
  • 24. I nversion o f C ontrol
  • 25. 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 “ don't call us, we'll call you.” There are many implementations of IoC, Dependency Injections is one of there. What is IoC?
  • 26. Technique for supplying an external dependency to a software component. Implemented by a Container (IoC) Creates and assembles component/objects and manages their lifecycle Generally configured by coding or external file Three forms of injection Constructor Injection Setter Injection Interface Injection What is Dependency Injection?
  • 27. IoC Container Copier + Copier(r : IReader, w : IWriter) + PerformCopy() IWriter IReader Keyboard + Read(c : char ) Video + Write () : char
  • 28. << create >> <<create>> IoC Container <<create>> Copier + Copier(r : IReader, w : IWriter) + PerformCopy() XML Config IWriter IReader Keyboard + Read(c : char ) Video + Write () : char IoCContainer …
  • 29. DI: Constructor Injection class Copier { IReader _reader; IWriter _writer; public Copier( IReader reader, IWriter writer ) { _reader = reader; _writer = writer; } public void PerformCopy() { int chr; while ( (chr = _reader.Read() ) != '\r') _writer.Write(chr); } } To prefer in case of Mandatory Dependencies
  • 30. DI: Setter Injection class Copier { private IReader _reader; public IReader Reader { set{_reader = value;} } private IWriter _writer ; public IWriter Writer { set{_writer = value;} } public void PerformCopy() { int chr; while ((chr = _reader.Read()) != '\r') _writer.Write(chr); } } To prefer in case of Optional Dependencies
  • 31. DI: Interface Injection class Copier { private IReader _reader; public void InjectReader(IReader reader) { _reader = reader; } private IWriter _writer; public void InjectWriter(IWriter writer) { _writer = writer; } public void PerformCopy() { int chr; while ((chr = _reader.Read()) != '\r') _writer.Write(chr); } } interface IReaderInject { void injectReader(IReader reader); } interface IWriterInject { void injectWriter(IWriter reader); } Quite Never Used
  • 32. Pro High Decoupling level Completely transparent to the Domain Model High Configurable Integrated with lots of framework Cons Difficult to understand by newbie Complex to debug DI: Consideration
  • 33. A spect O riented P rogramming
  • 34. AOP is a programming paradigm AOP is a new way of thinking about software design Enhance OOP in separating concerns to improve modularization OOP modularizes concerns AOP addresses cross-cutting concerns What is AOP?
  • 35. Separation of Concern Searching Booking Payment
  • 36. Separation of Concern Reduce software complexity Limit the impact of change Facilitate reuse Simplify components integration Booking Payment Searching OOP Searching Booking Payment
  • 37. Crosscutting Concern Crosscutting concerns are functionalities that span multiple modules Security Logging Transaction Management … Hard to model with traditional OOP approach Code Scattering Code Tangling Booking Security Logging Payment Security Logging Searching Security Logging OOP Crosscutting Concerns Searching Booking Payment
  • 38. Crosscutting Concern Booking Payment Searching Security Logging AOP Booking Security Logging Payment Security Logging Searching Security Logging OOP Crosscutting Concerns Searching Booking Payment
  • 39. How it works… Object_B Object_A Object Oriented Flow
  • 40. How it works… Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 41. How it works… Aspect Equivalent to class in OOP Used to describe a Crosscutting Concern Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 42. How it works… Join Point Well-defined point during the execution of a program (e.g. method invocation, attribute access, exception handling,…) Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 43. How it works… Advice action taken by an aspect at a particular join point Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 44. How it works… Pointcut Predicate that matches join points Advice is associated with a pointcut expression and runs at any join point matched by the pointcut Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 45. How it works… Target Object Also called Advised Object Object being advised by one or more aspects Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation
  • 46. Weaving Run-Time Weaving Compile-Time Weaving … behind the scenes Aspect Object_B Object_A advice Object Oriented Flow Aspect Oriented Flow pointcut = method_B Target Object = Object_B jointpoint = method invocation Weaving
  • 47. References Martin Fowler Inversion of Control Containers and the Dependency Injection pattern Robert C. Martin The Dependency Inversion Principle AOP Alliance
  • 49. Slide and Materials www.dotnetmarche.org Grazie!