Created
March 30, 2011 10:31
-
-
Save corruptmem/894181 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; | |
using System.Reflection; | |
using Castle.Core; | |
using Castle.Core.Internal; | |
using Castle.Facilities.TypedFactory; | |
using Castle.MicroKernel; | |
using Castle.MicroKernel.ComponentActivator; | |
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 interface IHumanFactory | |
{ | |
IPerson GetById(string id); | |
} | |
public class CustomTypedFactoryComponentResolver : TypedFactoryComponentResolver | |
{ | |
public CustomTypedFactoryComponentResolver(string componentName, Type componentType, IDictionary additionalArguments) | |
: base(componentName, componentType, additionalArguments) | |
{ | |
} | |
public override object Resolve(Castle.MicroKernel.IKernelInternal kernel, Castle.MicroKernel.IReleasePolicy scope) | |
{ | |
if (ComponentName != null) | |
{ | |
if (kernel.LoadHandlerByKey(ComponentName, ComponentType, AdditionalArguments) == null) | |
{ | |
throw new ComponentNotFoundException(ComponentName); | |
} | |
return kernel.Resolve(ComponentName, ComponentType, AdditionalArguments, scope); | |
} | |
return kernel.Resolve(ComponentType, AdditionalArguments, scope); | |
} | |
} | |
public class CustomTypedFactoryComponentSelector : DefaultTypedFactoryComponentSelector | |
{ | |
protected override ITypedFactoryComponentResolver BuildFactoryComponent(MethodInfo method, string componentName, Type componentType, System.Collections.IDictionary additionalArguments) | |
{ | |
var itemType = componentType.GetCompatibleArrayItemType(); | |
if (itemType == null) | |
{ | |
return new CustomTypedFactoryComponentResolver(componentName, componentType, additionalArguments); | |
} | |
return new TypedFactoryCollectionResolver(itemType, additionalArguments); | |
} | |
protected override string GetComponentName(MethodInfo method, object[] arguments) | |
{ | |
if (method.Name == "GetById" && arguments.Length == 1 && arguments[0] is string) | |
{ | |
Console.WriteLine("Returning {0}", arguments[0]); | |
return (string)arguments[0]; | |
} | |
return base.GetComponentName(method, arguments); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var ct = new WindsorContainer(); | |
ct.AddFacility<TypedFactoryFacility>(); | |
ct.Register(Component.For<CustomTypedFactoryComponentSelector>()); | |
ct.Register(AllTypes.FromThisAssembly().BasedOn<IPerson>().WithService.FirstInterface().Configure(x => x.Named(x.Implementation.Name))); | |
ct.Register(Component.For<IHumanFactory>().AsFactory(c => c.SelectedWith<CustomTypedFactoryComponentSelector>())); | |
var factory = ct.Resolve<IHumanFactory>(); | |
Console.WriteLine(factory.GetById("Bob").Name); // outputs Bob | |
Console.WriteLine(factory.GetById("Jack").Name); // outputs Jack | |
Console.WriteLine(factory.GetById("Frank").Name); // throws exception | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment