Whether TransactionalTypes.TransactionScope can be used for two Parent?

Whether TransactionalTypes.TransactionScope can be used for two Parent?

Old forum URL: forums.lhotka.net/forums/t/2912.aspx


Jarvis posted on Sunday, May 20, 2007

I have two parents like as AddressInfo and ContactInfo. when I do update, I need they can change same. If there are some errors, they all can rollback. like

---------------------------------------------------------

[Transactional(TransactionalTypes.TransactionScope)]

protected override void DataPortal_Update()

{

updateAddress();

updateContact();

}

RockfordLhotka replied on Sunday, May 20, 2007

There's a difference between parent and root. A parent is any object that contains child objects, but a parent can also be a child. A root object has no parent.

I'll assume you want to save two root objects within a single transaction.

To do this, you need to create a third object - often a CommandBase-derived object - that performs the save of both root objects. Your UI code might look like this:

ContactUpdater upd = new ContactUpdater();
upd.Contact = _contact;
upd.Address = _address;
upd = upd.Execute();
_contact = upd.Contact;
_address = upd.Address;

The ContactUpdater class is a CommandBase-derived object that implements a static/Shared Update() factory method that loads the command object with the two business object references, then calls DataPortal.Execute() to execute the command. The command runs on the server, and your code looks like this:

[Transactional(TransactionTypes.TransactionScope)]
protected override void DataPortal_Execute()
{
  _contact = _contact.Save();
  _address = _address.Save();
}

The command object is merely being used as a way to ferry the objects to the server, invoke their Save() methods within a transaction, and ferry the resulting objects back to the client.

Copyright (c) Marimer LLC