Return 来自 RavenDb 中 patchcommand 的实际修补 属性 值

Return actual patched property value from patchcommand in RavenDb

A 有一个 "Billing" class 描述了我在多线程应用程序中的财务帐户模型。如果我想将钱从一个帐单转移到另一个帐单,我会制作补丁命令,即递增一个 属性 并递减另一个。在没有冗余加载查询的情况下修补后 return 实际 属性 值的正确方法是什么?

结算class

public class Billing
{
    public string Id { get; set; }
    public decimal Balance { get; set; }
}

我的补丁方法

public void TransferMoney(string billingSrc, string billingDst, decimal money)
    {
        _ctx.DatabaseCommands.Batch(new ICommandData[]
        {                
            new ScriptedPatchCommandData
            {
                Key = billingSrc,
                Patch = new ScriptedPatchRequest
                {
                    Script = @"this.Balance -= money;",
                    Values = {{"money", money}}
                }
            },
            new ScriptedPatchCommandData
            {
                Key = billingDst,
                Patch = new ScriptedPatchRequest
                {
                    Script = @"this.Balance += money;",
                    Values = {{"money", money}}
                }
            }
        });
    }

您可以使用 output() 方法将值发送回用户。

自 RavenDb v3 起,ScriptedPatchRequest 中可用的一组预定义 JavaScript 函数具有方法 output(message),它允许调试补丁并在输出中打印传递的消息。请参阅 v3.0, v3.5, v4.0.

的文档

参见下面的示例:

var balancePatch = dbSession.Advanced
    .DocumentStore
    .DatabaseCommands
    .Patch(
        billingDst /* document ID */,
        new ScriptedPatchRequest
        {
            Script = @"this.Balance += money; output(this.Balance);",
            Values = {{"money", money}}
        });

// Reading the debug output
var balance = balancePatch["Debug"].Values().First().Value<decimal>();