Problem about saving properties

Problem about saving properties

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


Tian'en Gu posted on Friday, November 28, 2008

Hi Rockford and other brothers,
i have problem about saving properties.
Several monthes ago i use CSLA 3.0.4 to create a business class, I write the following code in DataPortal_Insert:

            RecipeEntity recipeEntity = new RecipeEntity();
            recipeEntity.RecipeGUID = this._recipeGUID;
            recipeEntity.Id = this._iD;
            recipeEntity.Name = this._name;  
           this._modifiedBy = ApplicationContext.User.Identity.Name;
            recipeEntity.ModifiedBy = this._modifiedBy;           

            new RecipeDAL().Add(recipeEntity);

            UpdateChildren();
            MarkOld();

and i wrote the following to invoke DataPortal_Insert:
    
            Recipe RecipeNew = Recipe.NewRecipe();
            RecipeNew.ID = 38;
            RecipeNew.Name = "Tax";           
            RecipeNew.Save(); 
I used to assign value to property '_modifiedBy' in method DataPortal_Insert, and after saving the object RecipeNew can get the value of
'_modifiedBy', and i dont need to use the return value of RecipeNew.Save(), because i debug into the source code and find in BusinessBase.Save() that the method pass 'this' into Client.DataPortal.Update(), and the porperty value of 'this' can be changed and saved after invoking Save(), right?

Now I want to use CSLA 3.5.2 to update the original code, and i dont want to change the code invoke DataPortal_Insert, i just use the new LoadProperty and ReadProperty features to make the business code more simple:

           RecipeEntity recipeEntity = new RecipeEntity();
            recipeEntity.RecipeGUID = ReadProperty<Guid>(RecipeGUIDProperty);
            recipeEntity.Id = ReadProperty<int>(IDProperty);
            recipeEntity.Name = ReadProperty<string>(NameProperty);              
            this.ModifiedBy = ApplicationContext.User.Identity.Name;             // this line: problem
            recipeEntity.ModifiedBy = ReadProperty<string>(ModifiedByProperty);

             new RecipeDAL().Add(recipeEntity);

            UpdateChildren();
            MarkOld();

But this time after invoking DataPortal_Insert(), I found the outside object RecipeNew can not keep the value of 'ModifiedByProperty', i also debug in the source code but not found any strange places ('this' is passed into Client.DataPortal.Update as usual), I get confused. I know I can assign the value outside DataPortal_Insert but this is a big project and should try to avoid modifying the code which invoke the BLL, or i will spend plenty of time on it and need to pay much attention on testing work.

So are there any ideas to 'save' me successfully this time?  Any help are appreciated.

 

triplea replied on Friday, November 28, 2008

Just a thought. Is your ModifiedBy definition for correct? E.g. in the get/set you are not setting the value to the wrong property?

Also, I would recommend instead of:

this.ModifiedBy = ApplicationContext.User.Identity.Name;

to do this:

LoadProperty(ModifiedByProperty, ApplicationContext.User.Identity.Name);

JoeFallon1 replied on Saturday, November 29, 2008

This code is almost always wrong:

RecipeNew.Save(); 

It should almost always be:

myBo = RecipeNew.Save(); 

About the only time you can "just call .Save"  is if you immediately discard the BO and close the screen. There was a change in the way CSLA handles this code from 3.0 to 3.5 and later so that old code will now break - because it was mistaken code anyway. Rocky is now forcing the issue after giving warning about it in 3.0.

Joe

Copyright (c) Marimer LLC