Unit Of Work Pattern

February 18, 2009

I’ve done some research on the Unit Of Work pattern and this post will show you what I’ve learned.

Definition

A short version of the official definition of the term “Unit Of Work” can be found on Martin Fowler’s website. A longer version can be found here.

Implementation
A small proof of concept type of implementation can be found at Code Insanity: The Unit of Work Pattern. He continuous in his post Implementing a persistence ignorant Unit of Work framework.

Looking around the internet for information you will often find remarks about the Unit Of Work pattern in conjunction with multiple threads. The article over at InfoQ named Confusing unit-of-work with threads gives a good overview on the caveats lurking there.

Now, how do we use this Unit Of Work?

Usage in ASP.NET applications

There is a great deal of concensus on how to apply the unit of work pattern in web applications in general and in ASP.NET in particular.

In general, in web applications the Unit Of Work per Request implementation is used. You can find a sample implementation in the article Using the unit of work-per-request pattern in ASP.NET MVC and using NHibernate in NHibernate Best Practices with ASP.NET
An alternative is the Unit Of Work per Conversation. The Transactions and Units of Work in Web-Applications article gives a good general introduction and the post What are the benefits of session-per-conversation? compares the two approaches.

Usage in Windows Foms applications

Windows Forms applications are a completely different beast. In a web application there is a natural boundary of a request. There is however no such thing in a winforms application. So finding guidance is difficult. The abone mentioned post Transactions and Units of Work in Web-Applications is a good start.

Conclusion

The Unit Of Work pattern is a basic pattern to streamline data access in applications. It provides a means to keep track of changes to your business objects and apply the changes as a single unit of work to your database,

I hope you found the links illumminating and if you have any yourself, please leave a comment.

Links
Definition:
P of EAA: Unit of Work
IS Architecture Pattern: Unit of Work

Implementation:
Code Insanity: The Unit of Work Pattern
Code Insanity: Implementing a persistence ignorant Unit of Work framework
InfoQ: Confusing unit-of-work with threads

General usage:
Transactions and Units of Work in Web-Applications

In ASP.NET:
Using the unit of work-per-request pattern in ASP.NET MVC
NHibernate Best Practices with ASP.NET, 1.2nd Ed.
Appling Unit Of Work pattern
ASP.NET MVC, DataContext and The Unit of Work Pattern

Just a quick posting of an application of Windows Workflow Foundation in the area of image processing.

When I started the Exploring Workflow Foundation series of posts this is the reason I started them: I wanted to make a library in the area of image processing based on workflow activities. Well, to be honest, it’s more the other way around: I wanted to learn Workflow Foundation and ended up with image processing as a candidate subject. Anyway, this library is not complete, let alone production ready code. It’s what I have so far but it still needs a lof of tweaking. For example, I did not make use of any validation code when compiling th eworkflow, which is something I will definitely add in the future. There is also no real exception handling code. In other words: this version is just a quick post of what I have so far but still needs a lot of work.

So keep watching for updates.

Just go to the ImageProcessing WF page for a download of the available code.

Links for this week:

casting and the “as” keyword in C#
This is probably basic knowledge, but here are some references for the difference between the possible casting constructs in C#:
Prefix-casting versus as-casting in C#
What’s the difference between cast syntax and using the as operator?
Type casting impact over execution performance in C#
http://stackoverflow.com/questions/61733/c-casting-vs-parse

Generics in .Net
Last time I allready gave some links on generics in C#. Here’s one more on using a generic type in a constructor, which is not possible:
C# generics on conversion operator and constructor

WCF and enumerations
You’d think you wouldn’t need to do anything special to send enumerations in a datacontract in WCF, but apparently you have to:
Looking at the WCF Windows Communication Foundation
Serializing Enums in WCF
EnumMemberAttribute Class

Crystal reports under vs 2003: refresh the database fields
Anyone using Crystal Repports most have had this experience: you add a property to your datasource, recompile, but still the property doesn’t showup in the available fields for your report. This is how you can make it available without having to reconstruct your report:
Refresh database fields in crystal report designed
Crystal – refresh database
how to refresh the field listings from a dataset in Crystal Reports VS2003

Timers in .NET framework
I had to port an application from .NET 1.1 to .NET 3.5 this week. The original code had some timers in the user interface and in refactoring the applications those timers ended up in a service. There are some cave-at’sespecially with re-entrance in your timer method:
Comparing the Timer Classes in the .NET Framework Class Library

Rename and move file in sourcesafe when using visual studio
For anyone using Visual Sourcesafe integration in Visual Studio and trying to rename a file from within the Visual Studio project explorer: think twice. This doesn’t work is one would expect. The file is checked out from Sourcesafe and renamed. But this renaming is only at the filesystem level and not in Sourcesafe. So now you have two files in Sourcesafe: the original which is checked out and no longer available in Visual Studio, and the new one which is marked for inclusion in Sourcesafe.
The procedure you do have to follow is described in following posts.
How to: Rename a File or Project
Working with Visual SourceSafe
A quote from Using Visual Sourcesafe:

The advice from those with experience of developing using VSS is that before adding files to the VSS database it is useful to progress the development to a stage where the general structure of the application and its source code is mostly settled. This is because the process of renaming and moving application files can raise various small problems.

Well, so much for fearless refactoring