public class SmartTransaction : CustomTransaction
{
bool isReadOnly = true;
public SmartTransaction( Transaction trans )
: base( trans )
{
}
/// <summary>
///
/// Property: bool IsReadOnly (default = true)
///
/// If true, the transaction will be committed when it is
/// disposed. Otherwise, the transaction is aborted when
/// it is disposed (default behavior). The initial value
/// of this property is true, and will become false the
/// first time an object is obtained via GetObject() and
/// the OpenMode argument is OpenMode.ForWrite, or if a
/// a new object is added via AddNewlyCreatedDBObject().
///
/// Once the value has become true and changes have been
/// made, the transaction acts like a normal transaction,
/// requiring a call to Commit() to prevent the instance
/// from aborting when it is disposed.
///
/// Typically, you set this property to false just before
/// modifying the drawing and/or the environment, and want
/// those changes to be rolled back if an exception occurs.
///
/// If your operations are purely read-only, and you do not
/// modify the database or environment, you would leave the
/// value of this property set to true, and not bother to
/// call Commit() (the transaction is committed when it is
/// disposed in that case).
///
/// </summary>
public bool IsReadOnly
{
get
{
return isReadOnly;
}
set
{
isReadOnly = value;
}
}
public override DBObject GetObject( ObjectId id, OpenMode mode )
{
return this.GetObject( id, mode, false, false );
}
public override DBObject GetObject( ObjectId id, OpenMode mode, bool openErased )
{
return this.GetObject( id, mode, openErased, false );
}
public override DBObject GetObject( ObjectId id, OpenMode mode, bool openErased, bool forceOpenOnLockedLayer )
{
if( mode == OpenMode.ForWrite && isReadOnly )
isReadOnly = false;
return base.GetObject( id, mode, openErased, forceOpenOnLockedLayer );
}
public override void AddNewlyCreatedDBObject( DBObject obj, bool add )
{
if( add && isReadOnly )
isReadOnly = false;
base.AddNewlyCreatedDBObject( obj, add );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( base.AutoDelete )
{
if( isReadOnly )
this.Commit();
else
this.Abort();
}
}
base.Dispose( disposing );
}
}