提问人:Omar 提问时间:8/8/2023 更新时间:8/8/2023 访问量:41
使用 TryAsync 和 Bind 方法使用 Language-ext 处理 C# 错误
C# Error Handling with Language-ext using TryAsync and Bind methods
问:
我尝试使用 和 来自 Language-ext,但没有成功。这是我最接近实现它的地方。TryAsync
Bind
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: ...
});
}
从这里开始,我该如何改进我的代码以使用?我正在尝试做本文的最后一个示例。Bind
类似的东西
.Bind(SignedDocument)
.Bind(SearchSigner)
.Bind(Sign)
.Bind(Save)
.Bind(Publish)
.Match(...);
我也无法使用,也注意到对于每种方法,失败的过程都是不同的,对于所有方法来说都不一样。那么,我该如何实现这一目标呢?是否可能或有任何其他方法?谢谢!Try
TryAsync
答: 暂无答案
评论