public void PersistSessionObject(IDbConnectionAbstraction connectionAbstraction)
{
/// If this is a delayed transaction then we'll never need to update this. The reasons are -
/// 1. Changing the ID of this object is pointless as it would violate referential integrity.
/// 2. Changing the Session ID would never happen as this would breach Session fidelity.
/// 3. Changing the Parameter Type is not going to happen. As this would already be referenced
/// in multiple places it would be changing a pre-existing and expected context.
/// 4. The value never needs to be inserted as this is delayed and will be defined when the
/// transaction operation is run. - This isn't true, we do need the value to be updated in case we're referring to a MapParameter from an old transaction.
if (!ValueUpdated && IsDelayed && !IsNew)
{
return;
}
SqlCommand parameterSqlCommand = null;
try
{
bool addValue;
if (IsNew && !ExistsInDb(connectionAbstraction))
{
addValue = true;
parameterSqlCommand = new SqlCommand(InsertParameter, connectionAbstraction.Connection);
}
else if (IsDirty)
{
string updateSql;
if (ValueUpdated && Value != Guid.Empty)
{
updateSql = string.Format(UpdateParameter, "[Value] = @Value, ");
addValue = true;
}
else
{
updateSql = string.Format(UpdateParameter, "");
addValue = false;
}
parameterSqlCommand = new SqlCommand(updateSql, connectionAbstraction.Connection);
}
else
{
return;
}
parameterSqlCommand.Parameters.Add(IdSqlParameter);
if (addValue)
{
parameterSqlCommand.Parameters.Add(ValueSqlParameter);
}
parameterSqlCommand.Parameters.Add(SessionIdSqlParameter);
parameterSqlCommand.Parameters.Add(IsDelayedSqlParameter);
parameterSqlCommand.Parameters.Add(ParameterTypeSqlParameter);
connectionAbstraction.Open();
parameterSqlCommand.ExecuteNonQuery();
ValueUpdated = false;
connectionAbstraction.Close();
}
finally
{
if (parameterSqlCommand != null)
{
parameterSqlCommand.Dispose();
}
}
}