IoC and Interface Recommendations

I am experimenting with IoC on my way to TDD by playing with an existing project. In a nutshell, my question is: What are the best practices in IoC when public and non-public methods are interesting?

There are two classes:

public abstract class ThisThingBase
{
    public virtual void Method1() {}
    public virtual void Method2() {}

    public ThatThing GetThat()
    {
        return new ThatThing(this);
    }
    internal virtual void Method3() {}
    internal virtual void Method4() {}
}

public class Thathing
{
    public ThatThing(ThisThingBase thing)
    {
        m_thing = thing;
    }
    ...
}

      

ThatThing does some things by using the ThisThingBase reference to invoke methods that are often overloaded by children of ThisThingBase.

Method1 and Method2 are publicly available. Method 3 and Method 4 are internal and only used for this.

I would like to test ThatThing without ThisThing and vice versa.

While learning about IoC, my first thought was that I should define the IThing interface, implement it with ThisThingBase, and pass it to the ThatThing constructor. IThing will be a public interface client that can invoke a call, but it doesn't include Method3 or Method4, which also need ToThing.

Should I define a second interface - perhaps IThingInternal - for these two methods and pass the BOTH interfaces to ThatThing?

0


source to share


1 answer


The problem with IoC containers is that they cannot manage the lifecycle of objects. Why factory method on ThisThingBase? If it were possible to create an IoC Thatthing container, it would increase testability.

It's hard to tell with an example, but perhaps you have an unnecessary relationship between Thatthing and ThisThingBase.



Interfaces can be nice, but sometimes it's enough with virtual methods in a class where you can enable testing.

0


source







All Articles