仅当 Option 为 Some 值而非 None 时才调用 Langext MapAsync
我使用 LangExt 库,并且存在 UserId 1 不存在用户的情况。
public async Task<Option<UserViewModel> GetUser()
=> await GetUser(1)
.MapAsync(this.alterName)
.MapAsync(this.alterCountry)
.MapAsync(this.applyMoreChange)
.Map(this.mapper.map<ViewModel>);
由于 userId 1 不存在用户,因此 GetUser(1)
返回 Option
并且其余代码失败并出现异常
LangExt.ValueIsNoneException : Value is None.
at languageExt.OptionAsyncAwaiter`1.GetResult()
How to handle this case.?并确保仅当选项是某个用户而不是无用户时才执行 mapAsync 和 map 链接。
I use LangExt library and i have the case of no user exists with UserId 1.
public async Task<Option<UserViewModel> GetUser()
=> await GetUser(1)
.MapAsync(this.alterName)
.MapAsync(this.alterCountry)
.MapAsync(this.applyMoreChange)
.Map(this.mapper.map<ViewModel>);
since no user exists for userId 1, the GetUser(1)
returns Option<None>
and the remaining code fails with the exception
LangExt.ValueIsNoneException : Value is None.
at languageExt.OptionAsyncAwaiter`1.GetResult()
How to handle this case.? and to make sure the mapAsync and map chaining execute only if option is some user and not none.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
github 上的 This 页面似乎表明该选项将具有 .IfSome() 函数。 github 提供了以下代码片段:
关于实施的建议(不知道项目的完整想法)将是这样的:
This page on github seems to indicate that option will have a .IfSome() function. The github gives the following code snippet:
A suggestion on implementation (without knowing the full idea of your project) would be something like this: