![Page 2: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/2.jpg)
What am I going to talk about?
• The problem: Application Architecture.• What is Inversion of Control?• Inversion of Control Containers.• A Real Application: Suteki Shop.
![Page 3: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/3.jpg)
Comonent size vs. complexity is not linear
Complexity
Features
![Page 4: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/4.jpg)
Break large pieces into smaller pieces
![Page 5: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/5.jpg)
The goal of good architecture
• Don’t repeat yourself (DRY)• Decreased coupling• Separation of concerns• Reduced cost of change• Greater flexibility• Do more for less ££££
![Page 6: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/6.jpg)
What is a component
Wikipedia says:• Multiple use• Non context specific• Composable with other components• Encapsulated• A unit of independent deployment and
versioning
![Page 7: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/7.jpg)
What is inversion of control?
• A way of designing reusable components• A way of decoupling services• A design pattern not a framework• ‘Inject’ dependencies through constructor or
parameters (Dependency Injection)• It’s easy!
![Page 8: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/8.jpg)
Show me the code
<Demo>
![Page 9: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/9.jpg)
Problems with typical coding techniques
• Difficult or impossible to test• Tightly coupled• Violates the single responsibility principle• Violates the open closed principle
![Page 10: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/10.jpg)
Vocabulary
Service = Interface (IEmailSender)Component = Class (EmailSender)
BUTNot all classes are components
![Page 11: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/11.jpg)
IoC design style
• Systems are composed of small specialised services– Represented by interfaces
• Components declare– The services they provide (by implementation)– The services they require (by DI)
• Components do not dictate their own lifestyle– Do not implement singleton yourself
• Let TDD drive your design
![Page 12: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/12.jpg)
What is an IoC container?
• A (very) smart factory• Automatically resolves dependencies• Automatically injects concrete instances• All services are registered in the container• Single point of access for services• Transparent• Various implementations for .NET
![Page 13: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/13.jpg)
Castle MicroKernel / Windsor
• Part of the Castle Project castleproject.org• Started by Hamilton Verissimo• Also includes:– MonoRail MVC framework for ASP.NET– ActiveRecord based on NHibernate
![Page 14: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/14.jpg)
IoC container example
<demo>
![Page 15: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/15.jpg)
How dependencies are resolved
IReportBuilder
IReportSender
Reporter
![Page 16: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/16.jpg)
Flexibility
<demo>
![Page 17: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/17.jpg)
Lifestyles
• Singleton (the default)• Transient• Per Thread• Pooled• Per Web Request• Custom
![Page 18: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/18.jpg)
A Real Application
http://code.google.com/p/sutekishop/http://sutekishop.co.uk/
<suteki shop demo>
![Page 19: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/19.jpg)
Pros?
• Simpler component architecture• Reduced cost of change• Easy to unit test• Easily move between application
configurations• Ready made configuration (IoC containers)
![Page 20: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/20.jpg)
Cons?
• Not another thing to learn?• Higher level of abstraction• Performance?• Lifestyle mistakes can be hard to diagnose• Can’t do obfuscation and configuration• Maybe it’s time we looked at dynamically
typed languages?
![Page 21: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/21.jpg)
Should I use it?
• Already familiar with OO principles and patterns?
• Already writing unit tests?• Using a statically typed language?
If not, learn how to do these firstDon’t impose an IoC container on a team which
can’t see its benefit
![Page 22: Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow@yahoo.com mikehadlow.blogspot.com](https://reader036.vdocuments.site/reader036/viewer/2022062422/56649f2f5503460f94c49f2f/html5/thumbnails/22.jpg)
Resources
• Castle Project castleproject.org• Oren Eini ayende.com/blog• Alex Henderson bittercoder.com• ALT NET
http://tech.groups.yahoo.com/group/altdotnet/