LINQ to SQL N-Tier Smart Client – Part 3 Database Transactions

Avatar

Beth

In my previous posts this week I showed how to build a simple distributed application with a Windows client, a WCF hosted middle-tier and a data access layer that used LINQ to SQL:

LINQ to SQL N-Tier Smart Client – Part 1 Building the Middle-Tier

LINQ to SQL N-Tier Smart Client – Part 2 Building the Client

After sleeping on the design I realized that there’s a scenario that we may want to handle. When we built the connected client-server version of the application (using the connected DataContext), because the DataContext is tracking all our changes (updates/inserts/and deletes) when we call SubmitChanges these updates are all processed in one single database transaction.

This may or may not be required for your application and in the case of Orders/OrderDetails it’s okay to allow the updates and inserts and then the deletes to be processed in separate transactions. However what if we were working with drug interactions in a medical application or other data that needs to provide this level of integrity?

It’s easy to make these modifications to our n-tier application we built. All we need to do is attach ALL the changes that we want processed in a single database transaction to one instance of the DataContext. To do this first we need to modify our service to accept all our changes. This can end up putting more data on the wire which we discussed in Part 1 so you need to evaluate your scenarios carefully. In our case I’m only pulling up open orders for a particular customer ID so the data set is relatively small.

First add the following interface on our WCF service:

Next add the implementation to the OMSDataManager class in the data access layer to go ahead and attach all the changes to a single DataContext and submit all the changes at once. Note that the validation is performed exactly as before (when SubmitChanges is called).

We can then modify our form to call this new operation. On the client form I just added a new method called SaveAll. Note that the same simple change tracking is being used.

So now when we make updates, inserts and deletes to our Orders and OrderDetails then we can save them all in a single database transaction.

I’ve uploaded the latest version of the application onto Code Gallery with the modifications.

Enjoy!

Avatar
Beth Massi

Follow Beth   

No Comments.