0

I have a problem with writing valid configuration for my classes. Suppose that I Have:

interface IA{}

class abstract A : IA {}

class A1 : A, IA
{
    private readonly IB _ib;

    public A1(IB ib)
    {
        _ib = ib;
    }
}

class A2 : A, IA
{
    private readonly IB _ib;

    public A1(IB ib)
    {
        _ib = ib;
    }
}

And I want to inject this classes to constructor of others, and I think there I have no problems with that, doing this by something like this:

For<IEnumerable<IA>>.Use(x => x.AllInstances<A>());

but I don't know to resolve problem with property ib, if I want to use different _ib for class A1 and for class A2.

interface B{}

class B1 : B {}

class B2 : B {}

I know that if I have only one class like B and class like A I can simply do something like this:

For<IA>().Use<ASample>().CTor<IB>().Is<BSample>();

But with my problem I try to use something like this:

For<IA>().ConditionallyUse(c => { c.If(t => t.ConcreteType == typeof(A1))
.ThenIt
.Is.ConstructedBy(by => new A1(by.InstanceOf<B1>()));    
c.If(t => t.ConcreteType == typeof(A1))
.ThenIt
.Is.ConstructedBy(by => new A2(by.InstanceOf<B2>()));
});

But it gives me an error of bidirectional registration..

Or maybe I should think about something like this:

For<IEnumerable<IA>>.Use(x => x.AllInstances<A>());
For<IEnumerable<IB>>.Use(x => x.AllInstances<B>());

but with this variant I should create an abstract class, and an additional method to use only one injected property from the list (for classes which inherit from interface B).

If anyone could help me I will be very appreciate!

MNie
  • 1,347
  • 1
  • 15
  • 35

1 Answers1

0

Resolution to my problem was to write something like that:

For<IA>().Add<A1>().CTor<IB>().Is<B1>();
For<IA>().Add<A2>().CTor<IB>().Is<B2>();

//and then
For<IEnumerable<IA>>.Use(x => x.GetAllInstances<IA>());
MNie
  • 1,347
  • 1
  • 15
  • 35