StrataFrame Forum

Using transaction and show broken rules

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

By Chan - 2/22/2007

Hi,

I have code to update some tables (not visible on screen) upon Form.AfterSave() event fired using transaction as shown below.

1. Is it the "right way"?

2. I want show broken rules if failed. How should I show them? Any advice?

public SaveUndoResult SaveToTransfer()
{
  SaveUndoResult loReturn = SaveUndoResult.Success;
  try
  {
    //PushDataToTransfer();
    BusinessLayer.TransactionBegin("", IsolationLevel.ReadCommitted);
    if (loReturn == SaveUndoResult.Success)
    {
       loReturn = _TransfersBO.Save(true);
    }
    if (loReturn == SaveUndoResult.Success)
    {
       _TransferDetailsBO.Save(true);
    }
    if (loReturn == SaveUndoResult.Success)
    {
      _PInvoiceTransferBO.Save(true);
    }
  }
  catch (DataLayerSavingException ex)
  {
    throw ex;
  }
  catch (Exception ex)
  {
    throw new BusinessLayerException("An error occurred while saving the data to the server.", ex);
  }
  finally
  {
    if (loReturn == SaveUndoResult.Success)
    {
      BusinessLayer.TransactionCommit("");
    }
    else
    {
    BusinessLayer.TransactionRollback("");
    }
  }
  return loReturn;

}

By StrataFrame Team - 2/22/2007

Yes, that is the correct way to do it.  The only thing I would change is if you're planning on threading this or running this on a web application (any time you might have this code running more than once at the same time), you'll want to specify a transaction key for the Begin, Commit, Rollback, and saves.  Maybe something like Guid.NewGuid().ToString() to make it random (but save it off so you can pass the same value to all of the method calls that are part of the same transaction).  But yeah, what you have looks good. 

After each of the Save() calls, the BrokenRules property on the business object will have all of the broken rules.  You can enumerate through that collection and create a flat list of the broken rules for display to the user.