Error in 3 Tier Business List Base Save()

Error in 3 Tier Business List Base Save()

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


Jeff.Bradford posted on Wednesday, October 31, 2007

Hello All,

I couldnt find another post on the forums for this topic, so I apologize in advance if it's been covered. I am having a problem in 3 tier that I am not having in 2 tier when running the exact same code. I am finding that after adding a new item to a BusinessListBase collection and saving, the status of IsNew on the item is not propogating back to the collection from the item. I will add the item to the collection, save the collection, MarkOld() on the item and when control goes back to the collection, the item shows IsNew = true. Take the following code snippets for example:

protected override void DataPortal_Update(){ ... //Update on BusinessListBase Parent Collection

foreach (MyItem child in this)

{ //Stop Point 1, Child.IsNew is true, as it should be

if (child.IsNew)

child.Insert(this);

...

} // At Stop Point 3, Child.IsNew is true as it should NOT be.

}

internal void Insert(MyCollection parent) //Insert on Child - BusinessBase

{

... //Do Inserting Stuff

MarkOld();

MarkClean();

} //At Stop Point 2, this.IsNew is False

 

Again, this happens in 3 tier mode, but not 2 tier. I am currently using CSLA 3.0.1. Any ideas/clarification?

Thanks,

Jeff

Jeff.Bradford replied on Wednesday, October 31, 2007

Edit: Upon closing studio and reopening, therefore resetting the local development server, I can not reproduce the error at stop point 3. It now shows false. However, after passing the object back through the portal, the col[item].IsNew is still true. In stepping through the code, the value in the data portal result remains false. As soon as control returns to the script I called MyCollection.Save() from, MyCol[Item].IsNew is still true. Why would it lose this change from the portal object?

I apologize if I have confused anyone in my descriptions.

Thanks in advance.

 

 

Patrick.Roeper replied on Wednesday, October 31, 2007

Jeff,

I might not understand completely but the scenario you describe sounds similar to one we ran into a few months ago. Our application was running fine in 2 tier mode but when we switched to 3 tier we noticed that whenever we created a new root object and saved it, we saw the changes in the database but the IsNew flag never went to false. So everytime you created a new object, clicked Save on our UI, and then clicked close, you were still prompted with "Would you like to save the new [Root Object]?".

Turns out that we were not Saving the object correctly. Save() returns the updated instance of the root object being saved. When we were calling Save() directly on the business object, we were not updating the original business object to the saved copy that came back from the data portal.

The following code is from chapter 9 of the csla 2.0 book:

Project temp = _project.Clone();

temp.ApplyEdit();

try

{

_project = temp.Save();

....

Once we changed our code over to this we were not seeing the problem anymore. Hopefully what you are dealing with is similar to our previous problem and this will correct the issue.

Jeff.Bradford replied on Thursday, November 01, 2007

Patrick,

Yep, that was it. I was just applying a .Save() to my object rather than cloning it and setting the returned object from the portal to my object. Thanks for helping me with something I should have known all along. :P

Jeff

 

RockfordLhotka replied on Thursday, November 01, 2007

In CSLA 3.0.2 there's a config flag: AutoCloneOnUpdate. If you set this to true, it will cause the object to be cloned when the data portal is in local mode. The result is that you'd see this issue at all times Smile [:)]

This turns out to be a good thing though, partially because it helps you find this sort of bug immediately. But also because you must clone the object if you want to be able to handle any exceptions in the DataPortal_XYZ methods in a stable manner. Specifically, if you want to be able to show the user a working object after such an exception.

I discuss the details of this in the Using CSLA .NET 3.0 ebook.

In CSLA 3.5 I'll be changing the default of AutoCloneOnUpdate to be true. It defaults to false in 3.0.2 for backward compatibility, but it is such an important thing, that I'm changing the default in 3.5 to be what it should be - though you'll still be able to set it to the old value if necessary.

Copyright (c) Marimer LLC