How to eliminate the duplication in these 2 interfaces caused by using struct as parameter?
I have 2 C# interfaces like this:
public interface IEvernoteJobListener
{
void CopyS3File(S3Location src, EvernoteLocation des, Action<Exception> complete);
}
public interface ICopyJobListener
{
void CopyS3File(S3Location src, S3Location des, Action<Exception> complete);
}
You can see that they are almost identical besides their second parameter, which makes the implementation nearly identical. What stops me from merge them into a single interface is that EvernoteLocation and S3Location are structs, so I can't make them inherit from the same parent and thus eliminate the difference between them and only keep one interface and one method.
What are the possible solutions to eliminate the duplication of the interface?
Edit:
The code of implementation might help clarify the question:
public void CopyS3File (S3Location src, EvernoteLocation des, Action<Exception> complete)
{
reader.ReadS3ToFile(src, (file, readExc) => {
if(readExc != null)
{
complete(readExc);
return;
}
// This is a Evernote writer.
writer.WriteFromFile(file, des, writeExc => {
complete(writeExc);
});
});
}
public void CopyS3File (S3Location src, S3Location des, Action<Exception> complete)
{
reader.Re开发者_开发技巧adS3ToFile(src, (file, readExc) => {
if(readExc != null)
{
complete(readExc);
return;
}
// This is a S3 writer.
writer.WriteFromFile(file, des, writeExc => {
complete(writeExc);
});
});
}
You could make the interface generic.
public interface IJobListener<TDes>
{
void CopyS3File(S3Location src, TDes des, Action<Exception> complete);
}
But I don't know how that helps with your implementation.
Why not implement an interface on both structs and use that as input parameter?
public struct S3Location : ICanHasInterface
{
//...
}
public struct EvernoteLocation : ICanHasInterface
{
//...
}
public interface ICopyJobListener
{
void CopyS3File(S3Location src, ICanHasInterface des, Action<Exception> complete);
}
Just use generic type parameter:
interface IJobLIstener<T>
{
void CopyS3File(S3Location src, T des, Action<Exception> complete)
}
public interface IEvernoteJobListener: IJobLIstener<EvernoteLocation>
{
}
public interface ICopyJobListener: IJobLIstener<S3Location>
{
}
BTW,
if S3Location encapsulates more than 2-3 primitive type values I would recommend to make it class
rather than structure
精彩评论