I designed the data access portion of our framework so that every time a business object (BO) needs to interact with the database, it would have to open a connection, invoke the data access layer (to execute the query), and then close the connection. Then if it needed to run in a transaction, it would open the connection, begin the transaction, invoke the data access layer (to execute the query) and then commit the transaction, close the transaction, and finally close the connection.
I did it this way in the mindset of “open late, close early”… but what if I needed to call other BOs to submit data in a single transaction? Is there a better way to handle opening and closing connections as well as working with transactions?
I’m a rookie in designing application architecture, so I hope I’m not doing this wrongly… any help is appreciated.
If a given business object needs to execute various methods in a transaction, use a
TransactionScopelike so:If any of the objects throws an exception, it will rollback the transaction.
See the MSDN reference page for
TransactionScopefor more.