Skip to content

Instantly share code, notes, and snippets.

@mynkow
Created January 13, 2012 19:51
Show Gist options
  • Save mynkow/1608359 to your computer and use it in GitHub Desktop.
Save mynkow/1608359 to your computer and use it in GitHub Desktop.
Failed to resolve component with handler selector
using System;
using System.Collections.Generic;
using System.Linq;
using Castle.MicroKernel;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;
namespace CastleWindsor.Tests
{
public interface ITestComponent { }
public class FirstComponent : ITestComponent { }
public class FirstSelector : IHandlerSelector
{
public bool HasOpinionAbout(string key, Type service)
{
return service == typeof(FirstComponent);
}
public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
{
return handlers.Where(x => x.ComponentModel.Implementation == typeof(FirstComponent)).First();
}
}
public class SecondComponent : ITestComponent { }
public class SecondSelector : IHandlerSelector
{
public bool HasOpinionAbout(string key, Type service)
{
return service == typeof(SecondComponent);
}
public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
{
return handlers.Where(x => x.ComponentModel.Implementation == typeof(SecondComponent)).First();
}
}
public class TestFacility : AbstractFacility
{
protected override void Init()
{
Kernel.AddHandlerSelector(new FirstSelector());
Kernel.AddHandlerSelector(new SecondSelector());
Kernel.Register(
Component.For<ITestComponent>().ImplementedBy<FirstComponent>(),
Component.For<ITestComponent>().ImplementedBy<SecondComponent>());
}
}
public class TestContainer : WindsorContainer
{
public TestContainer()
{
AddFacility(new TestFacility());
}
}
[TestFixture]
public class TestHandlerSelector
{
[Test]
public void Test()
{
var container = new TestContainer();
var instance = container.Resolve<SecondComponent>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment