Skip to content

Commit 3b5a12c

Browse files
committed
chore: remove logic related to ER splitting
1 parent e4a4d9c commit 3b5a12c

File tree

3 files changed

+51
-172
lines changed

3 files changed

+51
-172
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 18 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,14 @@ private void LoadInverseRelationships(object trackedRelationshipValue, Relations
171171
{
172172
var relationEntry = _context.Entry((IIdentifiable)trackedRelationshipValue);
173173
if (IsHasOneRelationship(hasOneAttr.InverseNavigation, trackedRelationshipValue.GetType()))
174-
{
175174
relationEntry.Reference(hasOneAttr.InverseNavigation).Load();
176-
}
177175
else
178-
{
179176
relationEntry.Collection(hasOneAttr.InverseNavigation).Load();
180-
}
181177
}
182178
else if (relationshipAttr is HasManyAttribute hasManyAttr && !(relationshipAttr is HasManyThroughAttribute))
183179
{
184180
foreach (IIdentifiable relationshipValue in (IList)trackedRelationshipValue)
185-
{
186181
_context.Entry(relationshipValue).Reference(hasManyAttr.InverseNavigation).Load();
187-
}
188182
}
189183
}
190184

@@ -193,15 +187,14 @@ private bool IsHasOneRelationship(string internalRelationshipName, Type type)
193187
var relationshipAttr = _resourceGraph.GetContextEntity(type).Relationships.SingleOrDefault(r => r.InternalRelationshipName == internalRelationshipName);
194188
if (relationshipAttr != null)
195189
{
196-
if (relationshipAttr is HasOneAttribute) return true;
190+
if (relationshipAttr is HasOneAttribute)
191+
return true;
192+
197193
return false;
198194
}
199-
else
200-
{
201-
// relationshipAttr is null when we don't put a [RelationshipAttribute] on the inverse navigation property.
202-
// In this case we use relfection to figure out what kind of relationship is pointing back.
203-
return !(type.GetProperty(internalRelationshipName).PropertyType.Inherits(typeof(IEnumerable)));
204-
}
195+
// relationshipAttr is null when we don't put a [RelationshipAttribute] on the inverse navigation property.
196+
// In this case we use relfection to figure out what kind of relationship is pointing back.
197+
return !(type.GetProperty(internalRelationshipName).PropertyType.Inherits(typeof(IEnumerable)));
205198
}
206199

207200

@@ -213,22 +206,16 @@ public void DetachRelationshipPointers(TEntity entity)
213206
{
214207
if (relationshipAttr is HasOneAttribute hasOneAttr)
215208
{
216-
var relationshipValue = GetEntityResourceSeparationValue(entity, hasOneAttr) ?? (IIdentifiable)hasOneAttr.GetValue(entity);
209+
var relationshipValue = (IIdentifiable)hasOneAttr.GetValue(entity);
217210
if (relationshipValue == null) continue;
218211
_context.Entry(relationshipValue).State = EntityState.Detached;
219212
}
220213
else
221214
{
222215
IEnumerable<IIdentifiable> relationshipValueList = (IEnumerable<IIdentifiable>)relationshipAttr.GetValue(entity);
223-
/// This adds support for resource-entity separation in the case of one-to-many.
224-
/// todo: currently there is no support for many to many relations.
225-
if (relationshipAttr is HasManyAttribute hasMany)
226-
relationshipValueList = GetEntityResourceSeparationValue(entity, hasMany) ?? relationshipValueList;
227216
if (relationshipValueList == null) continue;
228217
foreach (var pointer in relationshipValueList)
229-
{
230218
_context.Entry(pointer).State = EntityState.Detached;
231-
}
232219
/// detaching has many relationships is not sufficient to
233220
/// trigger a full reload of relationships: the navigation
234221
/// property actually needs to be nulled out, otherwise
@@ -286,45 +273,30 @@ private object GetTrackedRelationshipValue(RelationshipAttribute relationshipAtt
286273
wasAlreadyAttached = false;
287274
if (relationshipAttr is HasOneAttribute hasOneAttr)
288275
{
289-
/// This adds support for resource-entity separation in the case of one-to-one.
290-
var relationshipValue = GetEntityResourceSeparationValue(entity, hasOneAttr) ?? (IIdentifiable)hasOneAttr.GetValue(entity);
276+
var relationshipValue = (IIdentifiable)hasOneAttr.GetValue(entity);
291277
if (relationshipValue == null)
292278
return null;
293279
return GetTrackedHasOneRelationshipValue(relationshipValue, hasOneAttr, ref wasAlreadyAttached);
294280
}
295-
else
296-
{
297-
IEnumerable<IIdentifiable> relationshipValueList = (IEnumerable<IIdentifiable>)relationshipAttr.GetValue(entity);
298-
/// This adds support for resource-entity separation in the case of one-to-many.
299-
/// todo: currently there is no support for many to many relations.
300-
if (relationshipAttr is HasManyAttribute hasMany)
301-
relationshipValueList = GetEntityResourceSeparationValue(entity, hasMany) ?? relationshipValueList;
302-
if (relationshipValueList == null) return null;
303-
return GetTrackedManyRelationshipValue(relationshipValueList, relationshipAttr, ref wasAlreadyAttached);
304-
}
281+
282+
IEnumerable<IIdentifiable> relationshipValueList = (IEnumerable<IIdentifiable>)relationshipAttr.GetValue(entity);
283+
if (relationshipValueList == null)
284+
return null;
285+
286+
return GetTrackedManyRelationshipValue(relationshipValueList, relationshipAttr, ref wasAlreadyAttached);
305287
}
306288

307-
// helper method used in GetTrackedRelationshipValue. See comments there.
289+
// helper method used in GetTrackedRelationshipValue. See comments below.
308290
private IList GetTrackedManyRelationshipValue(IEnumerable<IIdentifiable> relationshipValueList, RelationshipAttribute relationshipAttr, ref bool wasAlreadyAttached)
309291
{
310292
if (relationshipValueList == null) return null;
311293
bool _wasAlreadyAttached = false;
312-
/// if we're not using entity resource separation, we can just read off the related type
313-
/// from the RelationshipAttribute. If we DO use separation, RelationshipAttribute.DependentType
314-
/// will point to the Resource, not the Entity, which is not the one we need here.
315-
bool entityResourceSeparation = relationshipAttr.EntityPropertyName != null;
316-
Type entityType = entityResourceSeparation ? null : relationshipAttr.DependentType;
317294
var trackedPointerCollection = relationshipValueList.Select(pointer =>
318-
{
319-
/// todo: we can't just use relationshipAttr.DependentType because
320-
/// this will point to the Resource type in the case of entity resource
321-
/// separation. We should consider to store entity type on
322-
/// the relationship attribute too.
323-
entityType = entityType ?? pointer.GetType();
295+
{ // convert each element in the value list to relationshipAttr.DependentType.
324296
var tracked = AttachOrGetTracked(pointer);
325297
if (tracked != null) _wasAlreadyAttached = true;
326-
return Convert.ChangeType(tracked ?? pointer, entityType);
327-
}).ToList().Cast(entityType);
298+
return Convert.ChangeType(tracked ?? pointer, relationshipAttr.DependentType);
299+
}).ToList().Cast(relationshipAttr.DependentType);
328300
if (_wasAlreadyAttached) wasAlreadyAttached = true;
329301
return (IList)trackedPointerCollection;
330302
}
@@ -513,32 +485,6 @@ private void AssignHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan
513485
}
514486
}
515487

516-
/// <summary>
517-
/// A helper method that gets the relationship value in the case of
518-
/// entity resource separation.
519-
/// </summary>
520-
private IIdentifiable GetEntityResourceSeparationValue(TEntity entity, HasOneAttribute attribute)
521-
{
522-
if (attribute.EntityPropertyName == null)
523-
{
524-
return null;
525-
}
526-
return (IIdentifiable)entity.GetType().GetProperty(attribute.EntityPropertyName)?.GetValue(entity);
527-
}
528-
529-
/// <summary>
530-
/// A helper method that gets the relationship value in the case of
531-
/// entity resource separation.
532-
/// </summary>
533-
private IEnumerable<IIdentifiable> GetEntityResourceSeparationValue(TEntity entity, HasManyAttribute attribute)
534-
{
535-
if (attribute.EntityPropertyName == null)
536-
{
537-
return null;
538-
}
539-
return ((IEnumerable)(entity.GetType().GetProperty(attribute.EntityPropertyName)?.GetValue(entity))).Cast<IIdentifiable>();
540-
}
541-
542488
/// <summary>
543489
/// Given a iidentifiable relationshipvalue, verify if an entity of the underlying
544490
/// type with the same ID is already attached to the dbContext, and if so, return it.

src/JsonApiDotNetCore/Models/IResourceMapper.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)