C# - 试图重构功能编程块

发布于 2025-02-08 09:37:57 字数 1742 浏览 0 评论 0原文

我试图重构此代码,其中是否存在对象是否在缓存中存在差异。如果不在缓存中,则消耗外部API并将结果返回到lambda表达式变量。如果是,请使用缓存变量。

我的问题是,我认为代码块是两次编写的,唯一的区别是变量。

  var cacheHit = _cache.TryGetValue("ClientUserId", out string cachedUser);
  if (!cacheHit)
  {
      await _userM.GetUserAsync(
       dbData.CreateEmployeeID.ToString(),
        cancellationToken)
        .MapOnFailure(failure => new AppF(failure.Message, failure.Exception))
        .DoAsync(async user =>
        {
        _cache.Set("ClientUserId", user.ClientUserId, cacheExpiration);

        var opsRequest = new OperationStatusRequest();

        var results = await _monitoringService.LogHttpRequestResponseAsync(
            operationStatusRequest,
            JsonSerializer.Serialize(cRequest),
            async () =>
                await _client.DetailsAsync(user.ClientUserId, cancellationToken) //from API call
        .DoAsync(async _ =>
        {
            //code here
        }
        ).DoOnFailureAsync(async _ =>
        {
             //code here
        });

        }); //user
  }
  else
  {
      //don't call GetUserAsync if user is already cached, proceed with the operation

        var opsRequest = new OperationStatusRequest();

        var results = await _monitoringService.LogHttpRequestResponseAsync(
            operationStatusRequest,
            JsonSerializer.Serialize(cRequest),
            async () =>
                await _client.DetailsAsync(cachedUser, cancellationToken) //cachedUser
        .DoAsync(async _ =>
        {
            //code here
        }
        ).DoOnFailureAsync(async _ =>
        {
             //code here
        });

  }

我已经对主要区别发表了评论,但是IF中的代码几乎遵循同一操作。

有没有办法在不重新编写整个实现的情况下编写IF-ELSE块?

I am trying to refactor this code wherein there is a difference whether an object is in a cache or not. If it's not in cache, then consume an external API and return result to a lambda expression variable. If it is, use the cached variable.

My issue is that I think the code block is written twice with the only difference is the variable.

  var cacheHit = _cache.TryGetValue("ClientUserId", out string cachedUser);
  if (!cacheHit)
  {
      await _userM.GetUserAsync(
       dbData.CreateEmployeeID.ToString(),
        cancellationToken)
        .MapOnFailure(failure => new AppF(failure.Message, failure.Exception))
        .DoAsync(async user =>
        {
        _cache.Set("ClientUserId", user.ClientUserId, cacheExpiration);

        var opsRequest = new OperationStatusRequest();

        var results = await _monitoringService.LogHttpRequestResponseAsync(
            operationStatusRequest,
            JsonSerializer.Serialize(cRequest),
            async () =>
                await _client.DetailsAsync(user.ClientUserId, cancellationToken) //from API call
        .DoAsync(async _ =>
        {
            //code here
        }
        ).DoOnFailureAsync(async _ =>
        {
             //code here
        });

        }); //user
  }
  else
  {
      //don't call GetUserAsync if user is already cached, proceed with the operation

        var opsRequest = new OperationStatusRequest();

        var results = await _monitoringService.LogHttpRequestResponseAsync(
            operationStatusRequest,
            JsonSerializer.Serialize(cRequest),
            async () =>
                await _client.DetailsAsync(cachedUser, cancellationToken) //cachedUser
        .DoAsync(async _ =>
        {
            //code here
        }
        ).DoOnFailureAsync(async _ =>
        {
             //code here
        });

  }

I have placed a comment on the main difference but the code in if and else block pretty much follows the same operation.

Is there a way that I can write the if-else block without re-writing the whole implementation twice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文