Skip to content

Commit bf98d7f

Browse files
feature: better entity extensions
- entity.Reduce(fact) - entity.Reduce(facts) - entity.Execute(command) - entity.ExecuteAndReduce(command) - entity.ExecuteAndReduce(commands)
1 parent c56733f commit bf98d7f

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed
Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,64 @@
11
using EntityDb.Abstractions.Commands;
22
using EntityDb.Abstractions.Facts;
33
using System.Collections.Generic;
4-
4+
using System.Linq;
5+
56
namespace EntityDb.Common.Extensions
67
{
78
/// <summary>
89
/// Extension methods for entities.
910
/// </summary>
1011
public static class EntityExtensions
1112
{
13+
/// <summary>
14+
/// Returns a new <typeparamref name="TEntity"/> that incorporates the modification of a single fact into an entity.
15+
/// </summary>
16+
/// <typeparam name="TEntity">The type of the entity to be modified.</typeparam>
17+
/// <param name="entity">The entity to be modified.</param>
18+
/// <param name="fact">The fact that modifies the entity.</param>
19+
/// <returns>A new <typeparamref name="TEntity"/> that incorporates the modification of <paramref name="fact"/> into <paramref name="entity"/>.</returns>
20+
public static TEntity Reduce<TEntity>(this TEntity entity, IFact<TEntity> fact)
21+
{
22+
return fact.Reduce(entity);
23+
}
24+
1225
/// <summary>
1326
/// Returns a new <typeparamref name="TEntity"/> that incorporates the modifications of a set of facts into an entity.
1427
/// </summary>
1528
/// <typeparam name="TEntity">The type of the entity to be modified.</typeparam>
16-
/// <param name="facts">The facts that modify the entity.</param>
1729
/// <param name="entity">The entity to be modified.</param>
30+
/// <param name="facts">The facts that modify the entity.</param>
1831
/// <returns>A new <typeparamref name="TEntity"/> that incorporates the modifications of <paramref name="facts"/> into <paramref name="entity"/>.</returns>
19-
public static TEntity Reduce<TEntity>(this IEnumerable<IFact<TEntity>> facts, TEntity entity)
32+
public static TEntity Reduce<TEntity>(this TEntity entity, IEnumerable<IFact<TEntity>> facts)
2033
{
21-
foreach (var fact in facts)
22-
{
23-
entity = fact.Reduce(entity);
24-
}
34+
return facts.Aggregate(entity, Reduce);
35+
}
2536

26-
return entity;
37+
/// <summary>
38+
/// Returns the enumerated modifications of a command for a given entity.
39+
/// </summary>
40+
/// <typeparam name="TEntity">The type of the entity to be modified.</typeparam>
41+
/// <param name="entity">The entity to be modified.</param>
42+
/// <param name="command">The command to execute.</param>
43+
/// <returns>The enumerated modifications of <paramref name="command"/> for <paramref name="entity"/>.</returns>
44+
public static List<IFact<TEntity>> Execute<TEntity>(this TEntity entity, ICommand<TEntity> command)
45+
{
46+
return command.Execute(entity).ToList();
2747
}
2848

2949
/// <summary>
30-
/// Returns a new instance of <typeparamref name="TEntity"/> that incorporates the modification of a command into an entity.
50+
/// Returns a new instance of <typeparamref name="TEntity"/> that incorporates the modifications of a command into an entity.
3151
/// </summary>
3252
/// <typeparam name="TEntity">The type of the entity to be modified.</typeparam>
3353
/// <param name="entity">The entity to be modified.</param>
3454
/// <param name="command">The command to execute.</param>
35-
/// <returns>A new instace of <typeparamref name="TEntity"/> that incorporates the modification of <paramref name="command"/> into <paramref name="entity"/>.</returns>
55+
/// <returns>A new instace of <typeparamref name="TEntity"/> that incorporates the modifications of <paramref name="command"/> into <paramref name="entity"/>.</returns>
3656
/// <remarks>
3757
/// This method is ONLY intended to be used for business logic tests.
3858
/// </remarks>
3959
public static TEntity ExecuteAndReduce<TEntity>(this TEntity entity, ICommand<TEntity> command)
4060
{
41-
return command.Execute(entity).Reduce(entity);
61+
return entity.Reduce(command.Execute(entity));
4262
}
4363

4464
/// <summary>
@@ -53,12 +73,7 @@ public static TEntity ExecuteAndReduce<TEntity>(this TEntity entity, ICommand<TE
5373
/// </remarks>
5474
public static TEntity ExecuteAndReduce<TEntity>(this TEntity entity, IEnumerable<ICommand<TEntity>> commands)
5575
{
56-
foreach (var command in commands)
57-
{
58-
entity = entity.ExecuteAndReduce(command);
59-
}
60-
61-
return entity;
76+
return commands.Aggregate(entity, ExecuteAndReduce);
6277
}
6378
}
6479
}

EntityDb.Common/Extensions/IServiceProviderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public static async Task<TEntity> GetEntity<TEntity>(this IServiceProvider servi
189189

190190
var facts = await transactionRepository.GetFacts(factQuery);
191191

192-
entity = facts.Reduce(entity);
192+
entity = entity.Reduce(facts);
193193

194194
if (serviceProvider.ShouldCache(snapshot, entity) && snapshotRepository != null)
195195
{

EntityDb.Common/Transactions/TransactionBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ private void AddTransactionCommand(Guid entityId, ICommand<TEntity> command)
4444
throw new CommandNotAuthorizedException();
4545
}
4646

47-
var nextFacts = command.Execute(previousEntity).ToList();
47+
var nextFacts = previousEntity.Execute(command);
4848

4949
nextFacts.Add(_serviceProvider.GetVersionNumberFact<TEntity>(previousVersionNumber + 1));
5050

51-
var nextEntity = nextFacts.Reduce(previousEntity);
51+
var nextEntity = previousEntity.Reduce(nextFacts);
5252
var nextTags = _serviceProvider.GetTags(nextEntity);
5353

5454
var transactionFacts = new List<TransactionFact<TEntity>>();

0 commit comments

Comments
 (0)