castle-windsor Hello World - Castle Windsor


Example

class Program
{
    static void Main(string[] args)
    {
        //Initialize a new container
        WindsorContainer container = new WindsorContainer();

        //Register IService with a specific implementation and supply its dependencies
        container.Register(Component.For<IService>()
                                    .ImplementedBy<SomeService>()
                                    .DependsOn(Dependency.OnValue("dependency", "I am Castle Windsor")));

        //Request the IService from the container
        var service = container.Resolve<IService>();
        
        //Will print to console: "Hello World! I am Castle Windsor
        service.Foo();
}

Services:

public interface IService
{
    void Foo();
}

public class SomeService : IService
{
    public SomeService(string dependency)
    {
        _dependency = dependency;
    }

    public void Foo()
    {
        Console.WriteLine($"Hello World! {_dependency}");
    }

    private string _dependency;
}