Unit Of Work Pattern

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

Advertisements

Exploring Workflow Foundation Intermezzo 1: Apply what we learned so far – Early post of the Image Processing Activity Library

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.