1 / 23

Developing loosely coupled systems with Dependency Injection, PicoContainer, NanoContainer and AOP

Developing loosely coupled systems with Dependency Injection, PicoContainer, NanoContainer and AOP. Aslak Hellesøy Senior Developer - ThoughtWorks. My geekwork. Agenda. Dependency Injection PicoContainer NanoContainer Questions/Discussion.

palma
Download Presentation

Developing loosely coupled systems with Dependency Injection, PicoContainer, NanoContainer and AOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Developing loosely coupled systems withDependency Injection,PicoContainer, NanoContainer and AOP Aslak Hellesøy Senior Developer - ThoughtWorks

  2. My geekwork

  3. Agenda • Dependency Injection • PicoContainer • NanoContainer • Questions/Discussion

  4. “Inversion of Control is about software Components doing what they are told, when they are told. Your OO application could well become unmaintainable without it.” Paul Hammant, ThoughtWorks • IoC – “The Hollywood Principle” • Dependency Injection • Dependency Inversion (DIP) • Lifecycle

  5. ? ? ? ? ? ? ? ? ? ? ? Complex dependencies • How do these relationships get established?

  6. A simple example Trader Ticker ? • A Stock Trader that buys stocks. • Gets the price from a Stock Ticker. • How does the Trader get wired to the Ticker?

  7. Explicit instantiation publicclass Trader { private Ticker ticker = new Ticker(); } Trader Ticker new

  8. Singleton ServiceLocator publicclass Trader { private Ticker ticker = (Ticker) ServiceLocator.getInstance(). get(Ticker.class); } Make/ Retrieve Cached Ticker Trader Ticker Give me a Ticker ServiceLocator

  9. Dependency Injection publicclass Trader { privatefinal Ticker ticker; public Trader(Ticker ticker) { this.ticker = ticker; } } Ticker Trader

  10. Summary new • Explicit instantiation • Components instantiate their own dependencies • Kitchen sink problem • Inhibits unit testing • Service Locator • Components look up dependencies in a well known place • Invasive • Inhibits unit testing • Dependency Injection • Components don’t reach out to retrieve dependencies • Instead they are handed dependencies by an external entity • Non-Invasive • Unit testing friendly Service Locator Dependency Injection

  11. Dependency Inversion • Favours loose coupling  testability in isolation • Components should be split in two parts • Service, a declaration of offered functionality (Java interface) • Implementation, a specific implementation of a service (class) • Makes multiple runtime coupling combinations easy • Breaks the dreaded “everything depends on everything” problem C1 I C1 C2 C2

  12. “I was expecting a paradigm shift and all I got was a lousy constructor” Zohar Melamed, BNP Paribas

  13. What does PicoContainer do? • A simple Dependency Injection container • It works like a hash table on mescaline • Object instantiation • Dependency injection • Pluggable lifecycle management • Dependency hierarchies • Extensible via API and NanoContainer 1. Register components 2. Materialize and lace the components

  14. PicoContainer pico container classes objects

  15. Our toy app TraderScheduler Trader Ticker SimpleTrader Market RandomTicker StreamProtocolMarket

  16. PicoContainer in action! [ALT+TAB]

  17. NanoContainer • Script front-end for PicoContainer • Allows soft assembly of components in: • Groovy • Beanshell • Javascript (Rhino) • Jython • XML • Adds powerful AOP capability (aopalliance/dynaop) • Various other extensions • WebWork 1&2 • Struts • Hibernate • JMX • And much more…

  18. NanoContainer in action! [ALT+TAB]

  19. Summary • Dependency Injection Pattern • Internal decoupling • Testability • Configurability • No-bullshit components • PicoContainer • Low level Java API • Dependency Injection • Component Lifecycle • Non-invasive • NanoContainer • Several high level script APIs • AOP • Integration with other frameworks

  20. Container Hierarchies • No more static singletons • Components in parent visible • Lifecycle propagated down • Core of complex assemblies • Parallel to classloaders

  21. Web app scope(javax.servlet.ServletContext) Session scope(javax.servlet.http.HttpSession) Request scope(javax.servlet.ServletRequest) NanoWar • Brings DI and AOP to your web framework • WebWork 1&2 • Struts • NanoWeb (our own framework based on Groovy/Velocity)

  22. NanoWar configuration <web-app> <context-param> <param-name>nanocontainer.groovy</param-name> <param-value><![CDATA[ ... Your groovy assembly script here (!) ... ]]></param-value> </context-param> </web-app>

  23. Thank You! aslak@thoughtworks.comhttp://blogs.codehaus.org/people/~rinkrank/ http://www.picocontainer.org/ http://www.nanocontainer.org/

More Related