How to update a ROLB?

How to update a ROLB?

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


selim51 posted on Sunday, April 13, 2008

Hello All!

In my database i have a parent table and a child table. Child table has a mandatory foreign key to the parent table. That means a child can't live for its own, it has to be associated with a parent.

So my Child class has an internal static NewChild() factory method. Childs should only created by parents to allocate them to their parent in this way: parent.Childs.NewChild(). This method calls the Child.NewChild() factory method and adds the returned child object into the list.

I wanted to prevent the user to make mistakes like this one:

Child child = aParent.Childs.NewChild();
// change some properties of child
child.Name = "a child name";
anotherParent.Childs.Add(child);
aParent.Save();
anotherParent.Save();

One can argue that last comes wins, but I don't like that. So I decided to make the Childs list a ROLB. This time I can control that the user can't do the mistake. Changing the parent of a child could be achieved in this way:

Child child = aParent.Childs.NewChild();
aParent.Childs.ReallocateChild(child, anotherParent);

Reallocating a child means removing it from one list, adding it to the other list, and changing it's foreign key field.

Now this is where the problem starts. Assume that parent is self not dirty, but some of the childs in the read only list are. parent.IsDirty returns false, because ROLB does not implement ITrackStatus interface, so it is skiped looking wether its items are dirty or not.

Overwriting methods like add and remove and so on of BLB seems for me the wrong way. The only "right way" I see is that parent should have a private BLB which is kept synchronized with ROLB so that in the next save the childs could be saved, too.

Is that the right way? Has anybody another cleaner solution?

Yavuz

ajj3085 replied on Monday, April 14, 2008

If you can add and remove children, ROLB isn't the proper subclass to use.  Use BLB.

selim51 replied on Monday, April 14, 2008

And then overwriting the add method throwing a NotImplementedException as you (I think you was) have suggested in a similar thread?

That's the way I wanted to try next.

Thank you.

Copyright (c) Marimer LLC