Skip to content

Commit d1f39e6

Browse files
committed
comments: added comment to assign method
1 parent 4cd9465 commit d1f39e6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ public void DetachRelationshipPointers(TEntity entity)
215215
}
216216
}
217217

218-
219-
220-
221218
/// <inheritdoc />
222219
public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
223220
{
@@ -247,12 +244,12 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
247244
{
248245
// load relations to enforce complete replace
249246
await _context.Entry(oldEntity).Collection(hasManyAttribute.InternalRelationshipName).LoadAsync();
250-
// also need to load inverse relationship here, see issue #502
247+
// todo: need to load inverse relationship here, see issue #502
251248
hasManyAttribute.SetValue(oldEntity, relationshipValue);
252249
}
253250
else if (relationshipEntry.Key is HasOneAttribute hasOneAttribute)
254251
{
255-
// need to load inverse relationship here, see issue #502
252+
// todo: need to load inverse relationship here, see issue #502
256253
hasOneAttribute.SetValue(oldEntity, relationshipValue);
257254
}
258255
}
@@ -261,22 +258,6 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
261258
return oldEntity;
262259
}
263260

264-
private void AssignHasManyThrough(TEntity entity, HasManyThroughAttribute hasManyThrough, IList relationshipValue)
265-
{
266-
var pointers = relationshipValue.Cast<IIdentifiable>();
267-
var throughRelationshipCollection = Activator.CreateInstance(hasManyThrough.ThroughProperty.PropertyType) as IList;
268-
hasManyThrough.ThroughProperty.SetValue(entity, throughRelationshipCollection);
269-
270-
foreach (var pointer in pointers)
271-
{
272-
var throughInstance = Activator.CreateInstance(hasManyThrough.ThroughType);
273-
hasManyThrough.LeftProperty.SetValue(throughInstance, entity);
274-
hasManyThrough.RightProperty.SetValue(throughInstance, pointer);
275-
throughRelationshipCollection.Add(throughInstance);
276-
}
277-
}
278-
279-
280261
/// <inheritdoc />
281262
public async Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable<string> relationshipIds)
282263
{
@@ -422,7 +403,26 @@ private void AttachHasManyAndHasManyThroughPointers(TEntity entity)
422403
}
423404
}
424405

406+
/// <summary>
407+
/// The relationshipValue parameter contains the dependent side of the relationship (Tags).
408+
/// We can't directly add them to the principal entity (Article): we need to
409+
/// use the join table (ArticleTags). This methods assigns the relationship value to entity
410+
/// by taking care of that
411+
/// </summary>
412+
protected void AssignHasManyThrough(TEntity entity, HasManyThroughAttribute hasManyThrough, IList relationshipValue)
413+
{
414+
var pointers = relationshipValue.Cast<IIdentifiable>();
415+
var throughRelationshipCollection = Activator.CreateInstance(hasManyThrough.ThroughProperty.PropertyType) as IList;
416+
hasManyThrough.ThroughProperty.SetValue(entity, throughRelationshipCollection);
425417

418+
foreach (var pointer in pointers)
419+
{
420+
var throughInstance = Activator.CreateInstance(hasManyThrough.ThroughType);
421+
hasManyThrough.LeftProperty.SetValue(throughInstance, entity);
422+
hasManyThrough.RightProperty.SetValue(throughInstance, pointer);
423+
throughRelationshipCollection.Add(throughInstance);
424+
}
425+
}
426426

427427
/// <summary>
428428
/// This is used to allow creation of HasOne relationships when the

0 commit comments

Comments
 (0)