Tuesday, April 19, 2011

Domain Events, MSMQ, and long running processes

How often have you dealt with the following scenario?

You are tasked with designing a new feature on your website where users have the ability to import data. Typically this is accomplished by some sort of a interface that allows a user to upload a file. The file is then either put into some sort of storage mechanism such as a database or a file system. At this point you have to make a decision.

Do we?
A.) Process the data right away during the same request?
B.) Create some sort of process/application that runs on an application server to run at some sort of predetermined time interval?

Both options A and B above will work given the right scenario. Option A is a bit more risky due to the fact that depending upon the size of the import it could possibly timeout during the request. In my experiences, option B is typically the method that is used.

So assuming we choose option B. We end up having to create a new application, have it copied/installed on the application server and then finally have it scheduled to run throughout the day. Nothing wrong with this implementation, but it ends up becomming more cumbersome when we have to deal with it several times. Regardless if it is a file upload process or any other long running process, when the user issues a command that would normally timeout we end up having to make a decision on how to handle it.

When I read the article by Udi Dahan Domain Events - Salvation I realized there is a better way to handle the problem described above. The fact that the user uploaded a file for import is an important concept within our domain. Here is where domain events come into play. Domain events can be used for multiple situations. A common scenario is when you need to bridge bounded contexts, or to seperate out our concerns. For this post I'll be using domain events to publish a message to MSMQ for processing. This way we don't have to worry about any timeout issues. The file is uploaded and the domain publishes an event that the file has been uploaded. The other advantage to handling long running processes this way is that whenever you need to implement something similar, all you have to do is add another handler to your windows service that is monitoring the queue instead of creating a new application.

In my next post I'll put together some example code on how this is accomplished.

Sunday, April 17, 2011

Greetings

Lately I've felt compelled to start blogging my ideas, or at least the ideas of others that I feel are important. Hopefully the development community will find them helpful as well. I'm going to attempt to post something "real" in the next few days. I plan on covering how to use domain events to handle long running processes. The inspiration for my first blog post comes from an excellent article by Udi Dahan called Domain Events - Salvation. I suggest reading his post because it's well worth the read.