Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created October 12, 2010 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/622091 to your computer and use it in GitHub Desktop.
Save mikehadlow/622091 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
using Castle.Windsor;
namespace Mike.Spikes
{
public class WindsorCircularArrayRefs
{
public void CreateAndUseCircularArrayRef()
{
var container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, allowEmptyCollections: true));
container.Register(
Component.For<IService>().ImplementedBy<MyCompositeService>().Named("composite"),
Component.For<IService>().ImplementedBy<MyService>(),
Component.For<IService>().ImplementedBy<MyOtherService>()
);
var service = container.Resolve<IService>("composite");
service.SayHello();
// outputs:
// Hello from MyService
// Hello from MyOtherService
}
}
public interface IService
{
void SayHello();
}
public class MyService : IService
{
public void SayHello()
{
Console.WriteLine("Hello from MyService");
}
}
public class MyOtherService : IService
{
public void SayHello()
{
Console.WriteLine("Hello from MyOtherService");
}
}
public class MyCompositeService : IService
{
readonly IEnumerable<IService> services;
public MyCompositeService(IEnumerable<IService> services)
{
this.services = services;
}
public void SayHello()
{
foreach (var service in services)
{
service.SayHello();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment