Blog.

Auto retry concurrent commands with ncqrs

MF

Marco Franssen /

2 min read284 words

Cover Image for Auto retry concurrent commands with ncqrs

In a previous post I showed you some pseudo code Gregory Young mentioned in his DDD CQRS course I attended in Krakow, Poland. In this course Greg made clear to us locking of databases isn't necessary. He showed us some pseudo code how to easily write a merge handler to handle all concurrency conflicts.

In my current project, based on the ncqrs-framework I implemented a simpler version of this merge handler which only retries each command if a ConcurrencyException occurs. To achieve this you can easily build a wrapper around the ncqrs commandservice.

public class SafeCommandService : CommandService
{
    public override void Execute(ICommand command)
    {
        try
        {
            base.Execute(command);
        }
        catch(ConcurrencyException ex)
        {
            Execute(command);
            //Log retry executed
        }
    }
}

You can choose to implement this in a WCF service or whatever you want. Every command that fails will be retried until it succeeds. I have tested this with hundred async JavaScript calls which send a real simple command (PrintLabelCommand). My domain only processes a LabelPrintedEvent. When sending these calls to the server my log tells me there are only 8 retries average. This is acceptable in our situation. If you have more complex scenario's you will probably need a more advanced setup like the pseudocode in this blogpost shows you.

I'm still searching for a better name for my class, because SafeCommandExecutor isn't the best name I think. So let me know if you have a better one. I hope this article will be useful for you guys. As always, please share…

If you improved my code let me know, so I can learn from it.

You have disabled cookies. To leave me a comment please allow cookies at functionality level.

More Stories

Cover Image for Pitching equals invisible convincing

Pitching equals invisible convincing

MF

Marco Franssen /

During the last year I learned and read a lot about convincing people. In this article I want to share some tricks to apply it yourself. Oh, its my first non technical article. So this will be a milestone for myself :D. It isn't always as easy to convince someone. Some people just manage to get more things done as others. A part of your skills to convince someone is in your own personality. To convince someone you have to be powerful, special and kind. This means you need to know where you're t…

Cover Image for Writing modular JavaScript without polluting the global namespace

Writing modular JavaScript without polluting the global namespace

MF

Marco Franssen /

Most of you have already seen a lot of spaghetti JavaScript code. One of the reasons you are reading this article will probably be, you don't want to make the same mistakes as others have done. So let's make the next step and stop polluting the global JavaScript namespace. Why is it bad to have all your script code available at global level? First of all you can possibly get a lot of errors when using modules developed by others, because you used the same names for you variables etc. The seco…

Cover Image for Install and boot Windows 8 from vhd

Install and boot Windows 8 from vhd

MF

Marco Franssen /

In a previous blog post I explained to you how to install Windows 8 in a virtual machine in VirtualBox. In VirtualBox I used 1GB of memory and 2 of my cores and it performed quite good. However in the metro interface I had some issues with my mouse (scrolling, delays etc.) So yesterday I decided to install Windows 8 on a vhd and boot directly from it. To do so I followed Scott Hanselman's blog post. Below I placed a shorter summary for you guys: Step 0 Make sure you have at least 40GB of free…

Cover Image for jQuery events contributes to clean Javascript

jQuery events contributes to clean Javascript

MF

Marco Franssen /

As the title reveals, this blogpost is about some clean javascript code example. Many developers dislike javascript because of different reasons. I think javascript is a pretty cool language. To prevent developers from disliking it and encourage them to show how great javascript can be, we all as developers have to write javascript in a clean way. This way lesser developers will be discouraged to use it. In this blogpost I will show you how to write some OOP style javascript, which you know as…