Search code examples
.netcastle-windsor

How to get started with Castle-Windsor?


I am completely new with IoC/windsor. I started with Google to learn it, but unfortunately, I haven't got proper documentation which could be easier for me to understand. so I came here with such this title/questions.

Every document/pages(web), Starting something similar like this

"We should start from registering the class/interface then resolve it ... "

but none of the page shows complete documentation on how to achieve that, I tried to make a simple project too, but I failed to run it. I don't know how to resolve container , where/how to call for install(), I am totally messed up.

Could anyone help me with a sample project which includes a complete demonstration of registration/installation?

Thanks in advance :)


Solution

  • Also Mark Seemann's Dependency Injection in .NET book is a good place to start. Well written and has a chapter on Castle Windsor specifically.

    They also have some good tutorials on code project, I used before:

    UPDATE

    Well, the most simplistic tutorial would be as follows:

    1) In VS2010 create new console application

    2) Right click on "References", select "Manage NuGet Packages", install Castle.Windsor

    3) Use code below for Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    
    namespace ExploringCastleWindsor
    {
        internal class Program
        {
            interface ILogger
            {
                void Log(string message);
            }
    
            class Logger : ILogger
            {
                public void Log(string message)
                {
                    Console.WriteLine(message);
                }
            }
    
            static void Main(string[] args)
            {
                // Registering
                var container = new WindsorContainer();
                container.Register(Component.For<ILogger>().ImplementedBy<Logger>());
                // Resolving
                var logger = container.Resolve<ILogger>();
                logger.Log("Hello World!");
            }
        }
    }