Configuration of general workflow with generic interfaces

Viewed 164

We recently stumbled across a problem in our data collection program. We use generic interfaces to have a general workflow that's easily customizable for different data inputs via concrete implementations in a config. A recent fix for a missing data bug is looking in general like the following:

[TestFixture]
internal class GenericsTest
{
    [Test]
    public void Config()
    {
        new Collector(new Search());
    }
    internal class Collector
    {
        public Collector(Clues<Where, What> foo)
        {

        }
    }
    internal interface Clues<T, P>
    {

    }
    internal class Search : Clues<Where, Item>
    {

    }
    internal class Where
    {

    }
    internal class Item : What
    {

    }

    internal interface What
    {

    }

}

The Config is initializing a specific Collector with a specific Search. The Collector itself is only expecting a generic interface with a concrete class Where and interface What as parameters. The Search class should satisfy these expectations by implementing the generic Clues interface with the Where class and the Item class which itself implements the What interface. Instead the initialization of Search fails because it can't make the connection between the specific implementations and the general expectation.

Error   CS1503  Argument 1: cannot convert from 'UnitTest.GenericsTest.GenericsTest.Search' to 'UnitTest.GenericsTest.GenericsTest.Clues<UnitTest.GenericsTest.GenericsTest.Where, UnitTest.GenericsTest.GenericsTest.What>'

We tried casting to the required types, but it only shifted the error to runtime.

The general concept is, that the workflow stays as generic as possible and the config takes care of all specific requirements. The concrete methods needed are supposed to be introduced in the item class and hidden behind the interface to avoid unnecessary cluttering.

Is there a specific way to write such an implementation in C#?

1 Answers
Related