Castle : Inversion of control

The Castle Project is a collection of useful  C# tools

This is a sample application of the Castle using an App.config:

It demonstrates the use of Castle.Winsor to allow an app to specify behaviour.
This  could easily be used to switch logging handlers.

Here is the main source of the application:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace CastleConsole
{
    using Castle.Windsor;                            // Castle.Winsor
    using Castle.Windsor.Configuration.Interpreters; //Castle.MicroKernel

    public interface ILogger
    {
        void Log(string msg );
    }

    public class LogToConsole : ILogger
    {
        public void Log(string msg)
        {
            System.Console.WriteLine(“The message was {0}”, msg);
        }
    }

    public class SecondLogger : ILogger
    {
        public void Log(string msg)
        {
            System.Console.WriteLine(“Second logger was {0}”, msg);
        }
    }

    public interface IExecute
    {
        void Execute();
    }

    /// <summary>
    /// This is a class that uses the logger
    /// </summary>
    public class ThreeExecutions : IExecute
    {
        /// <summary>
        /// This is used to store the logger interface
        /// </summary>
       
        ILogger _logger;

        public ThreeExecutions(ILogger logger)
        {
            _logger = logger;
        }

        public void Execute()
        {
            _logger.Log(“One”);
            _logger.Log(“Two”);
            _logger.Log(“Three”);
        }
    }

    /// <summary>
    /// Summary description for CastleConsoleTestClass
    /// </summary>
    public class CastleConsoleTestClass
    {
        static void Main(string[] args)
        {
            try
            {
                // This is the initial version
                //IWindsorContainer container = new WindsorContainer();
                //container.AddComponent(“logger 2”, typeof(ILogger), typeof(SecondLogger));
                //container.AddComponent(“logger”, typeof(ILogger), typeof(LogToConsole));           
                //container.AddComponent(“execute”, typeof(IExecute), typeof(ThreeExecutions));
               
                // This one uses app.config
                IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
                IExecute service = (IExecute)container[“execute”];
                service.Execute();

            }
            catch(Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
           

            System.Console.ReadLine();
        }
    }
}

 

Here is the app.config file:

 

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
  <configSections>
    <section name=”castle”
         type=”Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
                   Castle.Windsor” />
  </configSections>

  <castle>
    <components>
      <component id=”logger” service=”CastleConsole.ILogger, CastleConsole” type=”CastleConsole.LogToConsole, CastleConsole”  />
      <component id=”execute” service=”CastleConsole.IExecute, CastleConsole” type=”CastleConsole.ThreeExecutions, CastleConsole” />
    </components>
  </castle>

</configuration>

 

If you replace the first line of the components section with:

  <component id=”logger” service=”CastleConsole.ILogger, CastleConsole” type=”CastleConsole.SecondLogger, CastleConsole”  />

 

 

 

2 thoughts on “Castle : Inversion of control

  1. Thanks for the feedback. I was just trying to get a simple example of using Castle.Winsor using a config file. The castle documentation is a little unclear on the usage of the type parameter in this case (the type, assembly syntax threw me for a while).

    This style of programming seems to be exactly what is required to keep the unit tests and build times fast.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s