【发布时间】:2020-02-27 22:16:21
【问题描述】:
我正在尝试在 F# 中迁移 C# 程序。 该程序是一个FtpClient,它封装了FluentFtp库并通过如下接口暴露ftp方法:
public interface IFtpSession
{
IEnumerable<string> ListFiles(string remoteFolder);
}
sealed class FtpSession : IFtpSession
{
private FluentFTP.FtpClient FluentFtpClient { get; set; }
internal FtpSession(FluentFTP.FtpClient fluentFtpClient)
{
this.FluentFtpClient = fluentFtpClient;
}
public IEnumerable<string> ListFiles(string remoteFolder)
{
return FluentFtpClient.GetListing(remoteFolder).Where(fileItem => fileItem.Type == FtpFileSystemObjectType.File).Select(fileItem => fileItem.FullName);
}
}}
private static void CreateFtpSession(FtpConnectionSettings settings, Action<IFtpSession> onSessionOpen) {
FluentFTP.FtpClient ftpClient = new FluentFTP.FtpClient(settings.Host, settings.Port, settings.UserName, settings.UserPassword);
ftpClient.Connect();
FtpSession ftpSession = new FtpSession(ftpClient);
onSessionOpen(ftpSession);
ftpClient.Disconnect();
}
我想在 F# 中做同样的事情,但以一种功能性的方式。这意味着我不想使用具有成员的对象以 OO F# 样式复制上述代码,而是仅使用函数和值类型。
做这种工作的正确方法是什么?
在上面的示例中,只有一个函数(ListFiles),但我的问题是假设接口 IFtpSession 可以具有其他功能并且不尊重单一责任原则。
非常感谢您的帮助
【问题讨论】:
标签: design-patterns callback functional-programming f#