4

我正在编写一个基于Quickfix/N的 FIX 引擎,它侦听交易执行 (ExecutionReport) 并将它们保存到数据库中。

如果接收到的消息中不存在该值,则从 API 请求字段值会引发 FieldNotFoundException。例如,如果帐户不存在,调用 executionReport.Account 将引发异常。

由于某些字段是可选的,因此我必须在获取字段值之前明确检查字段值是否存在。我有两种可能性:

可能性一: executionReport.IsSetAccount() ? executionReport.Account : null;

可能性2:

  try
        {
            return executionReport.Account.getValue();
        }
        catch (Exception e)
        {
            return null;

        }

第一个选项很干净,但我觉得它真的很重,第二个可以概括为辅助函数,但它违背了 API 理念,我觉得我做错了什么。

那么我的问题是:

  • 还有另一种干净/正确的方法来完成这项工作吗?
  • 还是我对协议/API 的理解完全错误?我觉得我没有以正确的方式解决问题。

    非常感谢

4

2 回答 2

5

您还没有真正说明为什么您认为这些不干净,所以我不确定您到底在寻找什么。

我能想到的唯一选择是:

// Both these lines are functionally identical.
executionReport.IsSetField(1)
executionReport.IsSetField(QuickFix.Fields.Tags.Account)

// 1 is the tag for Account.
// The second line just uses the tag enum to get 1
//   instead of hardcoding the int.

那个更好吗?

于 2017-10-11T19:41:14.040 回答
1

好的,为了避免编写适配器类或者每次我想使用 ExecutionReport 字段时可能会检查,我创建了一个扩展类来完成这项工作:

    public static class ExecutionReportExtensions
{
    public static string AccountValue(this QuickFix.FIX44.ExecutionReport executionReport)
    {
        if (executionReport.IsSetAccount())
            return executionReport.Account.getValue();
        return null;
    }

然后按如下方式使用它:

执行executionReport.AccountValue()

于 2017-10-13T10:37:03.110 回答