ByronMayne / AutoFactories

AutoFactories is a C# library used to generate factories for types to be used in dependency injection.
MIT License
1 stars 0 forks source link

Constructors with types that come from source generators fail to compile #19

Open ByronMayne opened 4 months ago

ByronMayne commented 4 months ago

Lets say you have a source generator that produces an interface like this:

namespace Foo
{
    public interface IBar
    {}
}

Then you add this to a class with the [GenerateFactory] attribute applied to it like:

using Ninject;
using Foo;

[GenerateFactory]
public class ExampleClass
{
    public ExampleClass(IBar bar)
    {}
}

The generated class will look like this:

using System;
using Ninject;
using Ninject.Syntax;
using Ninject.Parameters;

public class ExampleClassFactory
{
    public ExampleClass Create(IBar bar) // <--- Compiler error here because it's not fully qualified 
    {} // blahh
}

The reason this is the case is because Source Generators can not depend on each other. So when internally Auto Factories goes to resolve IBar it only knows it's type name as the ISymbol is null. We have two methods we could do to avoid this issue

  1. Force all generated types to use the fully qualified name in the source class. Then define an analyzer that forces it.
  2. Gather all the namespaces of classes that are being added to the factory and append them to the top. There is a chance of name space collisions however this does not require user interaction.