0

I've tried using TryAsync and Bind from Language-ext but haven't been successful. This is the closest I'm to achieving it.

public virtual async Task Execute(SignDocumentCommand signDocumentCommand)
{
    (await converterRepository.FindByDocumentId(signDocumentCommand.DocumentId)).Match(
        Right: converterFindSuccessfully => 
            TryToBuildSigner(
                signDocumentCommand, 
                converterFindSuccessfully.Converter.CountryId, 
                converterFindSuccessfully.Converter.SupplierId), 
        Left: converterFindFailed =>
        {
            // TODO: ...
        });
}

private void TryToBuildSigner(
    SignDocumentCommand signDocumentCommand, 
    CountryId countryId,
    SupplierId supplierId)
{
    signerBuilder.Build(countryId, supplierId).Match(
        Right: async signerBuildSuccessful => 
            await TryToSignDocument(signDocumentCommand, signerBuildSuccessful.Signer),
        Left: signerBuildFailed => {
              // TODO: ...
        });
}

private async Task TryToSignDocument(SignDocumentCommand signDocumentCommand, Domain.Signer signer)
{
    var documentToSign = await documentToSignRepository.FindByDocumentId(signDocumentCommand.DocumentId);
    
    (await signer.Sign(documentToSign)).Match(
        Right: async signDocumentSuccessful =>
        {
            var signedDocument = new SignedDocument(
                DocumentId: signDocumentCommand.DocumentId,
                CorrelationId: signDocumentCommand.CorrelationId,
                Content: signDocumentSuccessful.DocumentContent);
            
            await TryToSaveDocumentSigned(signedDocument);
        },
        Left: signDocumentFailed =>
        {
            // TODO: ...
        });
}

private async Task TryToSaveDocumentSigned(SignedDocument signedDocument)
{
    (await signedDocumentRepository.Save(signedDocument))
        .Match(
            Right: async signedDocumentSaveSuccessful => 
                await TryToPublishDocumentSigned(signedDocument),
            Left: signedDocumentSaveFailed =>
            {
                    // TODO: Hacer acción cuándo el documento no se ha podido guardar.
            });
}

private async Task TryToPublishDocumentSigned(SignedDocument signedDocument)
{
    (await publisher.Publish(signedDocument)).Match(
        Right: successfulPublish =>
        {
            // TODO: ...
        },
        Left: failedPublish =>
        {
            // TODO: ...
        });
}

From here, how I could evolve my code to use Bind? I'm trying to do the last example of this article.

Something like

  .Bind(SignedDocument)
            .Bind(SearchSigner)
            .Bind(Sign)
            .Bind(Save)
            .Bind(Publish)
            .Match(...);

I wasn't able to use Try nor TryAsync, also notice that for each method the failed process is different, isn't the same for all of them. So, how could I achieve this? is it possible or is there any other approach? Thanks!

Omar
  • 374
  • 1
  • 14

0 Answers0