How can I accomplish the equivalent of this? I understand that this is not possible with TransactionScopes , but I would like to execute the equivalent in another way:
Business Logic Class:
public bool Bar() { try { using (var tsWork = new TransactionScope()) { ComplicatedDataImportCode(somedata); FlagRecordInDatabaseAsImported(); // this is the same record that modified in the catch tsWork.Complete(); return true; } catch (DuplicateDataException err) { // if we got here, the above transaction should have rolled back, // so take that same record in the database and update it to "Duplicate". FlagSameRecordInDatabaseAsDuplicate(err.Message); } return false; }
Now this works great until I encapsulate all of this inside a transaction (maybe an integration test that I want to roll back after the statements have completed).
A simple test to prove my point:
public void CanTest() { // Arrange var foo = new Foo(); using (var ts = new TransactionScope()) { // Act var success = foo.Bar(); // Assert if (success) { Assert.That(SomethingThatTestsThatTheDataWasImported); } else { Assert.That(SomethingThatTestsThatTheRecordWasMarkedAsDuplicate); } // Now that we have been able to perform our Asserts, rollback. } }
Ultimately, the code in Foo.Bar() can be changed to accommodate the solution, however, the code in ComplicatedDataImportCode() cannot be changed for this solution, and therefore, I really need to make sure that I roll back correctly in the crash script.
Again, I understand that TransactionScopes cannot be used for this, according to the post I referred to at the beginning of this question. I used TransactionScopes here to indicate what I wanted to do, and I'm looking for a better alternative way to implement this functionality.
Jaxidian
source share