我正在重构测试项目中使用的一些实用程序代码模块,并希望应用我对干净的OOP设计模式和坚实原则的知识,使这些模块更有用,更易于使用。
我有一个名为IFtpFileCleaner的接口:
///
/// Responsible for deleting a single file located in a remote server
///
public interface IFtpFileCleaner
{
///
/// Deletes a remote file
///
///
/// The full remote path.
///
void Clean(string remotePath);
}我有一个名为RemoteFileCleaner的抽象类,它实现了IFtpFileCleaner:
///
/// The file cleaner base class provides common code for all the implementations to reduce code duplication
///
public abstract class RemoteFileCleaner : IFtpFileCleaner
{
///
/// The FTP _client.
///
private readonly IFtpSecureClient _client;
///
/// Initializes a new instance of the class.
///
///
/// The FTP client.
///
protected RemoteFileCleaner(IFtpSecureClient client)
{
_client = client;
}
///
/// Deletes a single file located on a remote server
///
///
/// The full path of the remote file.
///
public void Clean(string remotePath)
{
try
{
_client.Init();
_client.DeleteFile(remotePath);
}
catch (Exception e)
{
Console.WriteLine($"Failed to clean file {remotePath}.{Environment.NewLine}{e}");
throw;
}
finally
{
_client.Disconnect();
}
Console.WriteLine($"Remote File {remotePath} was deleted.");
}
}现在,我可以有我的文件清理器的具体实现。比如SFTPFileCleaner,FTPFileCleaner和FTPSFileCleaner。它们看起来都很相似。例如,SFTPFileCleaner如下所示:
///
/// The SFTP implementation of .
///
public class SFTPFileCleaner : RemoteFileCleaner
{
///
/// Initializes a new instance of the class.
///
public SFTPFileCleaner()
: base(new FtpSecureClientTestFactory().CreateSFTPClient())
{
}
}如您所见,我在具体实现中执行"Bastard注入“,因为我希望在测试方法中很容易地实例化这些清洁剂。下面是一个示例:
[TearDown]
public void Cleanup()
{
_ftpCleaner = new SFTPFileCleaner();
_ftpCleaner.Clean(_expectedRemotePath);
}发布于 2018-05-24 16:06:11
让IRemoteFileCleaner作为基类,并将FTPFileCleaner (从IRemoteFileCleaner继承来作为所有其他类继承的类)难道不是更有意义的吗?
https://codereview.stackexchange.com/questions/195097
复制相似问题