I have recently came to this dilemma: let's say I want to use two libraries each of them defines a interface for a consumer and a consumable:
#region Library A
// This region represents a library (A) defining some interfaces
/// <summary>
/// Interface to be implemented by any processor of type A
/// </summary>
interface AProcessor
{
void Process(AProcessable a);
}
/// <summary>
/// Interface to be implemented by any object processable by a processor of type A
/// </summary>
interface AProcessable
{
int MyPropertyA { get; set; }
}
#endregion
#region Library B
// This region represents a library (B) defining some other interfaces
/// <summary>
/// Interface to be implemented by any processor of type B
/// </summary>
interface BProcessor
{
void Process(BProcessable a);
}
/// <summary>
/// Interface to be implemented by any object processable by a processor of type B
/// </summary>
interface BProcessable
{
int MyPropertyB { get; set; }
}
#endregion
Then, on my application, I implement a consumable type that implements both consumable interfaces from each library:
/// <summary>
/// A type processable by processors of type A and processors of type B.
/// </summary>
class Processable :
AProcessable,
BProcessable
{
// Implements interface AProcessable
public int MyPropertyA { get; set; }
// Implements interface BProcessable
public int MyPropertyB { get; set; }
}
And I implement a consumer type capable of consume objects of type Processable, which implements AProcessable and BProcessable:
/// <summary>
/// A processor that implements an A-type processor and a B-type processor.
/// </summary>
class Processor :
AProcessor,
BProcessor
{
// I thought that Process method would implement AProcessor.Process() and
// BProcessor.Process() as it expects a Processable object as parameter which
// implements AProcessable and BProcessable, but it doesn't.
// Compiler rises an error telling that Processor doesn't implement neither
// AProcessor.Process() nor BProcessor.Process()
/*
public void Process(Processable a)
{
throw new NotImplementedException();
}
*/
// Implementing both methods does not work because it creates ambiguity: when
// we call Processor.Process(p) being "p" a Processable object the compiler
// doesn't know if it has to call Processor.Process(AProcessable) or
// Processor.Process(BProcessable).
/*
public void Process(AProcessable a)
{
throw new NotImplementedException();
}
public void Process(BProcessable a)
{
throw new NotImplementedException();
}
*/
}
¿What's my design error and what would be the correct approach?
Thanks in advance.