Need to load data into a ERL but save each child independently

Need to load data into a ERL but save each child independently

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


j055 posted on Thursday, November 12, 2009

Hi

I'm not sure of the best way to go about this.

I want to load data into a collection but do a child.Save() on each child item.

I want to be able to save child items which are valid even if the ERL is not valid.

The other thing is, if I add new items to the collection I don't want the validation to take place until the item is about to be saved. Otherwise the child could be valid in the collection but fail when added to the database because inserts or deletes have happened since the collection was loaded.

Is there a standard pattern I can adopt to allow this functionallity with CSLA?

Many thanks
Andrew

rsbaker0 replied on Thursday, November 12, 2009

It is neither a standard pattern nor the intended use of the class, but I have used EditableRootListBase (ERLB) in modified form to solve similar problems.

With ERLB, it's up to you to do any saving of the items in the list (but note that you save by index, not by calling save on the item).

Since ERLB itself has no concept of itself as a whole being valid or invalid (nor even a built-it Save method for the entire list), you are free to implement these to your liking. (And you don't even have to post-pone validation -- the ERLB won't care unless you actually try to save the broken item).

There are some wrinkles in this -- ERLB isn't wired up to be a child of another object, but this isn't to hard to work around.

However, if you do postpone validation like you suggested, I'd think the standard BusinessListBase would also work for you -- you get to implement your own Save() method for this class also and don't have to use the child data portal.

j055 replied on Thursday, November 12, 2009

Ah, not sure I understand you fully but reading between the lines perhaps this would work?

I load the data into the BusinessListBase with one db call. The child object uses:

Child_Fetch and DataPortal_Insert, DataPortal_Update methods to I can call the save in each child one at a time from the instances created by the list.

Hmm, means the root.Save() won't work as expected, but maybe that's ok. Or what about using a ReadOnlyList and BusinessBase children?

Any ideas?

rsbaker0 replied on Thursday, November 12, 2009

It would be helpful to know a little more about how you want the user interface to react to invalid items in the list. You won't (easily) be able to call Save() on an object that is or contains a BusinessListBase that has any broken objects in the collection, so this may be a factor.

That's why I coerced a class derived from EditableRootListBase into doing this. The parent object won't check it for broken items -- so you can implement this yourself as you see fit. It's also pretty easy to make a pass over the list and just save the valid items, and do whatever you want with the broken ones.

Even if the "parent" object is itself broken, you could still make direct save calls onto the ERLB "child". You won't be able to do this with a BLB.

I'm not sure how you would use a ReadOnlyList, since many UI components would not allow you to edit the corresponding properties.

Just to be clear, my suggested approach could certainly be considered "CSLA abuse", but sometimes you're dealing with a use case that doesn't fit the "all or nothing" persistence model in CSLA well.

RockfordLhotka replied on Friday, November 13, 2009

If you can save an object independently, then it isn't a child object, it is a root object, by the very definition of these terms within CSLA parlance.

The EditableRootListBase (ERLB) class is a collection specifically designed to support the idea of loading a collection of objects, and then saving changes to each object independently. It is a collection of root objects, instead of a collection of child objects.

In the book and elsewhere you'll find this referred to as a dynamic list.

RockfordLhotka replied on Friday, November 13, 2009

One other thing to consider is the use of a diffgram or datagram.

There's a sample called Csla.DiffGram in the .NET samples download that illustrates a technique for extending BLB to use a diffgram to only save items in the list that have been changed.

I wrote this as an example of how to deal with large collections where few rows change, but it might give you some ideas as well.

Copyright (c) Marimer LLC