Hi
It seems that there is a bug when resolving typed factories with more
than 2 levels of inheritance.
We have the following sample:
----------
using System;
using Castle.Windsor;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
namespace TypedFactoryTest
{
class Program
{
static void Main(string[] args)
{
IWindsorContainer container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<IFooFactory3>()
.AsFactory()
);
container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Transient);
container.Register(Component.For<IFoo2>().ImplementedBy<Foo2>().LifeStyle.Transient);
var factory = container.Resolve<IFooFactory3>();
var foo = factory.CreateFoo();
var foo2 = factory.CreateFoo2();
factory.ReleaseBar(foo2);
factory.ReleaseBar(foo);
container.Dispose();
}
}
public interface IBar : IDisposable { }
public interface IFoo : IBar { }
internal class Foo : IFoo
{
public void Dispose()
{
}
}
public interface IFoo2 : IBar { }
internal class Foo2 : IFoo2
{
public void Dispose()
{
}
}
public interface IFooFactory
{
void ReleaseBar(IBar bar);
}
public interface IFooFactory2 : IFooFactory
{
IFoo CreateFoo();
}
public interface IFooFactory3 : IFooFactory2
{
IFoo2 CreateFoo2();
}
}
---------------
when trying to resolve a typed factory that inherits from a base
interface( which itself inherits from a base interface and have a void
method), there is an exception thrown ("An item with the same key has
already been added.")
The problem comes from the following method (in
Castle.Facilities.TypedFactory.TypedFactoryInterceptor):
-----------
protected virtual void BuildHandlersMap(Type service)
{
if (service.Equals(typeof(IDisposable)))
{
MethodInfo method = service.GetMethods().Single<MethodInfo>();
this.methods.Add(method, new Dispose(new
Action(this.Dispose)));
}
else
{
foreach (MethodInfo method in service.GetMethods())
{
if (this.IsReleaseMethod(method))
{
this.methods.Add(method, new Release(this.kernel));
}
else
{
this.methods.Add(method, new Resolve(this.kernel,
this.ComponentSelector, new
Action<object>(this.trackedComponents.Add)));
}
}
foreach (Type @interface in service.GetInterfaces())
{
this.BuildHandlersMap(@interface);
}
}
}
---------
Since the method is recursive, the method from the base interface will
be added twice in our case (one time when calling GetInterfaces() for
the most specialized class and another time when calling
GetInterfaces() for the intermediate class). The problem will always
happens when there is more than 2 level of inheritance.
Thanks!
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.