Should I make a call to GetObjectContext() in every function in my DLL?
Obviously, you are going to do that in your “main” methods, but does calling GetObjectContext() in every function (main and helper functions) make your code more efficient?
In the following example I’ve called GetObjectContext() in each function. Is that overkill or quality coding practice?
Function Sum(nNumber1, nNumber2) as Integer '--- this is the main function a client would call GetObjectContext() '--- now call helper function to do the work nAnswer = TotalThem(nNumber1, nNumber2) Sum = nAnswer End Function Function TotalThem(nNumber1, nNumber2) as Integer GetObjectContext() TotalThem = nNumber1 + nNumber2 End Function
MTS automatically creates the ObjectContext for each object when you instantiate an object under MTS. You call GetObjectContext from a method when you need to perform certain actions. The GetObjectContext method returns a reference to the ObjectContext object. You can use this object to:
- Declare that the object’s work is complete. Prevent a transaction from being committed, either temporarily or permanently.
- Instantiate other MTS objects and include their work within the scope of the current object’s transaction.
- Find out if a caller is in a particular role.
- Find out if security is enabled.
- Find out if the object is executing within a transaction.
- Retrieve Microsoft Internet Information Server (IIS) built-in objects.
So, no, you do not have to call the GetObjectContext method as you are doing in each method. You only need to call it when you need to do one of the above actions. Also, in Windows 2000, call the ObjectContext.CreateInstance in order to instantiate other MTS Objects so they live in the same transaction that has been removed.