File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
src/EntityDb.Common.Tests/Extensions Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ using EntityDb . Abstractions . Entities ;
2+ using EntityDb . Abstractions . Transactions ;
3+ using System ;
4+ using System . Linq ;
5+ using System . Threading . Tasks ;
6+
7+ namespace EntityDb . Common . Tests . Extensions
8+ {
9+ /// <summary>
10+ /// Extensions on <see cref="IEntityRepository{TEntity}"/>
11+ /// </summary>
12+ public static class EntityRepositoryExtensions
13+ {
14+ public static async Task ForEachCommandWithPreviousEntity < TEntity >
15+ (
16+ this IEntityRepository < TEntity > entityRepository ,
17+ ITransaction < TEntity > transaction ,
18+ Action < TEntity > processor
19+ )
20+ {
21+ var entityTaskDictionary = transaction . Commands
22+ . GroupBy ( transactionCommand => transactionCommand . EntityId )
23+ . ToDictionary
24+ (
25+ group => group . Key ,
26+ group =>
27+ {
28+ var firstCommand = group . First ( ) ;
29+
30+ return entityRepository . GetAtVersion ( firstCommand . EntityId , firstCommand . EntityVersionNumber - 1 ) ;
31+ }
32+ ) ;
33+
34+ await Task . WhenAll ( entityTaskDictionary . Values ) ;
35+
36+ var entityDictionary = entityTaskDictionary
37+ . ToDictionary
38+ (
39+ x => x . Key ,
40+ x => x . Value . Result
41+ ) ;
42+
43+ foreach ( var transactionCommand in transaction . Commands )
44+ {
45+ var previousEntity = entityDictionary [ transactionCommand . EntityId ] ;
46+
47+ processor . Invoke ( previousEntity ) ;
48+
49+ entityDictionary [ transactionCommand . EntityId ] = transactionCommand . Command . Reduce ( previousEntity ) ;
50+ }
51+ }
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments