Hi all

I've recently started to evaluate NHibernate and have a question regarding 
implementation of one-many associations.  Let's say we have

[ParentClass] Parent 1----* Children [ChildClass]

It seems to me that when considering an in-memory model only I would do 
something like this

ParentClass
    //01: Don't allow modification of the Children property
    public ReadOnlyCollection<ChildClass> Children
    {
        get
        {
            return new List<ChildClass>(children).AsReadOnly();
        }
    }

    //Internal method to keep parent in sync with children
    internal void ChildAdded(ChildClass c)
    {
        children.Add(c);
    }

    //Internal method to keep parent in sync with children
    internal void ChildRemoved(ChildClass c)
    {
        children.Remove(c);
    }

ChildClass
    public ParentClass Parent
    {
        get { return parent; }
        set
        {
            //Ensure original parent no longer thinks it owns this child
            if (Parent != null)
                Parent.ChildRemoved(this);

            parent = value;

            //Ensure the new parent knows it does own the child
            if (Pareng != null)
                Parent.ChildAdded(this);
        }
    }


NHibernate In Action suggests the following

    public void AddChild(ChildClass c)
    {
        Children.Add(c);
        c.Parent = this;
    }

Problems I have with this approach are

01: It depends upon the developer always using Parent.AddChild rather than 
Parent.Children.Add or setting Child.Parent.
02: If a Child already belongs to a different Parent and we are changing the 
association then I have a choice between
    A: If the Child's existing parent is already loaded, leaving 
OriginalParent.Children with this Child in the list, or
    B: If I add code to first remove the Child from its existing Parent then 
having to load OriginalParent when it is not needed

The OPF I am used to using will do all of this for me, it will update the 
original Parent.Children *if* it is already loaded, if it is not already 
loaded then there is no need as the association state is stored in the 
child.

I'm a bit concerned that I have to choose between having out of sync data, 
or having to load a Parent instance when it is not needed.


Any recommendations?


Pete
====
http://mrpmorris.blogspot.com



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to