1

In my application I handle lots of ViewModels that get registered inside a LightInject Container via several Interfaces. Some of those Interfaces are derived from other for UnitTesting purposes.

When resolving multiple ViewModels with the same Interface that is an interface further above I get more ViewModelinstances than expected.

I made a simplified example for this behavior. Is it possible to prevent this behavior somehow?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
public interface IBar
{
}

public interface IFoo : IBar
{
}

public class Cat : IFoo
{
}

class Program
{
    static void Main(string[] args)
    {
        var container = new LightInject.ServiceContainer();

        container.Register<IBar, Cat>();
        container.Register<IFoo, Cat>();

        var m = container.GetAllInstances(typeof(IBar));

        // m will contain 2 Instances of Cat. Is it possible it will resolve only 1 Instance?
    }
}

}

Mohnkuchenzentrale
  • 5,745
  • 4
  • 30
  • 41

1 Answers1

1

Try this var container = new ServiceContainer(new ContainerOptions() {EnableVariance = false});

seesharper
  • 3,249
  • 1
  • 19
  • 23