说我有这段代码:
private void CreateSnapshots(IEnumerable<StreamHead> streams)
{
foreach (StreamHead head in streams)
{
IAggregate aggregate = ???;
IMemento memento = aggregate.GetSnapshot();
var snapshot = new Snapshot(head.StreamId, head.SnapshotRevision + 1, memento);
eventStore.AddSnapshot(snapshot);
observer.Notify(new SnapshotTaken(head.StreamId, head.HeadRevision));
}
}
如何知道要为当前流加载的聚合?我也在使用CommonDomain。那里有东西吗?
由于
答案 0 :(得分:3)
EventStore的快照方面需要一点爱。我试图使IStoreEvents接口适合使用单个聚合。我还试图确保快照不会干扰或妨碍正常使用。
自v2.0发布以来,我现在已将注意力转向v2.1,我将能够进行一些与此相关的小API更改。与此同时,您最好的选择可能是在进行快照时完全绕过IStoreEvents。
另一种方法是使用常规代码在进程中运行快照代码。加载聚合时需要快照,您可以轻松地将对该聚合的引用异步推送到快照代码。通过这种方式,您实际上不必进行加载,因为您已经有了聚合。
答案 1 :(得分:1)
我找到了一个解决方案对我来说(这绝对是一个黑客行为)。它仍然是带外快照。这是它的sample。
private void CreateSnapshots(IEnumerable<StreamHead> streams)
{
foreach (StreamHead head in streams)
{
//NOTE: This uses a patched version of EventStore that loads commit headers in OptimisticEventStream.PopulateStream()
// <code>
// this.identifiers.Add(commit.CommitId);
// this.headers = this.headers.Union(commit.Headers).ToDictionary(k => k.Key, k => k.Value);
// </code>
var stream = eventStore.OpenStream(head.StreamId, int.MinValue, int.MaxValue);
//NOTE: Nasty hack but it works.
var aggregateType = stream.UncommittedHeaders.Where(p=>p.Key=="AggregateType").First();
var type = aggregateTypeResolver(aggregateType.Value.ToString());
MethodInfo methodInfo = typeof(IRepository).GetMethod("GetById");
MethodInfo method = methodInfo.MakeGenericMethod(type);
object o = method.Invoke(repository, new object[]{head.StreamId, head.HeadRevision});
var aggregate = (IAggregate) o;
IMemento memento = aggregate.GetSnapshot();
var snapshot = new Snapshot(head.StreamId, head.HeadRevision, memento);
eventStore.AddSnapshot(snapshot);
observer.Notify(new SnapshotTaken(head.StreamId, head.HeadRevision));
}
}