I have an entity A which has three children entities X, Y and Z.
I have also a generic repository one of those floating around the web...
In my Service method AXYZ()
I do multiple repositories calls: (pseudo code)
repoA.Update(objectA);
repoY.Delete(objectA.PK_Id);
foreach:
objectX.FK_ID = objectA.PK_ID;
repoX.Add(objectX);
foreach:
objectY.FK_ID = objectA.PK_ID;
repoY.Add(objectY);
foreach:
objectZ.FK_ID = objectA.PK_ID;
repoZ.Add(objectZ);
_unitOfWork.Commit();
Now I asked myself, should it not be enough or better to do all foreach.Add operations not on the repoXXX just instead do the Add operation on the children collections?:
repoA.Update(objectA);
repoY.Delete(objectA.Id);
foreach:
objectA.CollectionX.Add(objectX);
objectA.CollectionY.Add(objectY);
objectA.CollectionZ.Add(objectZ);
_unitOfWork.Commit();
A sidenote:
The Delete operation on repoY could not be done inmemory as the objects_Y are not loaded into the context.
What do you think?
I tagged this question also with domain driven design maybe this scenario has something to do with it?