c# - Parent child loop, design pattern -
Assume that I have two squares (using C #).
Class A and B can be converted into Class AB. B is the parent (type B) of a property in class. I have an example which I can call list & lt; B & gt;
wants to change to a list & lt; A & gt;
In Class A, there is information to know which parent is the example of B.
Suppose I have a loop trough my list & lt; A & gt;
to change it
for (int i = 0; i
The method ConvertToB creates a b example and if the parent is already in, then listOfB searches. So we say that our convertor tab looks like this:
Public B ConvertTob (AA, List & Lt; B & gt; listOfB) {B newInstanceOfB = A to B; Bipart = List of B First and default (x = & gt; x.Id == a.ParentId); }
If this has not been converted yet, then this code will be incorrect. If we change the listofA [1] then F.X, this will be the list of related parents B [6] We will not be available at the time when we want to change the listofA [1].
My question is how will this problem be resolved in a clean object oriented manner.
Edit:
A is a feature with some properties that is also in B. A and B are not related to B. The list of children allowed in B can also be. This list & lt; B & gt;
contains a list that is part of listofB list.
Then f.ex: listofA [0] will be changed to [B] in the listing [0]. List [B] [0] has a list of parents [4] and allowed children {listofB [7], listofB [8]}
Sorry to not mention it before.
Do you really need a design pattern? You can do just 2 loops, first of all the B examples will be born and the second will find and set the parents. You can also use LINQ bits.
var listOfB = listOfA.Cast & lt; B & gt; (). ToList () foeach (listOfB in var b) {b.Parent = listOfB.FirstOrDefault (x = & gt; x.Id == b.ParentId); // This will work because there is a clear legacy, B has all the properties A)
Comments
Post a Comment