Skip to content

Commit c56733f

Browse files
refactor: pass entityId to IConstructingStrategy.Construct instead of ITaggingStrategy.GetTags
this way, the entityId is generally available to any current or future logic that needs to access it
1 parent 4fabda7 commit c56733f

File tree

9 files changed

+23
-22
lines changed

9 files changed

+23
-22
lines changed

EntityDb.Abstractions/Strategies/IConstructingStrategy.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace EntityDb.Abstractions.Strategies
1+
using System;
2+
3+
namespace EntityDb.Abstractions.Strategies
24
{
35
/// <summary>
46
/// Represents a type that constructs a new instance of <typeparamref name="TEntity"/>.
@@ -9,7 +11,8 @@ public interface IConstructingStrategy<TEntity>
911
/// <summary>
1012
/// Returns a new instance of a <typeparamref name="TEntity"/>.
1113
/// </summary>
14+
/// <param name="entityId">The id of the entity.</param>
1215
/// <returns>A new instaance of <typeparamref name="TEntity"/>.</returns>
13-
TEntity Construct();
16+
TEntity Construct(Guid entityId);
1417
}
1518
}

EntityDb.Abstractions/Strategies/ITaggingStrategy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ public interface ITaggingStrategy<TEntity>
1212
/// <summary>
1313
/// Returns the tags for a <typeparamref name="TEntity"/>.
1414
/// </summary>
15-
/// <param name="entityId">The id of the entity.</param>
1615
/// <param name="entity">The entity.</param>
1716
/// <returns>The tags for <paramref name="entity"/>.</returns>
18-
ITag[] GetTags(Guid entityId, TEntity entity);
17+
ITag[] GetTags(TEntity entity);
1918
}
2019
}

EntityDb.Common.Tests/Extensions/IServiceProviderExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void GivenNoTaggingStrategy_WhenGettingTags_ThenReturnEmptyArray()
2525

2626
// ACT
2727

28-
var tags = serviceProvider.GetTags(Guid.NewGuid(), new TransactionEntity());
28+
var tags = serviceProvider.GetTags(new TransactionEntity());
2929

3030
// ASSERT
3131

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using EntityDb.Abstractions.Strategies;
22
using EntityDb.Abstractions.Tags;
3-
using System;
43
using System.Collections.Generic;
54

65
namespace EntityDb.Common.Entities
@@ -10,7 +9,7 @@ namespace EntityDb.Common.Entities
109
/// </summary>
1110
public interface ITaggedEntity
1211
{
13-
/// <inheritdoc cref="ITaggingStrategy{TEntity}.GetTags(Guid, TEntity)"/>
14-
public IEnumerable<ITag> GetTags(Guid entityId);
12+
/// <inheritdoc cref="ITaggingStrategy{TEntity}.GetTags(TEntity)"/>
13+
public IEnumerable<ITag> GetTags();
1514
}
1615
}

EntityDb.Common/Extensions/IServiceProviderExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ public static async Task<ISnapshotRepository<TEntity>> CreateSnapshotRepository<
6666
/// </summary>
6767
/// <typeparam name="TEntity">The type of the entity.</typeparam>
6868
/// <param name="serviceProvider">The service provider.</param>
69+
/// <param name="entityId">The id of the entity.</param>
6970
/// <returns>A new instance of <typeparamref name="TEntity"/>.</returns>
70-
public static TEntity Construct<TEntity>(this IServiceProvider serviceProvider)
71+
public static TEntity Construct<TEntity>(this IServiceProvider serviceProvider, Guid entityId)
7172
{
7273
var constructingStrategy = serviceProvider.GetRequiredService<IConstructingStrategy<TEntity>>();
7374

74-
return constructingStrategy.Construct();
75+
return constructingStrategy.Construct(entityId);
7576
}
7677

7778
/// <summary>
@@ -107,16 +108,15 @@ public static IFact<TEntity> GetVersionNumberFact<TEntity>(this IServiceProvider
107108
/// </summary>
108109
/// <typeparam name="TEntity">The type of the entity.</typeparam>
109110
/// <param name="serviceProvider">The service provider.</param>
110-
/// <param name="entityId">The id of the entity.</param>
111111
/// <param name="entity">The entity.</param>
112112
/// <returns>The tags for <paramref name="entity"/>.</returns>
113-
public static ITag[] GetTags<TEntity>(this IServiceProvider serviceProvider, Guid entityId, TEntity entity)
113+
public static ITag[] GetTags<TEntity>(this IServiceProvider serviceProvider, TEntity entity)
114114
{
115115
var taggingStrategy = serviceProvider.GetService<ITaggingStrategy<TEntity>>();
116116

117117
if (taggingStrategy != null)
118118
{
119-
return taggingStrategy.GetTags(entityId, entity);
119+
return taggingStrategy.GetTags(entity);
120120
}
121121

122122
return Array.Empty<ITag>();
@@ -181,7 +181,7 @@ public static async Task<TEntity> GetEntity<TEntity>(this IServiceProvider servi
181181
snapshot = await snapshotRepository.GetSnapshot(entityId);
182182
}
183183

184-
var entity = snapshot ?? serviceProvider.Construct<TEntity>();
184+
var entity = snapshot ?? serviceProvider.Construct<TEntity>(entityId);
185185

186186
var versionNumber = serviceProvider.GetVersionNumber(entity);
187187

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using EntityDb.Abstractions.Strategies;
22
using EntityDb.Abstractions.Tags;
33
using EntityDb.Common.Entities;
4-
using System;
54
using System.Linq;
65

76
namespace EntityDb.Common.Strategies
87
{
98
internal sealed class TaggedEntityTaggingStrategy<TEntity> : ITaggingStrategy<TEntity>
109
where TEntity : ITaggedEntity
1110
{
12-
public ITag[] GetTags(Guid entityId, TEntity entity)
11+
public ITag[] GetTags(TEntity entity)
1312
{
14-
return entity.GetTags(entityId).ToArray();
13+
return entity.GetTags().ToArray();
1514
}
1615
}
1716
}

EntityDb.Common/Transactions/TransactionBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void AddTransactionCommand(Guid entityId, ICommand<TEntity> command)
3737
{
3838
var previousEntity = _knownEntities[entityId];
3939
var previousVersionNumber = _serviceProvider.GetVersionNumber(previousEntity);
40-
var previousTags = _serviceProvider.GetTags(entityId, previousEntity);
40+
var previousTags = _serviceProvider.GetTags(previousEntity);
4141

4242
if (_serviceProvider.IsAuthorized(previousEntity, command, _claimsPrincipal) == false)
4343
{
@@ -49,7 +49,7 @@ private void AddTransactionCommand(Guid entityId, ICommand<TEntity> command)
4949
nextFacts.Add(_serviceProvider.GetVersionNumberFact<TEntity>(previousVersionNumber + 1));
5050

5151
var nextEntity = nextFacts.Reduce(previousEntity);
52-
var nextTags = _serviceProvider.GetTags(entityId, nextEntity);
52+
var nextTags = _serviceProvider.GetTags(nextEntity);
5353

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

@@ -113,7 +113,7 @@ public async Task Load(Guid entityId, ITransactionRepository<TEntity> transactio
113113
/// </remarks>
114114
public TransactionBuilder<TEntity> Create(Guid entityId, ICommand<TEntity> command)
115115
{
116-
var entity = _serviceProvider.Construct<TEntity>();
116+
var entity = _serviceProvider.Construct<TEntity>(entityId);
117117

118118
if (_knownEntities.ContainsKey(entityId))
119119
{

EntityDb.TestImplementations/Entities/TransactionEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public TransactionEntity WithVersionNumber(ulong versionNumber)
3636
return this with { VersionNumber = versionNumber };
3737
}
3838

39-
public IEnumerable<ITag> GetTags(Guid entityId)
39+
public IEnumerable<ITag> GetTags()
4040
{
4141
if (Tags != null)
4242
{

EntityDb.TestImplementations/Entities/TransactionStateConstructingStrategy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using EntityDb.Abstractions.Strategies;
2+
using System;
23

34
namespace EntityDb.TestImplementations.Entities
45
{
56
public class TransactionEntityConstructingStrategy : IConstructingStrategy<TransactionEntity>
67
{
7-
public TransactionEntity Construct()
8+
public TransactionEntity Construct(Guid entityId)
89
{
910
return new TransactionEntity();
1011
}

0 commit comments

Comments
 (0)