Interface <T> binding at Windsor Castle

I have a generic relay interface IDataContainer

I use it for different types of T IPerson, ISuperMan, etc.

In the castle I register it

container.AddComponentWithLifestyle<IDataContainer<IPerson>, DataContainer<Person>>(LifestyleType.Transient);
container.AddComponentWithLifestyle<IDataContainer<ISuperMan>, DataContainer<SuperMan>>(LifestyleType.Transient);

      

at runtime the lock creates a dependency, for example

IDataContainer<IPerson> test = container.GetService<IDataContainer<IPerson>>();

      

but it fails with inability to execute ... the classes implement the interface and the namespaces are correct, etc.

Call

IPerson test = container.GetService<IPerson>();

      

Works (with registration <IPerson,Person>

)

Does the stone castle permit interface<T>

or?

+1


a source to share


2 answers


So it's getting late, but I think I know what you are doing here. I can pass this:

IDataContainer<IPerson> test = container.GetService<IDataContainer<IPerson>>();

      

By registering components as follows:



public class IoC
{
    public static void SetUp()
    {
        container = new WindsorContainer();
        container.AddComponent<IPerson, Person>();
        //container.AddComponentWithLifestyle<IDataContainer<IPerson>, DataContainer<Person>>(LifestyleType.Transient);
        //container.AddComponentWithLifestyle<IDataContainer<ISuperMan>, DataContainer<SuperMan>>(LifestyleType.Transient);
        container.AddComponentWithLifestyle("DataContainers", typeof(IDataContainer<>), typeof(DataContainer<>), LifestyleType.Transient);
    }

    public void TestOne()
    {
        SetUp();
        var test = container.GetService<IDataContainer<IPerson>>();
        Assert.That(test, Is.Not.Null);
    }

    public void TestTwo()
    {
        SetUp();
        var test = container.GetService<IPerson>();
        Assert.That(test, Is.Not.Null);
    }
}

internal interface IDataContainer<T> { }
internal class DataContainer<T> : IDataContainer<T> { }

internal interface IPerson { }
class Person : IPerson { }

internal interface ISuperMan { }
class SuperMan : ISuperMan { }

      

The two lines that are commented out are the two lines that exist in the question.

+2


a source


This has nothing to do with Windsor. You are getting a casting error because C # 2.0 and 3.0 do not support generic covariance. You are probably creating DataContainer<T>

implement IDataContainer<T>

, which means it DataContainer<Person>

implements IDataContainer<Person>

, not IDataContainer<IPerson>

which you are requesting from the container.



0


a source







All Articles