Created
March 30, 2011 10:42
-
-
Save corruptmem/894190 to your computer and use it in GitHub Desktop.
This file contains 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 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