如何查询存储在 SQL Persistence table 中的 Sagas

How to query Sagas stored in SQL Persistence table

我需要查询 属性 的 Saga Data class 以获得列表。它作为序列化对象存储在 SqlPersistance table [Data] 列中。 考虑一个场景,我的 SagaData 有一个名为 UserName 的 属性,所以我想查询与该用户相关的每个 saga。 以一种草率的方式,我可以查询列内容,获取列表并可以从内容中创建 Saga 对象,方法如下:

SELECT [Id]
      ,[Correlation_TaskId]
      ,[Metadata]
      ,[Data]
      ,[PersistenceVersion]
      ,[SagaTypeVersion]
      ,[Concurrency]
  FROM [myWonderfulDb].[dbo].[MyWonderfulPeristanceTable]
  where JSON_VALUE(Data,'$.Username') = 'arthur' 

但我正在寻找一种可能使用 NserviceBus API 的优雅方法。 ParticularSoftware 文档 (link: https://docs.particular.net/persistence/sql/saga-finder) 中描述了一个 SagaFinder 实现,但这 returns 只有一个对象并不完全适合我的场景。

文档中的实现方式如下:

class SqlServerSagaFinder :
    IFindSagas<MySagaData>.Using<MyMessage>
{
    public Task<MySagaData> FindBy(MyMessage message, SynchronizedStorageSession session, ReadOnlyContextBag context)
    {
        return session.GetSagaData<MySagaData>(
            context: context,
            whereClause: "JSON_VALUE(Data,'$.PropertyPathInJson') = @propertyValue",
            appendParameters: (builder, append) =>
            {
                var parameter = builder();
                parameter.ParameterName = "propertyValue";
                parameter.Value = message.PropertyValue;
                append(parameter);
            });
    }
}

任何想法表示赞赏。谢谢!

SagaFinder 的唯一目的是找到与传入消息相关的某个传奇的单个实例。我不知道有任何框架功能可以查询 saga(数据)实例。

我们在

提供了有关查询 saga 状态的指南

https://docs.particular.net/nservicebus/sagas/#querying-saga-data

简而言之,您可以查询 saga 数据,NServiceBus 没有提供开箱即用的方法,因为我们建议改用其他方法:

the saga to publish events containing the required data and have handlers that process these events and store the data in one or more read model(s) for querying purposes.

不推荐的主要点在我看来就是这两个

  • By exposing the data outside of the safeguards of the business logic in the saga the risk is that the data is not treated as read-only. Eventually, a component tries to bypass the saga and directly modify the data.
  • Querying the data might require additional indexes, resources etc. which need to be managed by the component issuing the query. Those additional resources can influence saga performance.