question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IoC ToAbstractFactory "Duplicate type name" exception for same-name interfaces

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TheMightyBorkoncommented, Apr 23, 2020

Thanks for looking at this so quickly!

Really liking the library BTW: makes tricky things easier but isn’t too prescriptive about it.

0reactions
canton7commented, Apr 24, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate type name within an assembly in .Net Core 1.1
ArgumentException : Duplicate type name within an assembly. ... name, TypeAttributes attr, Type parent, Type[] interfaces) at Microsoft.
Read more >
Software design pattern
In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software...
Read more >
Extending Data Lineage Analysis Platform with Support for ...
The plugin has been successfully tested on a small but realistic software system that can read data from a file, transform them, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found