IoC ToAbstractFactory "Duplicate type name" exception for same-name interfaces
See original GitHub issueI’m having some problems with the ToAbstractFactory()
extension method. In my particular scenario, the name of the interface that I’m trying to create an abstract factory for is “duplicated” but exists in two different namespaces. If I try to set up these bindings in the ConfigureIoC
method I get an ArgumentException with the message “Duplicate type name within an assembly”. This is from a reflection emit call which ultimately traces back to StyletIoC.Internal.Container.GetFactoryForType(Type serviceType)
.
In a nutshell, if I have, say, an IFactory
interface in two different namespaces (each with different methods), and I try to set up abstract factory bindings for them, then I get a runtime error. Is this a bug? I don’t know, but it seems like a scenario that should be supported as the two interfaces ultimately have different full names (being in different namespaces).
Here’s some test code to illustrate the issue:
using System;
using Stylet;
using StyletIoC;
namespace StyletTest.Foo
{
public sealed class FooViewModel { }
public interface IFactory
{
FooViewModel CreateFooViewModel();
}
public sealed class FactoryImpl : IFactory
{
public FooViewModel CreateFooViewModel() { throw new NotImplementedException(); }
}
public sealed class FooConductorViewModel
{
public FooConductorViewModel(IFactory factory) { }
}
}
namespace StyletTest.Bar
{
public sealed class BarViewModel { }
public interface IFactory
{
BarViewModel CreateBarViewModel();
}
public sealed class FactoryImpl : IFactory
{
public BarViewModel CreateBarViewModel() { throw new NotImplementedException(); }
}
public sealed class BarConductorViewModel
{
public BarConductorViewModel(IFactory factory) { }
}
}
namespace StyletTest
{
public sealed class RootViewModel
{
public RootViewModel(Foo.FooConductorViewModel fooConductor, Bar.BarConductorViewModel barConductor) { }
}
public sealed class Bootstrapper : Bootstrapper<RootViewModel>
{
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
base.ConfigureIoC(builder);
// The following two lines work (direct binding to implementations)
//builder.Bind<Foo.IFactory>().To<Foo.FactoryImpl>();
//builder.Bind<Bar.IFactory>().To<Bar.FactoryImpl>();
// The following two lines fail at runtime (abstract factory bindings)
builder.Bind<Foo.IFactory>().ToAbstractFactory();
builder.Bind<Bar.IFactory>().ToAbstractFactory();
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Thanks for looking at this so quickly!
Really liking the library BTW: makes tricky things easier but isn’t too prescriptive about it.
I think I tried something similar at one point and moved away from it: the reason being that it completely messes up the “Go To Type” functionality in Visual Studio. You can’t simply navigate to a type by typing its name. These days I like to have every type name by unique inside a project (and preferable the whole solution), to make navigation easier.