vendredi 1 novembre 2019

F# functional Callback

I'm trying to migrate a C# program in F#. The program is a FtpClient that wrap the FluentFtp library and expose the ftp methods through an interface as follow:

  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();
    }

I would like to do the same thing in F# but in a functional way. That means that I don't want to use objects with members to replicate the above code in a OO F# style, but using only functions and value types.

What is the correct way to do this kind of job?

In the example above there is only one function (ListFiles), but my question assume that the interface IFtpSession could have other functions and doesn't respect the single responsability principle.

Thanks a lot for your help

Aucun commentaire:

Enregistrer un commentaire