Is there a way in c# to expand a call to multiple implementations of an interface implicitly?

Viewed 40

Let's say I have two classes that both implement interface Test

public interface Test 
{
    void Write(string value);
}

public class Data1 : Test
{
    public void Write(string value) { Console.WriteLine(value); }
}
public class Data2 : Test
{
    public void Write(string value) { Console.WriteLine(value); }
}

Is there any way to use the interface to call multiple implementations of it?

For example, with the above code I want to do the following:

public class Wrapper
{
    private Data1 data1;
    private Data2 data2;

    // i'm aware this isn't code but this is the best way I had of describing it
    // basically my goal is to call both implementations with a single call
    public CallMethod(Test.Method, Parameters[]) 
    {
        data1.Method(Parameters); 
        data2.Method(Parameters);
    }
}

Its possible I'm going about this the wrong way but the whole goal of this is that I am in the process of migrating to a new database. I have two classes that both implement the same interface that write their data to their own database. I want to call both of those at the same time without having to explicitly write a wrapper method for each method. Eventually I will rip the old data class out and want to have as little code churn as possible.

1 Answers

There is no built-in way how to do that. You have to have custom wrapper class/struct.

Related