Exemplo n.º 1
0
 public Task AcceptAggregateRootChanges(IAggregateRoot aggregateRoot)
 {
     lock (_lockObj)
     {
         if (aggregateRoot == null)
         {
             throw new ArgumentNullException("aggregateRoot");
         }
         _aggregateRootInfoDict.AddOrUpdate(aggregateRoot.UniqueId, x =>
         {
             aggregateRoot.AcceptChanges();
             _logger.InfoFormat("Aggregate root in-memory cache initialized, aggregateRootType: {0}, aggregateRootId: {1}, aggregateRootVersion: {2}", aggregateRoot.GetType().FullName, aggregateRoot.UniqueId, aggregateRoot.Version);
             return(new AggregateCacheInfo(aggregateRoot));
         }, (x, existing) =>
         {
             //更新到内存缓存前需要先检查聚合根引用是否有变化,有变化说明此聚合根已经被重置过状态了
             if (aggregateRoot.Version > 1 && existing.AggregateRoot != aggregateRoot)
             {
                 throw new AggregateRootReferenceChangedException(aggregateRoot);
             }
             //接受聚合根的最新事件修改,更新聚合根版本号
             var aggregateRootOldVersion = existing.AggregateRoot.Version;
             aggregateRoot.AcceptChanges();
             existing.UpdateAggregateRoot(aggregateRoot);
             _logger.InfoFormat("Aggregate root in-memory cache changed, aggregateRootType: {0}, aggregateRootId: {1}, aggregateRootNewVersion: {2}, aggregateRootOldVersion: {3}", aggregateRoot.GetType().FullName, aggregateRoot.UniqueId, aggregateRoot.Version, aggregateRootOldVersion);
             return(existing);
         });
     }
     return(Task.CompletedTask);
 }