Collection with Parent reference

Collection with Parent reference

Old forum URL: forums.lhotka.net/forums/t/4143.aspx


FreeAsInBeer posted on Thursday, January 10, 2008

Hi all,

I've been afk for about 6 months :), but I'm back with some nooby questions!!!

I have the same situation mentioned here;

http://forums.lhotka.net/forums/permalink/13793/13787/ShowThread.aspx#13787

Root
 contains Col1
   contains ChildA objects

I need Col1 to have a reference to Root.

In Col1 I have a method that does a check like this;
           
if (ChildA.Size > Root.MaximumChildSize) throw SomeException

This is just one example, there are others. This isn't a validation thing, it's a check in an Add/Assign etc.

However, in another post i read this;

One of the basic OOP principles to keep in mind is program to an interface and not an implementation.  A child should not have a reference to its concrete parent.  A child should know as little about its parent as possible.  It is not a good idea for the child to have a reference to its parent because it becomes tightly-coupled with its parent.  This creates a rigid design and does not make your child reusable.  The most common way to solve your problem is to abstract out what your child needs into an interface or abstract class.  The parent should implement this interface and the child should only reference the parent through this interface.

My dilemna - the 'SetParent and [NonSerialized(), NotUndoable()]' way or the 'OOP interface' way.

Does anyone have a working c# sample of the SetParent way that they could please share?

thanks in advance
FiaB

SomeGuy replied on Thursday, January 10, 2008

You could also have root monitor col1 for changes and throw the exception.

 

Copyright (c) Marimer LLC