0
votes

On the IOC Castle Windsor wiki [Editor's Note: After this question was written, the wiki has been updated with more example code], there is the following (incomplete) demo code.

How do I complete this code so it runs properly in a C# console app?

//application starts...
var container = new WindsorContainer();

// adds and configures all components using WindsorInstallers from executing assembly
container.Install(FromAssembly.This());

// instantiate and configure root component and all its dependencies and their dependencies and...
var king = container.Resolve<IKing>();
king.RuleTheCastle();

// clean up, application exits
container.Dispose();

Update:

Discovered tutorial videos on IOC in general.

Got as far as adding an interface and a concrete implementation of the interface:

interface IKing
{
    void RuleTheCastle();   
}

public class King
{
    void RuleTheCastle()
    {
        Consolel.Write("Rule the castle.");
    }
}

However, it threw a runtime error when I ran it.

Update:

I ended up using Ninject, because:

  1. The documentation is far superior to Castle Windsor (and Spring.NET for that matter).
  2. It uses expression compilation/LCG, so its faster (8x to 50x) compared to Castle Windsor, which uses Reflection.

To get started, click on "Visit the Dojo", then follow through the series of NInject tutorials on GitHub.

After you've done this, you can view a tutorial video on TekPub. The video does go through the concepts quite fast, so it might be good to complete the series of NInject tutorials on GitHub first.

1
Er, why the vote down? If Castle Windsor is as simple as it seems to think it is, shouldn't it be simple to complete the sample demo code on the Windsor wiki so it works?Contango
Have you written some code in addition to the example? If not, you need to create the IKing interface, create a concrete class which implements the IKing interface, and create a configuration to tell Castle.Windsor to resolve IKing as your concrete class.Espen Burud
I got as far as creating the IKing interface, and creating a concrete class which implements the IKing interface. I didn't get as far as creating a configuration to tell Castle.Windsor where to resolve IKing as the concrete class - I assumed that this would happen automatically.Contango
you need to add a Windsor Installer in the assembly to let Windsor know that the IKing interface will be resolved at runtime by the King concrete class.Cristian Lupascu

1 Answers

2
votes

Typically, in order to apply IoC in a C# console application you would use the entry point (e.g. the Main method) to:

  • instantiate and configure an IoC container - the first two lines in the sample code
  • use the container to build an object graph (an object and its dependencies) - 3rd line of code
  • pass control to the resolved object, usually by calling a method on it - 4th line of code
  • after the application is done, tell the container perform any clean up it needs to perform - 5th line of code

This what that code demo presents, and from this point of view it's complete.

However, in order for this application to work there are two other very important prerequisites:

  • the application has to be designed with the Inversion-of-Control principle in mind (this is actually the hard part)
  • the entry-point assembly of the console app has to contain Windsor Installers that are used to configure the container and that basically define what concrete types will be used whenever an interface is needed.

The code for the Installer class in this example would look like this:

public class KingApplicationInstaller : IWindsorInstaller
{
   public void Install(IWindsorContainer container, IConfigurationStore store)
   {
        container.Register(
            Component.For<IKing>().ImplementedBy<King>());
   }
}