0

I'm using .NET Core 3.0 and have the following classes

public class DataProviderA: IDataProvider { }
public class DataProviderB: IDataProvider { }

public class DataProviderCombined: IDataProvider { 
      public DataProviderCombined(IDataProvider providerA, IDataProvider providerB) { ... }
}

In my Startup.cs I have the services registered as the following:

services.AddTransient<IDataProvider, DataProviderCombined>();

services.ForConcreteType<DataProviderCombined>().Configure.Scoped()
        .Ctor<IDataProvider>("providerA").Is<DataProviderA>().Transient()
        .Ctor<IDataProvider>("providerb").Is<DataProviderB>().Transient();

This doesn't seem to resolve properly as I'm getting the following errors:

Lamar.IoC.LamarException: Cannot build registered instance dataProvider of 'IDataProvider': Bi-directional dependencies detected:

Is there a registration piece that I am missing in Lamar to be able to do this in .NET Core 3?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
SamIAm
  • 2,241
  • 6
  • 32
  • 51

1 Answers1

0

I misunderstood the services.ForConcreteType<> and how to use it and was not using it properly.

So with the above, DataProviderCombined would have been used in a controller, but with through constructor injection using IDataProvider. It would not have ever worked because the registration was done through an implementation and not the interface.

In the end, the following was done:

services.For<IDataProvider>().Add<DataProviderA>().Named("DA");
services.For<IDataProvider>().Add<DataProviderB>().Named("DB");

services.For<IDataProvider>().Add<CombinedDataProvider>()
            .Ctor<IDataProvider>("daProvider").Is<DataProviderA>()
            .Ctor<IDataProvider>("dbProvider").Is<DataProviderB>()
            .Named("Combined");

services.ForConcreteType<DataController>().Configure
            .Scoped()
            .Ctor<IDataProvider>("combinedDataProvider")
            .IsNamedInstance("Combined");

This allowed me to use the Combined Data Provider in the constructor using the following:

public class DataController {
   public DataController(IDataProvider combinedDataProvider) { ... }
}
SamIAm
  • 2,241
  • 6
  • 32
  • 51