仅当 Option 为 Some 值而非 None 时才调用 Langext MapAsync

发布于 2025-01-14 08:19:55 字数 651 浏览 2 评论 0原文

我使用 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.?并确保仅当选项是某个用户而不是无用户时才执行 ma​​pAsyncma​​p 链接。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘亓 2025-01-21 08:19:55

github 上的 This 页面似乎表明该选项将具有 .IfSome() 函数。 github 提供了以下代码片段:

// Returns the Some case 'as is' and 10 in the None case
int x = optional.IfNone(10);

// As above, but invokes a Func<T> to return a valid value for x
int x = optional.IfNone(() => GetAlternative());

// Invokes an Action<T> if in the Some state;
optional.IfSome(x => Console.WriteLine(x));

关于实施的建议(不知道项目的完整想法)将是这样的:

public async Task<Option<UserViewModel>> GetUser(int num)
{
    var user = await GetUser(1);
    if (user.IsSome)
    {
        user.MapAsync(this.alterName)
            .MapAsync(this.alterCountry)
            .MapAsync(this.applyMoreChange)
            .Map(this.mapper.map<ViewModel>);

        return user;
    }
}

This page on github seems to indicate that option will have a .IfSome() function. The github gives the following code snippet:

// Returns the Some case 'as is' and 10 in the None case
int x = optional.IfNone(10);

// As above, but invokes a Func<T> to return a valid value for x
int x = optional.IfNone(() => GetAlternative());

// Invokes an Action<T> if in the Some state;
optional.IfSome(x => Console.WriteLine(x));

A suggestion on implementation (without knowing the full idea of your project) would be something like this:

public async Task<Option<UserViewModel>> GetUser(int num)
{
    var user = await GetUser(1);
    if (user.IsSome)
    {
        user.MapAsync(this.alterName)
            .MapAsync(this.alterCountry)
            .MapAsync(this.applyMoreChange)
            .Map(this.mapper.map<ViewModel>);

        return user;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文