Since, two methods with the same parameters but different return values will not compile. What is the best way to define this interface without loosing clarity?
public interface IDuplexChannel<T, U>
{
void Send(T value, int timeout = -1);
void Send(U value, int timeout = -1);
bool TrySend(T value, int timeout = -1);
bool TrySend(U value, int timeout = -1);
T Receive(int timeout = -1);
U Receive(int timeout = -1);
bool TryReceive(out T value, int timeout = -1);
bool TryReceive(out U value, int timeout = -1);
}
I considered using out params but that would make it a little awkward to use.
public interface IDuplexChannel<T, U>
{
void Send(T value, int timeout = -1);
void Send(U value, int timeout = -1);
bool TrySend(T value, int timeout = -1);
bool TrySend(U value, int timeout = -1);
void Receive(out T value, int timeout = -1);
void Receive(out U value, int timeout = -1);
bool TryReceive(out T value, int timeout = -1);
bool TryReceive(out U value, int timeout = -1);
}
Generic version, a little unwieldy but it works.
public interface IDuplexChannel<T, U>
{
void Send(T value, int timeout = -1);
void Send(U value, int timeout = -1);
bool TrySend(T value, int timeout = -1);
bool TrySend(U value, int timeout = -1);
V Receive<V>(int timeout = -1) where V : T, U;
bool TryReceive(out T value, int timeout = -1);
bool TryReceive(out U value, int timeout = -1);
}
The main problem is you are trying to view the duplex channel from both ends at the same time. Data travels both ways on a duplex channel, but there are still definite endpoints. What you send on one end is what you receive on the other.
That said, you should be using WCF anyway, especially since you’re using .NET 4.0.
Edit: Pictures