我正在将 C# 中的 Marten 示例改编为 F#,但奇怪的是无法真正获得我的聚合的正确值:
open System
open Marten
open Marten.Schema.Identity
type AccountCreation = {
Owner: string
AccountId: Guid
CreatedAt: DateTimeOffset
StartingBalance: decimal
}
type Transaction = {
To: Guid
From: Guid
Description: string
Time: DateTimeOffset
Amount: decimal
}
type AccountEvent =
| AccountCreated of AccountCreation
| AccountCredited of Transaction
| AccountDebited of Transaction
type Account() =
member val Id = Unchecked.defaultof<Guid> with get,set
member val Owner = Unchecked.defaultof<string> with get,set
member val Balance = Unchecked.defaultof<decimal> with get,set
member val CreatedAt = Unchecked.defaultof<DateTimeOffset> with get,set
member val UpdatedAt = Unchecked.defaultof<DateTimeOffset> with get,set
member this.Apply(accountEvent: AccountEvent) =
match accountEvent with
| AccountEvent.AccountCreated accountCreation ->
this.Id <- accountCreation.AccountId
this.Owner <- accountCreation.Owner
this.Balance <- accountCreation.StartingBalance
this.CreatedAt <- accountCreation.CreatedAt
this.UpdatedAt <- accountCreation.CreatedAt
| _ -> ()
[<EntryPoint>]
let main argv =
use store = DocumentStore.For(fun options ->
let connectionString = sprintf "host=%s;database=%s;username=%s;password=%s"
"localhost"
"postgres"
"root"
"root"
options.Connection(connectionString)
options.Events.AddEventType(typeof<AccountEvent>)
options.Events.InlineProjections.AggregateStreamsWith<Account>() |> ignore
)
use session = store.LightweightSession()
let khalidId = CombGuidIdGeneration.NewGuid()
let khalid = AccountEvent.AccountCreated({
Owner = "Khalid Abuhakmeh"
AccountId = khalidId
StartingBalance = 1000m
CreatedAt = DateTimeOffset.UtcNow
})
session.Events.Append(khalidId, khalid) |> ignore
session.SaveChangesAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
let account = session.LoadAsync<Account>(khalidId)
|> Async.AwaitTask
|> Async.RunSynchronously
let stream = session.Events.FetchStream(khalidId)
printfn "%A" account
printfn "%A" stream
0
问题是当请求具有相关id的聚合对应的文档时,它只是返回null
let account = session.LoadAsync<Account>(khalidId)
|> Async.AwaitTask
|> Async.RunSynchronously
奇怪地获取事件流按预期返回事件:
let stream = session.Events.FetchStream(khalidId)
就像投影从未构建或存储,因此从未调用Account聚合Apply方法。
资料来源:Marten - 预测
[编辑]
事实证明,如果我不使用受歧视的工会,而是直接使用不同案例的类型,它会起作用:
open System
open Marten
open Marten.Schema.Identity
type AccountCreation = {
Owner: string
AccountId: Guid
CreatedAt: DateTimeOffset
StartingBalance: decimal
}
type Transaction = {
To: Guid
From: Guid
Description: string
Time: DateTimeOffset
Amount: decimal
}
type AccountEvent =
| AccountCreated of AccountCreation
| AccountCredited of Transaction
| AccountDebited of Transaction
type Account() =
member val Id = Unchecked.defaultof<Guid> with get,set
member val Owner = Unchecked.defaultof<string> with get,set
member val Balance = Unchecked.defaultof<decimal> with get,set
member val CreatedAt = Unchecked.defaultof<DateTimeOffset> with get,set
member val UpdatedAt = Unchecked.defaultof<DateTimeOffset> with get,set
member this.Apply(accountCreation: AccountCreation) =
this.Id <- accountCreation.AccountId
this.Owner <- accountCreation.Owner
this.Balance <- accountCreation.StartingBalance
this.CreatedAt <- accountCreation.CreatedAt
this.UpdatedAt <- accountCreation.CreatedAt
[<EntryPoint>]
let main argv =
use store = DocumentStore.For(fun options ->
let connectionString = sprintf "host=%s;database=%s;username=%s;password=%s"
"localhost"
"postgres"
"root"
"root"
options.Connection(connectionString)
options.Events.AddEventType(typeof<AccountCreation>)
options.Events.AddEventType(typeof<Transaction>)
options.Events.InlineProjections.AggregateStreamsWith<Account>() |> ignore
)
use session = store.LightweightSession()
let khalidId = CombGuidIdGeneration.NewGuid()
let khalid = {
Owner = "Khalid Abuhakmeh"
AccountId = khalidId
StartingBalance = 1000m
CreatedAt = DateTimeOffset.UtcNow
}
session.Events.Append(khalidId, khalid) |> ignore
session.SaveChangesAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
let account = session.LoadAsync<Account>(khalidId)
|> Async.AwaitTask
|> Async.RunSynchronously
let stream = session.Events.FetchStream(khalidId)
printfn "%A" account
printfn "%A" stream
0
[编辑] 我在 GitHub 上打开了一个问题:https ://github.com/JasperFx/marten/issues/1283