减少本地定义字段的数量

发布于 2024-12-01 16:21:44 字数 854 浏览 0 评论 0原文

我有一个类,它有许多遵循以下模式的方法:

public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
{
  _getSomeData1ResponseHandler = responseHandler;
  Transactions.GetSomeData1(SomeData1ResponseHandler, parameters);
}

private void SomeData1ResponseHandler(Response response)
{
  if (response != null)
  {
    CreateSomeData1(response.Data);
  }
}

public void CreateSomeData1(Data data)
{
  //Create the objects and then invoke the Action
  ...

  if(_getSomeData1ResponseHandler != null)          
  {
    _getSomeData1ResponseHandler();
  }
}

因此,某些外部类调用 GetSomeData1 并传入一个 Action,该 Action 应该在数据可用时调用。 GetSomeData1 存储响应 Action 并调用 Transactions.GetSomeData1 (异步调用)。一旦异步调用完成,就会创建数据并调用原始传入的 Action。

问题是,对于每个不同的 GetSomeData 调用,我需要存储传入的 Action,以便稍后引用它。有没有办法可以将原始操作传递给最终方法(CreateSomeData1)而不存储它?

谢谢。

I have a class that has a number of methods that follow the below pattern:

public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
{
  _getSomeData1ResponseHandler = responseHandler;
  Transactions.GetSomeData1(SomeData1ResponseHandler, parameters);
}

private void SomeData1ResponseHandler(Response response)
{
  if (response != null)
  {
    CreateSomeData1(response.Data);
  }
}

public void CreateSomeData1(Data data)
{
  //Create the objects and then invoke the Action
  ...

  if(_getSomeData1ResponseHandler != null)          
  {
    _getSomeData1ResponseHandler();
  }
}

So, some external class calls GetSomeData1 and passes in an Action, which should be called when the data is available. GetSomeData1 stores the response Action and calls Transactions.GetSomeData1 (an async call). Once that async call is finished, the data is created and the original passed in Action is called.

The thing is, for every different GetSomeData call I have, I need to store the passed in Action so I can reference it later. Is there a way I can somehow pass the original Action to the final method (CreateSomeData1) without storing it?

Thanks.

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

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

发布评论

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

评论(1

只是偏爱你 2024-12-08 16:21:44

您可以使用 lambda 表达式将数据作为捕获的变量传递。不需要类字段。

    public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
    {
      Transactions.GetSomeData1(response => SomeData1ResponseHandler(response, responseHandler), parameters);
    }

    private void SomeData1ResponseHandler(Response response, Action responseHandler)
    {
      if (response != null)
      {
        CreateSomeData1(response.Data, responseHandler);
      }
    }

    public void CreateSomeData1(Data data, Action responseHandler)
    {
      //Create the objects and then invoke the Action
      ...

      if(responseHandler != null)          
      {
        responseHandler();
      }
    }

在内部,编译器将创建类和字段来保存数据,但您编写的代码更干净且更易于维护。

You can pass the data along as a captured variable with lambda expressions. No need for class fields.

    public void GetSomeData1(DataParameters[] parameters, Action responseHandler)
    {
      Transactions.GetSomeData1(response => SomeData1ResponseHandler(response, responseHandler), parameters);
    }

    private void SomeData1ResponseHandler(Response response, Action responseHandler)
    {
      if (response != null)
      {
        CreateSomeData1(response.Data, responseHandler);
      }
    }

    public void CreateSomeData1(Data data, Action responseHandler)
    {
      //Create the objects and then invoke the Action
      ...

      if(responseHandler != null)          
      {
        responseHandler();
      }
    }

Internally, the compiler will create classes and fields to hold the data, but the code you write is cleaner and easier to maintain.

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