Advice regarding the use of Transactions is contradictory.
Refer the ..\ObjectARX 2016\Samples\dotNet\ files
Typically the Tables are instantiated using the GetObject method of the TransactionManager.
The AddNewlyCreatedDBObject method variously uses the Transaction and the TransactionManager
Advice and examples elsewhere use the methods of the Transaction, which I understand is correct.
Why are they different ?
No matter which one used they end up calling the same methods but for many calls it might matter which one used for AddNewlyCreatedDBObject.
Maybe better stated as how it is accessed as shown below.
For ObjectARX the
GetObject and
AddNewlyCreatedDBObject methods are duplicated in TransactionManager and uses last transaction added(TopTransaction) to call Getobject() and AddNewlyCreatedDBObject.
Since GetObject and AddNewlyCreatedDBObject can only be used by the last transaction added the TransactionManager will always use the correct Transaction to call both methods , and do not have to keep up with a instance to pass between methods.
The way it is implemented in .NET is Transaction and TransactionManager GetObject call a static method defined in TransactionManager which calls GetObject of the last Transaction
Transaction GetObject
public virtual unsafe DBObject GetObject(.....)
{
this.CheckTopTransaction();
return Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(.....);
}
So Transaction.GetObject calls TransactionManager.GetObjectInternal to call back to its unmanged Getobject (), so it does not matter which one and will
end up calling the same methods.
The same goes for AddNewlyCreatedDBObject but how you
access it could make a difference when used many times in loop.
Transaction's AddNewlyCreatedDBObject
public virtual void AddNewlyCreatedDBObject(DBObject obj, [MarshalAs(UnmanagedType.U1)] bool add)
{
this.TransactionManager.AddNewlyCreatedDBObject(obj, add);
}
AddNewlyCreatedDBObject does not use a static method and uses a instance of TransactionManger.
The Transaction's property named TransactionManager of type TransactionManager is used to call TransactionManager.AddNewlyCreatedDBObject.
But Look at Transaction's property for TransactionManager
public virtual Autodesk.AutoCAD.DatabaseServices.TransactionManager TransactionManager
{
get
{
IntPtr unmanagedPointer
= new IntPtr
(AcDbImpTransaction
.transactionManager((AcDbImpTransaction
* modopt
(IsConst
) modopt
(IsConst
)) this.GetImpObj())); return (Autodesk.AutoCAD.DatabaseServices.TransactionManager) RXObject.Create(unmanagedPointer, false);
}
}
Notice how the property getter
creates a new manged wrapper each time it is called(return ...RXObject.Create(unmanagedPointer, false))
So for adding many objects it might be best to create a variable to use same TransactionManager.