In my project I have the following three interfaces, which are implemented by classes that manage merging of a variety of business objects that have different structures.
public interface IMerger<TSource, TDestination>
{
TDestination Merge(TSource source, TDestination destination);
}
public interface ITwoWayMerger<TSource1, TSource2, TDestination>
{
TDestination Merge(TSource1 source1, TSource2 source2, TDestination destination);
}
public interface IThreeWayMerger<TSource1, TSource2, TSource3, TDestination>
{
TDestination Merge(TSource1 source1, TSource2 source2, TSource3 source3, TDestination destination);
}
This works well, but I would rather have one IMerger interface which specifies a variable number of TSource parameters, something like this (example below uses params; I know this is not valid C#):
public interface IMerger<params TSources, TDestination>
{
TDestination Merge(params TSource sources, TDestination destination);
}
It there any way to achieve this, or something functionally equivalent?
You can’t. That is a key part of the API. You could, however, do something around the side, such as accepting a
Type[]argument. You might also think up some exotic “fluent API / extension method” way of doing it, but to be honest it probably won’t be worth it; but something like:or with generic type inference:
Each merge step would simple store away the work to do, only accessed by
Execute.