Skip to content

Commit 46f50e1

Browse files
committed
feat: made internal constructors public, removed usage RelationshipProxy when RelationshipAttribute suffices
1 parent a47f0f3 commit 46f50e1

File tree

9 files changed

+50
-44
lines changed

9 files changed

+50
-44
lines changed

src/JsonApiDotNetCore/Hooks/Execution/AffectedRelationships.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public interface IAffectedRelationships<TDependent> : IAffectedRelationships whe
3030

3131
public class AffectedRelationships<TDependent> : IAffectedRelationships<TDependent> where TDependent : class, IIdentifiable
3232
{
33-
private readonly Dictionary<RelationshipProxy, HashSet<TDependent>> _groups;
33+
private readonly Dictionary<RelationshipAttribute, HashSet<TDependent>> _groups;
3434

3535
public Dictionary<RelationshipAttribute, HashSet<TDependent>> AllByRelationships()
3636
{
37-
return _groups?.ToDictionary(p => p.Key.Attribute, p => p.Value);
37+
return _groups;
3838
}
39-
internal AffectedRelationships(Dictionary<RelationshipProxy, IEnumerable> relationships)
39+
public AffectedRelationships(Dictionary<RelationshipAttribute, IEnumerable> relationships)
4040
{
4141
_groups = relationships.ToDictionary(kvp => kvp.Key, kvp => new HashSet<TDependent>((IEnumerable<TDependent>)kvp.Value));
4242
}
@@ -48,7 +48,7 @@ public Dictionary<RelationshipAttribute, HashSet<TDependent>> GetByRelationship<
4848

4949
public Dictionary<RelationshipAttribute, HashSet<TDependent>> GetByRelationship(Type principalType)
5050
{
51-
return _groups?.Where(p => p.Key.PrincipalType == principalType).ToDictionary(p => p.Key.Attribute, p => p.Value);
51+
return _groups?.Where(p => p.Key.PrincipalType == principalType).ToDictionary(p => p.Key, p => p.Value);
5252
}
5353
}
5454
}

src/JsonApiDotNetCore/Hooks/Execution/AffectedResourceDiff.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class ResourceDiff<TEntity> : AffectedResources<TEntity>, IAffectedResou
3131
/// </summary>
3232
public HashSet<TEntity> DatabaseValues { get => _databaseValues ?? ThrowNoDbValuesError(); }
3333

34-
internal ResourceDiff(IEnumerable requestEntities,
34+
public ResourceDiff(IEnumerable requestEntities,
3535
IEnumerable databaseEntities,
36-
Dictionary<RelationshipProxy, IEnumerable> relationships) : base(requestEntities, relationships)
36+
Dictionary<RelationshipAttribute, IEnumerable> relationships) : base(requestEntities, relationships)
3737
{
3838
_databaseValues = (HashSet<TEntity>)databaseEntities;
3939
_databaseValuesLoaded |= _databaseValues != null;

src/JsonApiDotNetCore/Hooks/Execution/AffectedResources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AffectedResources<TEntity> : AffectedRelationships<TEntity>, IAffec
1919
public HashSet<TEntity> Entities { get; }
2020

2121
internal AffectedResources(IEnumerable entities,
22-
Dictionary<RelationshipProxy, IEnumerable> relationships) : base(relationships)
22+
Dictionary<RelationshipAttribute, IEnumerable> relationships) : base(relationships)
2323
{
2424
Entities = new HashSet<TEntity>(entities.Cast<TEntity>());
2525
}

src/JsonApiDotNetCore/Hooks/Execution/HookExecutorHelper.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ internal class HookExecutorHelper : IHookExecutorHelper
2424
protected readonly Dictionary<DependentType, IHooksDiscovery> _hookDiscoveries;
2525
protected readonly List<ResourceHook> _targetedHooksForRelatedEntities;
2626
protected readonly IJsonApiContext _context;
27-
protected Dictionary<PrincipalType, List<RelationshipProxy>> _meta;
2827

2928
public HookExecutorHelper(
3029
IGenericProcessorFactory genericProcessorFactory,
@@ -35,7 +34,6 @@ IJsonApiContext context
3534
_genericProcessorFactory = genericProcessorFactory;
3635
_graph = graph;
3736
_context = context;
38-
_meta = new Dictionary<DependentType, List<RelationshipProxy>>();
3937
_hookContainers = new Dictionary<DependentType, IResourceHookContainer>();
4038
_hookDiscoveries = new Dictionary<DependentType, IHooksDiscovery>();
4139
_targetedHooksForRelatedEntities = new List<ResourceHook>();
@@ -81,9 +79,9 @@ public IResourceHookContainer<TEntity> GetResourceHookContainer<TEntity>(Resourc
8179
return (IResourceHookContainer<TEntity>)GetResourceHookContainer(typeof(TEntity), hook);
8280
}
8381

84-
public IEnumerable LoadDbValues(PrincipalType entityTypeForRepository, IEnumerable entities, ResourceHook hook, params RelationshipProxy[] relationships)
82+
public IEnumerable LoadDbValues(PrincipalType entityTypeForRepository, IEnumerable entities, ResourceHook hook, params RelationshipAttribute[] relationships)
8583
{
86-
var paths = relationships.Select(p => p.Attribute.RelationshipPath).ToArray();
84+
var paths = relationships.Select(p => p.RelationshipPath).ToArray();
8785
var idType = GetIdentifierType(entityTypeForRepository);
8886
var parameterizedGetWhere = GetType()
8987
.GetMethod(nameof(GetWhereAndInclude), BindingFlags.NonPublic | BindingFlags.Instance)
@@ -95,7 +93,7 @@ public IEnumerable LoadDbValues(PrincipalType entityTypeForRepository, IEnumerab
9593
return (IEnumerable)Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(entityTypeForRepository), values.Cast(entityTypeForRepository));
9694
}
9795

98-
public HashSet<TEntity> LoadDbValues<TEntity>(IEnumerable<TEntity> entities, ResourceHook hook, params RelationshipProxy[] relationships) where TEntity : class, IIdentifiable
96+
public HashSet<TEntity> LoadDbValues<TEntity>(IEnumerable<TEntity> entities, ResourceHook hook, params RelationshipAttribute[] relationships) where TEntity : class, IIdentifiable
9997
{
10098
var entityType = typeof(TEntity);
10199
var dbValues = LoadDbValues(entityType, entities, hook, relationships)?.Cast<TEntity>();
@@ -168,11 +166,11 @@ IEntityReadRepository<TEntity, TId> GetRepository<TEntity, TId>() where TEntity
168166
}
169167

170168

171-
public Dictionary<RelationshipProxy, IEnumerable> LoadImplicitlyAffected(
172-
Dictionary<RelationshipProxy, IEnumerable> principalEntitiesByRelation,
169+
public Dictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(
170+
Dictionary<RelationshipAttribute, IEnumerable> principalEntitiesByRelation,
173171
IEnumerable existingDependentEntities = null)
174172
{
175-
var implicitlyAffected = new Dictionary<RelationshipProxy, IEnumerable>();
173+
var implicitlyAffected = new Dictionary<RelationshipAttribute, IEnumerable>();
176174
foreach (var kvp in principalEntitiesByRelation)
177175
{
178176
if (IsHasManyThrough(kvp, out var principals, out var relationship)) continue;
@@ -212,13 +210,13 @@ public Dictionary<RelationshipProxy, IEnumerable> LoadImplicitlyAffected(
212210

213211
}
214212

215-
bool IsHasManyThrough(KeyValuePair<RelationshipProxy, IEnumerable> kvp,
213+
bool IsHasManyThrough(KeyValuePair<RelationshipAttribute, IEnumerable> kvp,
216214
out IEnumerable entities,
217-
out RelationshipProxy proxy)
215+
out RelationshipAttribute attr)
218216
{
219-
proxy = kvp.Key;
217+
attr = kvp.Key;
220218
entities = (kvp.Value);
221-
return (kvp.Key.Attribute is HasManyThroughAttribute);
219+
return (kvp.Key is HasManyThroughAttribute);
222220
}
223221
}
224222
}

src/JsonApiDotNetCore/Hooks/Execution/IHookExecutorHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ internal interface IHookExecutorHelper
3737
/// Load the implicitly affected entities from the database for a given set of target target entities and involved relationships
3838
/// </summary>
3939
/// <returns>The implicitly affected entities by relationship</returns>
40-
Dictionary<RelationshipProxy, IEnumerable> LoadImplicitlyAffected(Dictionary<RelationshipProxy, IEnumerable> principalEntities, IEnumerable existingDependentEntities = null);
40+
Dictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(Dictionary<RelationshipAttribute, IEnumerable> principalEntities, IEnumerable existingDependentEntities = null);
4141

4242
/// <summary>
4343
/// For a set of entities, loads current values from the database
4444
/// </summary>
45-
IEnumerable LoadDbValues(Type repositoryEntityType, IEnumerable entities, ResourceHook hook, params RelationshipProxy[] relationships);
45+
IEnumerable LoadDbValues(Type repositoryEntityType, IEnumerable entities, ResourceHook hook, params RelationshipAttribute[] relationships);
4646
bool ShouldLoadDbValues(Type containerEntityType, ResourceHook hook);
4747
}
4848
}

src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public virtual IEnumerable<TEntity> BeforeUpdate<TEntity>(IEnumerable<TEntity> e
4747
{
4848
if (GetHook(ResourceHook.BeforeUpdate, entities, out var container, out var node))
4949
{
50-
var dbValues = LoadDbValues(typeof(TEntity), (IEnumerable<TEntity>)node.UniqueEntities, ResourceHook.BeforeUpdate, node.RelationshipsToNextLayer);
50+
var relationships = node.RelationshipsToNextLayer.Select(p => p.Attribute).ToArray();
51+
var dbValues = LoadDbValues(typeof(TEntity), (IEnumerable<TEntity>)node.UniqueEntities, ResourceHook.BeforeUpdate, relationships);
5152
var diff = new ResourceDiff<TEntity>(node.UniqueEntities, dbValues, node.PrincipalsToNextLayer());
5253
IEnumerable<TEntity> updated = container.BeforeUpdate(diff, pipeline);
5354
node.UpdateUnique(updated);
@@ -79,7 +80,8 @@ public virtual IEnumerable<TEntity> BeforeDelete<TEntity>(IEnumerable<TEntity> e
7980
{
8081
if (GetHook(ResourceHook.BeforeDelete, entities, out var container, out var node))
8182
{
82-
var targetEntities = LoadDbValues(typeof(TEntity), (IEnumerable<TEntity>)node.UniqueEntities, ResourceHook.BeforeDelete, node.RelationshipsToNextLayer) ?? node.UniqueEntities;
83+
var relationships = node.RelationshipsToNextLayer.Select(p => p.Attribute).ToArray();
84+
var targetEntities = LoadDbValues(typeof(TEntity), (IEnumerable<TEntity>)node.UniqueEntities, ResourceHook.BeforeDelete, relationships) ?? node.UniqueEntities;
8385
var affected = new AffectedResources<TEntity>(targetEntities, node.PrincipalsToNextLayer());
8486

8587
IEnumerable<TEntity> updated = container.BeforeDelete(affected, pipeline);
@@ -251,7 +253,8 @@ void FireNestedBeforeUpdateHooks(ResourcePipeline pipeline, EntityChildLayer lay
251253
{
252254
if (uniqueEntities.Cast<IIdentifiable>().Any())
253255
{
254-
var dbValues = LoadDbValues(entityType, uniqueEntities, ResourceHook.BeforeUpdateRelationship, node.RelationshipsToNextLayer);
256+
var relationships = node.RelationshipsToNextLayer.Select(p => p.Attribute).ToArray();
257+
var dbValues = LoadDbValues(entityType, uniqueEntities, ResourceHook.BeforeUpdateRelationship, relationships);
255258
var resourcesByRelationship = CreateRelationshipHelper(entityType, node.RelationshipsFromPreviousLayer.GetDependentEntities(), dbValues);
256259
var allowedIds = CallHook(nestedHookcontainer, ResourceHook.BeforeUpdateRelationship, new object[] { GetIds(uniqueEntities), resourcesByRelationship, pipeline }).Cast<string>();
257260
var updated = GetAllowedEntities(uniqueEntities, allowedIds);
@@ -281,7 +284,7 @@ void FireNestedBeforeUpdateHooks(ResourcePipeline pipeline, EntityChildLayer lay
281284
/// Given a source of entities, gets the implicitly affected entities
282285
/// from the database and calls the BeforeImplicitUpdateRelationship hook.
283286
/// </summary>
284-
void FireForAffectedImplicits(Type entityTypeToInclude, Dictionary<RelationshipProxy, IEnumerable> implicitsTarget, ResourcePipeline pipeline, IEnumerable existingImplicitEntities = null)
287+
void FireForAffectedImplicits(Type entityTypeToInclude, Dictionary<RelationshipAttribute, IEnumerable> implicitsTarget, ResourcePipeline pipeline, IEnumerable existingImplicitEntities = null)
285288
{
286289
var container = _executorHelper.GetResourceHookContainer(entityTypeToInclude, ResourceHook.BeforeImplicitUpdateRelationship);
287290
if (container == null) return;
@@ -310,10 +313,10 @@ void ValidateHookResponse<T>(IEnumerable<T> returnedList, ResourcePipeline pipel
310313
/// NOTE: in JADNC usage, the root layer is ALWAYS homogenous, so we can be sure that for every
311314
/// relationship to the previous layer, the principal type is the same.
312315
/// </summary>
313-
(Dictionary<RelationshipProxy, IEnumerable>, PrincipalType) GetDependentImplicitsTargets(Dictionary<RelationshipProxy, IEnumerable> dependentEntities)
316+
(Dictionary<RelationshipAttribute, IEnumerable>, PrincipalType) GetDependentImplicitsTargets(Dictionary<RelationshipAttribute, IEnumerable> dependentEntities)
314317
{
315318
PrincipalType principalType = dependentEntities.First().Key.PrincipalType;
316-
var byInverseRelationship = dependentEntities.Where(kvp => kvp.Key.Attribute.InverseNavigation != null).ToDictionary(kvp => GetInverseRelationship(kvp.Key), kvp => kvp.Value);
319+
var byInverseRelationship = dependentEntities.Where(kvp => kvp.Key.InverseNavigation != null).ToDictionary(kvp => GetInverseRelationship(kvp.Key), kvp => kvp.Value);
317320
return (byInverseRelationship, principalType);
318321

319322
}
@@ -350,17 +353,17 @@ object ThrowJsonApiExceptionOnError(Func<object> action)
350353
/// If <paramref name="dbValues"/> are included, the values of the entries in <paramref name="prevLayerRelationships"/> need to be replaced with these values.
351354
/// </summary>
352355
/// <returns>The relationship helper.</returns>
353-
IAffectedRelationships CreateRelationshipHelper(DependentType entityType, Dictionary<RelationshipProxy, IEnumerable> prevLayerRelationships, IEnumerable dbValues = null)
356+
IAffectedRelationships CreateRelationshipHelper(DependentType entityType, Dictionary<RelationshipAttribute, IEnumerable> prevLayerRelationships, IEnumerable dbValues = null)
354357
{
355358
if (dbValues != null) ReplaceWithDbValues(prevLayerRelationships, dbValues.Cast<IIdentifiable>());
356-
return (IAffectedRelationships)TypeHelper.CreateInstanceOfOpenType(typeof(AffectedRelationships<>), entityType, true, prevLayerRelationships);
359+
return (IAffectedRelationships)TypeHelper.CreateInstanceOfOpenType(typeof(AffectedRelationships<>), entityType, prevLayerRelationships);
357360
}
358361

359362
/// <summary>
360363
/// Replaces the entities in the values of the prevLayerRelationships dictionary
361364
/// with the corresponding entities loaded from the db.
362365
/// </summary>
363-
void ReplaceWithDbValues(Dictionary<RelationshipProxy, IEnumerable> prevLayerRelationships, IEnumerable<IIdentifiable> dbValues)
366+
void ReplaceWithDbValues(Dictionary<RelationshipAttribute, IEnumerable> prevLayerRelationships, IEnumerable<IIdentifiable> dbValues)
364367
{
365368
foreach (var key in prevLayerRelationships.Keys.ToList())
366369
{
@@ -379,14 +382,14 @@ HashSet<IIdentifiable> GetAllowedEntities(IEnumerable source, IEnumerable<string
379382
}
380383

381384
/// <summary>
382-
/// Gets the inverse <see cref="RelationshipProxy"/> for <paramref name="proxy"/>
385+
/// Gets the inverse <see cref="RelationshipAttribute"/> for <paramref name="attribute"/>
383386
/// </summary>
384-
RelationshipProxy GetInverseRelationship(RelationshipProxy proxy)
387+
RelationshipAttribute GetInverseRelationship(RelationshipAttribute attribute)
385388
{
386-
return new RelationshipProxy(_graph.GetInverseRelationship(proxy.Attribute), proxy.PrincipalType, false);
389+
return _graph.GetInverseRelationship(attribute);
387390
}
388391

389-
IEnumerable LoadDbValues(Type containerEntityType, IEnumerable uniqueEntities, ResourceHook targetHook, RelationshipProxy[] relationshipsToNextLayer)
392+
IEnumerable LoadDbValues(Type containerEntityType, IEnumerable uniqueEntities, ResourceHook targetHook, RelationshipAttribute[] relationshipsToNextLayer)
390393
{
391394
if (!_executorHelper.ShouldLoadDbValues(containerEntityType, targetHook)) return null;
392395
return _executorHelper.LoadDbValues(containerEntityType, uniqueEntities, targetHook, relationshipsToNextLayer);

src/JsonApiDotNetCore/Hooks/Traversal/RelationshipsFromPreviousLayer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ internal interface IRelationshipsFromPreviousLayer
1414
/// Grouped by relationship to the previous layer, gets all the entities of the current layer
1515
/// </summary>
1616
/// <returns>The dependent entities.</returns>
17-
Dictionary<RelationshipProxy, IEnumerable> GetDependentEntities();
17+
Dictionary<RelationshipAttribute, IEnumerable> GetDependentEntities();
1818
/// <summary>
1919
/// Grouped by relationship to the previous layer, gets all the entities of the previous layer
2020
/// </summary>
2121
/// <returns>The dependent entities.</returns>
22-
Dictionary<RelationshipProxy, IEnumerable> GetPrincipalEntities();
22+
Dictionary<RelationshipAttribute, IEnumerable> GetPrincipalEntities();
2323
}
2424

2525
internal class RelationshipsFromPreviousLayer<TDependent> : IRelationshipsFromPreviousLayer, IEnumerable<RelationshipGroup<TDependent>> where TDependent : class, IIdentifiable
@@ -31,14 +31,14 @@ public RelationshipsFromPreviousLayer(IEnumerable<RelationshipGroup<TDependent>>
3131
_collection = collection;
3232
}
3333

34-
public Dictionary<RelationshipProxy, IEnumerable> GetDependentEntities()
34+
public Dictionary<RelationshipAttribute, IEnumerable> GetDependentEntities()
3535
{
36-
return _collection.ToDictionary(rg => rg.Proxy, rg => (IEnumerable)rg.DependentEntities);
36+
return _collection.ToDictionary(rg => rg.Proxy.Attribute, rg => (IEnumerable)rg.DependentEntities);
3737
}
3838

39-
public Dictionary<RelationshipProxy, IEnumerable> GetPrincipalEntities()
39+
public Dictionary<RelationshipAttribute, IEnumerable> GetPrincipalEntities()
4040
{
41-
return _collection.ToDictionary(rg => rg.Proxy, rg => (IEnumerable)rg.PrincipalEntities);
41+
return _collection.ToDictionary(rg => rg.Proxy.Attribute, rg => (IEnumerable)rg.PrincipalEntities);
4242
}
4343

4444
public IEnumerator<RelationshipGroup<TDependent>> GetEnumerator()

src/JsonApiDotNetCore/Hooks/Traversal/RootNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ internal class RootNode<TEntity> : IEntityNode where TEntity : class, IIdentifia
1717
public Type EntityType { get; internal set; }
1818
public IEnumerable UniqueEntities { get { return _uniqueEntities; } }
1919
public RelationshipProxy[] RelationshipsToNextLayer { get; private set; }
20-
public Dictionary<Type, Dictionary<RelationshipProxy, IEnumerable>> PrincipalsToNextLayerByType()
20+
public Dictionary<Type, Dictionary<RelationshipAttribute, IEnumerable>> PrincipalsToNextLayerByType()
2121
{
2222
return RelationshipsToNextLayer
2323
.GroupBy(proxy => proxy.DependentType)
24-
.ToDictionary(gdc => gdc.Key, gdc => gdc.ToDictionary(p => p, p => UniqueEntities));
24+
.ToDictionary(gdc => gdc.Key, gdc => gdc.ToDictionary(p => p.Attribute, p => UniqueEntities));
2525
}
2626

2727
/// <summary>
2828
/// The current layer entities grouped by affected relationship to the next layer
2929
/// </summary>
30-
public Dictionary<RelationshipProxy, IEnumerable> PrincipalsToNextLayer()
30+
public Dictionary<RelationshipAttribute, IEnumerable> PrincipalsToNextLayer()
3131
{
32-
return RelationshipsToNextLayer.ToDictionary(p => p, p => UniqueEntities);
32+
return RelationshipsToNextLayer.ToDictionary(p => p.Attribute, p => UniqueEntities);
3333
}
3434

3535
/// <summary>

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public interface IResourceGraph
6969
/// </summary>
7070
/// <param name="internalAttributeName">The internal attribute name for a <see cref="AttrAttribute" />.</param>
7171
string GetPublicAttributeName<TParent>(string internalAttributeName);
72+
73+
/// <summary>
74+
/// Helper method to get the inverse relationship attribute corresponding
75+
/// to a relationship.
76+
/// </summary>
7277
RelationshipAttribute GetInverseRelationship(RelationshipAttribute relationship);
7378

7479
/// <summary>

0 commit comments

Comments
 (0)