标签: try-with

F#问题与异步工作流和try/with

我正在努力整理一个简单的功能.

考虑以下定义:

type Entity = {Id:int;Data:string}

type IRepository =
  abstract member SaveAsync: array<Entity> -> Task<bool>
  abstract member RollBackAsync: array<Entity> -> Task<bool>

type INotification =
  abstract member SaveAsync: array<Entity> -> Task<bool>
Run Code Online (Sandbox Code Playgroud)

Task<T>是因为它们是用其他.NET语言开发的库.

(我为了这个例子创建了这段代码)

基本上,我想在存储库服务中保存数据,然后将数据保存在通知服务中.但是如果第二个操作失败,并且包含异常,我想回滚存储库中的操作.然后有两种情况我想要调用回滚操作,第一种if notification.SaveAsync返回false,第二种if抛出异常.当然,我想编写一次调用回滚,但我找不到方法.

这是我尝试过的:

type Controller(repository:IRepository, notification:INotification) =

  let saveEntities entities:Async<bool> = async{

    let! repoResult =  Async.AwaitTask <| repository.SaveAsync(entities)
    if(not repoResult) then
      return false
    else 
      let notifResult =
        try
           let! nr = Async.AwaitTask <| notification.SaveAsync(entities)
           nr
        with
          | _-> false

      if(not notifResult) then
        let forget = …
Run Code Online (Sandbox Code Playgroud)

f# asynchronous try-with

3
推荐指数
1
解决办法
261
查看次数

F#如何捕获所有异常

我知道如何捕获特定的异常,如下例所示:

let test_zip_archive candidate_zip_archive =
  let rc =
      try
        ZipFile.Open(candidate_zip_archive.ToString(), ZipArchiveMode.Read) |> ignore
      zip_file_ok
      with
      | :? System.IO.InvalidDataException -> not_a_zip_file
      | :? System.IO.FileNotFoundException -> file_not_found         
      | :? System.NotSupportedException -> unsupported_exception
  rc
Run Code Online (Sandbox Code Playgroud)

我正在阅读一堆文章,看看我是否可以在with通配符匹配中使用泛型异常.这样的构造是否存在,如果存在,它是什么?

.net f# wildcard pattern-matching try-with

2
推荐指数
2
解决办法
1132
查看次数

标签 统计

f# ×2

try-with ×2

.net ×1

asynchronous ×1

pattern-matching ×1

wildcard ×1