Learning StructureMap Through Tiny Goals


While I’ve been using Castle’s Windsor for a bit now, but wanted to get some StructureMap code running just to get some exposure to it; I’ve heard good things. I decided to set a simple goal and try to achieve it using StructureMap. Here’s my goal:

    1. Request a concrete type of an interface and run a method.
    2. Change the implementation of the interface (during runtime) and run the same method to compare against the original.
    3. Toggle back to the originally configured class.
   
I assumed that building a small “one-off” project that could do this would give me a good start to understanding the StructureMap syntax. The idea stemmed from thoughts of logging certain data using an administrative login. By swapping out the runtime type without rebuilding the application, you can apply different logic (via a Decorator Patttern) to track timings and any other data available to you.

My example is quite boring and unfortunately not useful or real-world, but it gets the job done to give me a base to get something like this set-up. I have my Interface and Classes code as the following:

You’ll see the only difference is the output of the Print() function. Now swapping between the two, I’d expect different results, all during runtime, during my main method, as follows:

The Aggressive Class vs The Laid Back Class