Wiki

Clone wiki

TestUnityIOC / Discussion of DI and UnityIOC

The code is split up into 3 files (rather messily). It's only meant to be illustrative :)

theclasses.cs is a dumping ground for the declarations of all the types used in making beverages, including liquids, powders, and the beverage maker itself. It also contains the types for logging, to spice things up a bit.

Form1.cs is (obviously) the main form of the application. It is meant to simulate what a user might see when interacting with a beverage maker. Specifically, it gives the user the option of choosing the liquid and powder, and the ability to "brew" the beverage.

Program.cs contains the main static method, and the plumbing for registering the various types with the IUnityContainer. It also creates the form and registers it as the only IOutput.

The combo boxes on the form get their list values from the container itself - they are meant to list the different ILiquid and IPowder types that have been registered, so that the user can choose among them at run time. I threw this into a dictionary just so I could iterate over them when populating the combo boxes.

I threw logging in because I wanted to experiment and also thought it would give me an opportunity to refactor later using AOP (once I figured that out ;) So, when the container creates any descendant of Loggable, the creation is logged.

All of that works as I expected. But what I originally wanted was to make Loggable have only a default ctor so that I would not have to provide ctors to it's descendants (Water, GroundCoffee etc). Alas, I couldn't make it work with just a setOutput method, so I added the constructors, and then everything worked.

I'm sure there's many other flaws in this design, and I'd welcome any advice about them.

-- Drew

Updated