我正在努力整理一个简单的功能.
考虑以下定义:
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) 我知道如何捕获特定的异常,如下例所示:
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通配符匹配中使用泛型异常.这样的构造是否存在,如果存在,它是什么?