Created
October 12, 2010 12:20
-
-
Save mikehadlow/622091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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