Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have the Martin Fowler's Refactoring book, just follow the "Change Unidirectional Association to Bidirectional" refactoring.</p> <p>In case you don't have it, here's how your classes will look like after the refactoring:</p> <pre><code>class C { // Don't to expose this publicly so that // no one can get behind your back and change // anything private List&lt;W&gt; contentsW; public void Add(W theW) { theW.Container = this; } public void Remove(W theW) { theW.Container = null; } #region Only to be used by W internal void RemoveW(W theW) { // do nothing if C does not contain W if (!contentsW.Contains(theW)) return; // or throw an exception if you consider this illegal contentsW.Remove(theW); } internal void AddW(W theW) { if (!contentW.Contains(theW)) contentsW.Add(theW); } #endregion } class W { private C containerC; public Container Container { get { return containerC; } set { if (containerC != null) containerC.RemoveW(this); containerC = value; if (containerC != null) containerC.AddW(this); } } } </code></pre> <p>Take note that I've made the <code>List&lt;W&gt;</code> private. Expose the list of Ws via an enumerator instead of exposing the list directly.</p> <p>e.g. public List GetWs() { return this.ContentW.ToList(); }</p> <p>The code above handles transfer of ownership properly. Say you have two instances of C -- C1 and C2 - and the instances of W -- W1 and W2.</p> <pre><code>W1.Container = C1; W2.Container = C2; </code></pre> <p>In the code above, C1 contains W1 and C2 contains W2. If you reassign W2 to C1</p> <pre><code>W2.Container = C1; </code></pre> <p>Then C2 will have zero items and C1 will have two items - W1 and W2. You can have a floating W</p> <pre><code>W2.Container = null; </code></pre> <p>In this case, W2 will be removed from C1's list and it will have no container. You can also use the Add and Remove methods from C to manipulate W's containers - so C1.Add(W2) will automatically remove W2 from it's original container and add it to the new one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload