public interface ISomeInterface
{
IOut SomeMethod(IIn aIn)
}
public class MyOut : IOut
{
public string AnExtraProp {get; set;}
}
public class MyIn : IIn
{
public string AnotherExtraProp {get; set;} }
}
public class MyConcreteOfSomeInterface : ISomeInterface
{
public MyOut SomeMethod(MyIn aIn)
{
}
}
Is it possible to have many classes (eg. MyConcreteOfSomeInterface, MyConcrete2OfSomeInterface, ....) implement an interface (eg. ISomeInterface) but yet have parameters of a concrete type (eg. MyIn, MyOut etc.).
I realise I could declare:
public interface ISomeInterface<TIn, TOut>
{
TOut SomeMethod(TIn aIn)
}
but as ISomeInterface will have many methods this will not be practical. So say I need to add additional methods SomeMethod2 and SomeMethod3 then I would end up with:
public interface ISomeInterface<TIn, TOut, TIn2, TOut2, TIn3, TOut3>
{
TOut SomeMethod(TIn aIn)
TOut2 SomeMethod(TIn2 aIn)
TOut3 SomeMethod(TIn3 aIn)
}
so the declaration becomes unwieldy pretty quickly.
What design pattern can I use to achieve:
- Many concrete classes implementing an interface ISomeInterface AND
- Using concrete parameters/return values that are implementing the necessary interfaces IIn, IOut?
There will be many methods on ISomeInteface with different types for the parameter/interface combos.
public interface ISomeInterface { TOut SomeMethod<TOut, TIn>(IIn aIn) where TOut: IOut where TIn : IIn; }
, It will be the callers responsablity to set the types of TOut and TIn when calling SomeMethod.