Skip to content

Instantly share code, notes, and snippets.

@corruptmem
Created March 30, 2011 10:42
Show Gist options
  • Save corruptmem/894190 to your computer and use it in GitHub Desktop.
Save corruptmem/894190 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
using Castle.Windsor;
namespace castlefacttest
{
public interface IPerson
{
string Name { get; }
}
public class Jack : IPerson
{
public string Name { get { return "Jack"; } }
}
public class Bob : IPerson
{
public string Name { get { return "Bob"; } }
}
public class Test
{
public Test(IEnumerable<IPerson> people)
{
Console.WriteLine("Test {0}", string.Join(", ", people.Select(x => x.Name)));
}
}
public class Program
{
public static void Main(string[] args)
{
var ct = new WindsorContainer();
ct.Kernel.Resolver.AddSubResolver(new CollectionResolver(ct.Kernel));
ct.Register(AllTypes.FromThisAssembly().BasedOn<IPerson>().WithService.FirstInterface().Configure(x => x.Named(x.Implementation.Name)));
ct.Register(Component.For<Test>());
ct.Resolve<Test>(); // Outputs: Test Jack, Bob
ct.Resolve<IEnumerable<IPerson>>(); // ComponentNotFoundException: No component for supporting the service castlefacttest.IPerson[] was found
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment