Dependency Injection
Dependency injection, From Wikipedia
http://en.wikipedia.org/wiki/Dependency_injection
Dependency Injection - Windsor
Windsor - source code
http://gojko.net/resources/windsor-facilities-20081023.zip
Castle project main web site
http://www.castleproject.org/
Automocking container
http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx
Method validator facility
http://using.castleproject.org/display/Contrib/Castle.Facilities.MethodValidator
Inversion of Control and Dependency Injection: Working with Windsor Container
http://msdn.microsoft.com/en-us/library/aa973811.aspx
Inversion of Control and Dependency Injection with Castle Windsor Container - Part I
http://dotnetslackers.com/Part1.aspx
// Tightly coupled. The CarService() is locked into a // specific engine. ICar car = new CarService(); car.setPedalPressure(12); float speed = car.getSpeed(); MessageBox.Show("Speed is " + speed.ToString(), "**NOTE**", MessageBoxButton.OK);
The user of DefaultCarService can choose the engine implementation.
Notice the yellow highlighted text - the DefaultCarService takes two types of engines
DefaultEngine - one of the two engine types that can be injected
BigV8Engine - one of the two engine types that can be injected
// Main code decides on the engine implementation. We chose Default but we // cloud choose BigV8EngineService()
ICar car = new DefaultCarService(new DefaultEngineService()); car.setPedalPressure(12); float speed = car.getSpeed(); MessageBox.Show(Speed is + speed.ToString(), **NOTE**, MessageBoxButton.OK);
// Easily swap in a completely different and highly decoupled Engine Service
car = new DefaultCarService(new BigV8EngineService()); car.setPedalPressure(12); speed = car.getSpeed(); MessageBox.Show(Speed is + speed.ToString(), **NOTE**, MessageBoxButton.OK);
class DefaultCarService : ICar { IEngine _engine = null; public DefaultCarService(IEngine engineImplementation) { _engine = engineImplementation; } public float getSpeed() { return _engine.getEngineRotation(); } public void setPedalPressure(float pedal_pressure) { if (pedal_pressure > 4.0 && pedal_pressure <= 6) _engine.setFuelConsumptionRate(23.3F); else if (pedal_pressure > 6.0) _engine.setFuelConsumptionRate(33.3F); else { _engine.setFuelConsumptionRate(13.3F); } } }