Child business object reference to parent/grandparent business object

Child business object reference to parent/grandparent business object

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


bniemyjski posted on Wednesday, July 14, 2010

Hello,

I spent about two hours looking into this, and I couldn't find a decent solution. I did come across these three posts:

http://forums.lhotka.net/forums/p/6210/43595.aspx
http://forums.lhotka.net/forums/t/5131.aspx
http://forums.lhotka.net/forums/p/2672/13793.aspx

There seems to be a lot of work you need to do to enable this. And it seems like it should be simple and handled by CSLA.

I took a look and I think the best spot to handle setting this would be in the Lists AddNewCore.

 protected override object AddNewCore()
        {
            Admission item = CSLATest.Business.Admission.NewAdmission();

            bool cancel = false;
            OnAddNewCore(ref item, ref cancel);
            if (!cancel)
            {
                // Check to see if someone set the item to null in the OnAddNewCore.
                if(item == null)
                    item = CSLATest.Business.Admission.NewAdmission();

                // Pass the parent value down to the child.
                Patient patient = this.Parent as Patient;
                if (patient != null)
                    item.PatientMember = patient;

                Add(item);
            }

            return item;
        }

You would also need to think about what happens if you are not using a List... In the scenario above, I had set a setter on the collection like so:

            internal set
            {
                PatientID = value.PatientID;
                LoadProperty(_patientMemberProperty, value);
            }

I really don't like this setup and I'm not happy with it. Apart from this, it leads you into a StackOverflow when you try to call Save(). If someone can look into this and supply a working sample in code that handles serialization, children and children collections. I would have no problem opening this back up and implementing.

For more information with a test solution and database, please take a look at the Google code issue here.

Thanks
-Blake Niemyjski

RockfordLhotka replied on Wednesday, July 14, 2010

In CSLA 4 the Parent properties are public on the base types, so a child shouldn't need to maintain a reference to objects higher in the graph - it can just walk up the graph with this.Parent.Parent.Parent or whatever.

bniemyjski replied on Wednesday, July 14, 2010

Hello,

Yeah, I looked into this, the problem we are having is that we have a child object (parent -> list -> grand child -> Child property or Parent -> child -> child property that could be pointing back to the parent via a fk relationship etc... We want to set this to have a reference back to the parent.

Thanks

-Blake Niemyjski

Copyright (c) Marimer LLC