Technology

Manage software dependencies with ioc and aop

Categories
Published
of 49
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Description
1. Manage software dependencies with IoC and AOP Stefano Leli 14° Workshop DotNetMarche Friday 16 th April 2010 @sleli [email_address] 2. <ul><li>Software…
Transcript
  • 1. Manage software dependencies with IoC and AOP Stefano Leli 14° Workshop DotNetMarche Friday 16 th April 2010 @sleli [email_address]
  • 2. <ul><li>Software Dependencies </li></ul><ul><li>Service Locator </li></ul><ul><li>Inversion of Control </li></ul><ul><li>Dependency Injection </li></ul><ul><li>Aspect Oriented Programming </li></ul>Agenda
  • 3. <ul><li>What is a Dependency? </li></ul>
  • 4. Dependencies public RequestService() { ClassB b = new ClassB() b.DoService(); } dependent +DoService() ClassA +RequestService() ClassB
  • 5. Layer Dependencies <ul><li>High-level modules should not depend on low-level modules. Both should depend on abstractions </li></ul><ul><li>Robert C. Martin </li></ul>Presentation Layer Business Layer Data Access Layer Depends on DB Depends on Depends on
  • 6. Why dependencies are evil? <ul><li>Tight coupling of software components </li></ul><ul><ul><li>hard to change because every change affects too many other parts of the system (Rigidity) </li></ul></ul><ul><ul><li>When you make a change, unexpected parts of the system break. (Fragility) </li></ul></ul><ul><ul><li>hard to reuse in another application. (Immobility) </li></ul></ul><ul><li>Software becomes hard to maintain </li></ul><ul><li>Difficult to isolate when testing </li></ul>
  • 7. <ul><li>Scenario </li></ul>
  • 8. Copier Example Copier + PerformCopy() Keyboard + ReadFromKB(c : char ) Video + WriteToVideo () : char
  • 9. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>Keyboard _reader; </li></ul><ul><li>Video _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_reader = new Keyboard(); </li></ul><ul><li>_writer = new Video(); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.ReadFromKeyboard() ) != ' ') </li></ul><ul><li>_writer.WriteToVideo(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Copier Example class Keyboard { public int ReadFromKeyboard() { return Console.ReadKey(true).KeyChar; } } class Video { public void WriteToVideo(int chr) { Console.Write((char)chr); } }
  • 10. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>Keyboard _reader; </li></ul><ul><li>Video _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_ reader = new Keyboard(); </li></ul><ul><li>_writer = new Video(); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.ReadFromKeyboard() ) != ' ') </li></ul><ul><li>_writer.WriteToVideo(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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. <ul><li>Program to an interface, not an implementation </li></ul>
  • 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. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_reader = new Keyboard(); </li></ul><ul><li>_writer = new Video(); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_reader = new Keyboard(); </li></ul><ul><li>_writer = new Video(); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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!!!
  • 15. <ul><li>Towards </li></ul><ul><li>Decoupling </li></ul>
  • 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. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_ reader = ReaderFactory. GetInstance (); </li></ul><ul><li>_writer = WriterFactory. GetInstance (); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Using a Factory class ReaderFactory { public static IReader GetInstance() { return new Keyboard(); } } class WriterFactory { public static IWriter GetInstance() { return new Video(); } }
  • 18. <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_reader = ReaderFactory. GetInstance (); </li></ul><ul><li>_writer = WriterFactory. GetInstance (); </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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 <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier() </li></ul><ul><li>{ </li></ul><ul><li>_reader = (IReader)ServiceLocator.Lookup(&quot;reader&quot;); </li></ul><ul><li>_writer = (IWriter)ServiceLocator.Lookup(&quot;writer&quot;); ; </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>/* 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. <ul><li>Pro </li></ul><ul><ul><li>Help to avoid coupling </li></ul></ul><ul><ul><li>Centralize dependencies resolution </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Introduce dependencies with the locator </li></ul></ul><ul><ul><li>Difficult to test </li></ul></ul>Service Locator
  • 24. <ul><li>I nversion </li></ul><ul><li>o f </li></ul><ul><li>C ontrol </li></ul>
  • 25. <ul><li>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. </li></ul><ul><li>Hollywood Principle </li></ul><ul><ul><li>“ don't call us, we'll call you.” </li></ul></ul><ul><li>There are many implementations of IoC, Dependency Injections is one of there. </li></ul>What is IoC?
  • 26. <ul><li>Technique for supplying an external dependency to a software component. </li></ul><ul><li>Implemented by a Container (IoC) </li></ul><ul><ul><li>Creates and assembles component/objects and manages their lifecycle </li></ul></ul><ul><ul><li>Generally configured by coding or external file </li></ul></ul><ul><li>Three forms of injection </li></ul><ul><ul><li>Constructor Injection </li></ul></ul><ul><ul><li>Setter Injection </li></ul></ul><ul><ul><li>Interface Injection </li></ul></ul>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 <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>IReader _reader; </li></ul><ul><li>IWriter _writer; </li></ul><ul><li>public Copier( IReader reader, IWriter writer ) </li></ul><ul><li>{ </li></ul><ul><li>_reader = reader; </li></ul><ul><li>_writer = writer; </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ( (chr = _reader.Read() ) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>To prefer in case of Mandatory Dependencies
  • 30. DI: Setter Injection <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>private IReader _reader; </li></ul><ul><li>public IReader Reader </li></ul><ul><li>{ </li></ul><ul><li>set{_reader = value;} </li></ul><ul><li>} </li></ul><ul><li>private IWriter _writer ; </li></ul><ul><li>public IWriter Writer </li></ul><ul><li>{ </li></ul><ul><li>set{_writer = value;} </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ((chr = _reader.Read()) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>To prefer in case of Optional Dependencies
  • 31. DI: Interface Injection <ul><li>class Copier </li></ul><ul><li>{ </li></ul><ul><li>private IReader _reader; </li></ul><ul><li>public void InjectReader(IReader reader) </li></ul><ul><li>{ </li></ul><ul><li>_reader = reader; </li></ul><ul><li>} </li></ul><ul><li>private IWriter _writer; </li></ul><ul><li>public void InjectWriter(IWriter writer) </li></ul><ul><li>{ </li></ul><ul><li>_writer = writer; </li></ul><ul><li>} </li></ul><ul><li>public void PerformCopy() </li></ul><ul><li>{ </li></ul><ul><li>int chr; </li></ul><ul><li>while ((chr = _reader.Read()) != ' ') </li></ul><ul><li>_writer.Write(chr); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>interface IReaderInject { void injectReader(IReader reader); } interface IWriterInject { void injectWriter(IWriter reader); } Quite Never Used
  • 32. <ul><li>Pro </li></ul><ul><ul><li>High Decoupling level </li></ul></ul><ul><ul><li>Completely transparent to the Domain Model </li></ul></ul><ul><ul><li>High Configurable </li></ul></ul><ul><ul><li>Integrated with lots of framework </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Difficult to understand by newbie </li></ul></ul><ul><ul><li>Complex to debug </li></ul></ul>DI: Consideration
  • 33. <ul><li>A spect </li></ul><ul><li>O riented </li></ul><ul><li>P rogramming </li></ul>
  • 34. <ul><li>AOP is a programming paradigm </li></ul><ul><li>AOP is a new way of thinking about software design </li></ul><ul><li>Enhance OOP in separating concerns to improve modularization </li></ul><ul><li>OOP modularizes concerns </li></ul><ul><li>AOP addresses cross-cutting concerns </li></ul>What is AOP?
  • 35. Separation of Concern Searching Booking Payment
  • 36. Separation of Concern <ul><li>Reduce software complexity </li></ul><ul><li>Limit the impact of change </li></ul><ul><li>Facilitate reuse </li></ul><ul><li>Simplify components integration </li></ul>Booking Payment Searching OOP Searching Booking Payment
  • 37. Crosscutting Concern <ul><li>Crosscutting concerns are functionalities that span multiple modules </li></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Logging </li></ul></ul><ul><ul><li>Transaction Management </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><li>Hard to model with traditional OOP approach </li></ul><ul><ul><li>Code Scattering </li></ul></ul><ul><ul><li>Code Tangling </li></ul></ul>Boo
  • Search
    Related Search
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks