Can a child know it's parent's values?

Can a child know it's parent's values?

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


jender624 posted on Thursday, August 17, 2006

Is it possible for a child (or child collection) to read the values of it's parent's properties directly?  Let's say for example the following set of code is in the child object:

If parentObject.PropertyValue = "someValue" Then...

Is something like this possible?

Thanks,

jender624

vargasbo replied on Thursday, August 17, 2006

Yes, you can, but you'll have to write the code on your own.

In my Unit class, i have a refec to the parent

        [NotUndoable()]
        [NonSerialized()]
        private CustomerMaster customerMaster;
        public CustomerMaster CustomerMaster
        {
            set { customerMaster = value; }
            get { return customerMaster; }
        }

Now, I set the customer on the client side, after the fetch is done.
Regards,
~Bo

malloc1024 replied on Thursday, August 17, 2006

A child should know as little about the parent as possible.  You want to avoid tight coupling between the child and parent.  To avoid this, create an interface that contains the information needed by the child.  Implement this interface in the parent and pass the reference to the child.  This will give you more flexibility and will allow any class that implements the interface to use that child.

DansDreams replied on Friday, August 18, 2006

I've thought and rethought this a few times, and here's my conclusion as of now.

In some cases, the child objects really only exist in the context of a specific parent container, and in that case I think it's ok.  For example, an OrderLineItem may do some calculation based on some value from its parent Order object because it can always count on the Order being the parent container and it's always created by a method that passes a parentOrder parameter.

But as a general rule, an object should not know anything outside its little encapsulated world.

jender624 replied on Friday, August 18, 2006

Sounds like it's not advised, in general.  That's okay; I'll find another way of doing what I'm trying to do.  Thanks for replying!

Copyright (c) Marimer LLC