StrataFrame Forum

Transaction on a form with "save" button from toolstrip... posible?

http://forum.strataframe.net/Topic30532.aspx

By Fabian R Silva, - - 11/3/2011

Greetings Strataframe Forum.
 I'm happy to be using StrataFrame again Smilemy boss has renewed my license today, because that for one or another reasons we had to postpone the development of our system until this month after a long long  time of inactivity.

where... I will start with a question to wam up engines Laugh

I'm building a "base" form to inherit from this in all my system's forms later. I see that the "Save" method of the form doen't allow to make a transaction automatically with all the BOs that are marked with "incluideInFormSave", then I suppose that if I have 2 BOs (Parent-Child) and the child have errors the Parent BO be saved and the child not.... (this is not transactional)

I overrides the form's Save method and wrap the base save method with a transaction... it appears to work but I not sure if I doing it well.
the code that I used is in the end of this post...

I'm doing this OK or there is a better and automated way setting a property or something else that do it automatically? 

Thanks, Fabian.

This is the wrapper code: 
        /// <summary>
        /// Saves the changes of all business objects on the form marked with IncludeInFormSave.
        /// </summary>
        /// <returns></returns>
        public override SaveUndoResult Save()
        {
            if (!SaveWithTransaction)
                return base.Save(false, ""); //if not save with transaction call base routine to save each BO and return...

            // ----------------------------------------------------------------------------------
            // If this form SaveWithTransaction continues here...
            SaveUndoResult saveUndoResult = SaveUndoResult.Success;

            //-- Add a try around the transaction processing
            // This enables the process to call TransactionRollback()
            // if anything bad happens during the transaction process.
            try
            {
                //-- Start the transaction
                BusinessLayer.TransactionBegin(DataSourceKey, IsolationLevel.ReadCommitted);

                //-- Save the business objects on the transaction
                saveUndoResult = base.Save(SaveWithTransaction); //<-- here I call that all the BOs be saved in the started transaction (not yet commited to DB)

                //-- When business objects are saved on the transaction, the pending changes to their internal DataTables are NOT saved until TransactionCommit() is called... 
                // Call transaction commit and  queries AND accept the pending changes on all of the business objects
                // that participated in the transaction.
                BusinessLayer.TransactionCommit(DataSourceKey);

                // IF the COMMIT IS succefull this line CLOSE the form WITH a DIALOGresult.OK, otherwise CATCH block IS executed AND rethrow the DB exception....
                CloseFormWithDialogResult(DialogResult.OK);
            }
            catch (Exception ex)
            {
                //-- If an exception occurs during the saving of the records, then 
                // abort the transaction.
                BusinessLayer.TransactionRollback(DataSourceKey);
                throw;
            }

            return saveUndoResult;
        }
By Fabian R Silva, - - 11/8/2011

somebody?

By Edhy Rijo - 11/8/2011

Hi Fabian,
I am not a C# developer, but your code looks just fine.  One thing to have in may is that when you have related business objects, if you issue a Save() in the child BO, this will automatically trigger a Save() in the parent in order to get the Parent PK Value to be saved in the child BO.  I don't recall if that functionality can be overwritten but just in case, in your Save() method make sure you are saving the Parent BO first, then all the child BOs.
By Fabian R Silva, - - 1/2/2012

thanks for the reply and advice on child BOs, As I'm using a wrapper and calling base.Save() like the overrided method, I suppose that it have to work as expected and the BO will manage relationships and save all BOs automatically. the diference is that I calling a begin and commit (or rollback) transaction in the override to make it work as a transaction, anything else works as usually using base.Save().

If I encounter problems later will report it in the forum asking for possible solutions.

Kind regards and hope a good 2012 for you, all the forum users and all the world Smile



 
Edhy Rijo (11/8/2011)
Hi Fabian,
I am not a C# developer, but your code looks just fine.  One thing to have in may is that when you have related business objects, if you issue a Save() in the child BO, this will automatically trigger a Save() in the parent in order to get the Parent PK Value to be saved in the child BO.  I don't recall if that functionality can be overwritten but just in case, in your Save() method make sure you are saving the Parent BO first, then all the child BOs.