CQRS: on Commands and Validation

Let’s have a quick discussion about CQRS. There’s a lot to say to be honest, so let’s try to focus on just one thing today: validating your Commands (who knows, I could start a series after this, we’ll see).

The idea is simple: how can I make sure that the data I am passing to my Command Handler is valid?

Also, what is the definition of “valid” ?

There are several aspect to take in consideration, several “levels” of validation. I could just make sure the Command object is not null and/or the data it contains is not empty. Or I could run the validation against some kind of context and check the application Business Rules.

As you can imagine, having different levels means that we could have different implementations scattered in various places/layers of our architecture. For example I could have the API Controller (or whatever outmost layer you have) check for null and perform some Business Context validation later, before or directly in the Command Handler.

In my last project however, I decided to keep things simple and keep my validation in just one place.

Initially the right spot was the Command Handler itself, but of course this would have violated the SRP.

A quick and immediate solution was to have a separate instance of a IValidator<TCommand> injected in the handler. Easy.

Then I realised that my handlers are more “close to the metal” than expected: in most of the cases they access directly the DAL (passing through some kind of IDbContext) and I didn’t wanted to rewrite the call to the IValidator in case I had to switch the persistence layer.

Luckily enough, there’s a nice pattern that came into rescue: the Decorator! As explained very clearly on the SimpleInjector docs, you can create a ValidationCommandHandlerDecorator class, inject an IValidator<TCommand> and let your IoC do the rest.


Bonus tip: in some cases you may want to skip completely the validation. Maybe you have a very good reason or maybe you’re just lazy. Whatever.

In this case, all you have to do is to write some kind of NullValidator<TCommand> class and instruct your IoC to use it when a specific validator is missing for that Command.

Dell Limerick Hackathon 2016

Hi everybody!

Last January we had an Hackathon here @ Dell Limerick, the main theme was “office productivity”, aka “how would you improve your and your coworker’s productivity”.

I was in a team with other 4 very smart guys, didn’t won but all in all it was a terrific experience…two days straight of brainstorming and coding madness combined with pizza and energy drinks.

The winners came up with an interesting prototype of a chat bot running as Lync addon that can answer every type of question, from “how’s the weather” to “who broke the last build?”, passing from “tell me about story 1234567”. I can’t go too deep in the details (also, lots of natural language analysis is involved) but it was definitely a very, very interesting project and really deserved to win.

My team instead…well we produced a voting platform for ideas. In a nutshell, every user registered to the community can post his ideas (which can be divided into macro-areas) and the others can vote it using points they have received when registering. If an idea is approved, the voters will get back the points and a small bonus. If an idea is cancelled instead, they will get the points back (but no bonus).

It was a cool project to work on, we used a very simple micro-service CQRS architecture running on AngularJS, WebApi and MongoDB. Oh and everything was hosted on Azure.

After the contest, we decided to release all the sources, you can find them on my GitHub repository.


“Mastering AngularJS for .NET Developers” is here!

Finally! I thought the package had gone lost but then…today the postman knocked the office doors aaaaaand here it is!

mastering angularjs - book


It’s an interesting book indeed. Covers most of the functionalities, front to back with an eye to the inner workings and how to integrate with .NET, EntityFramework and WebAPI.

Personally, I’m not a big fan of SPAs . I mean, for example there are cases when you need to hide some parts of the system using some kind of role management. Handling everything server-side “should” grant an higher level of security.

Also, you don’t send to the client unnecessary html blocks, which is always a good thing.

Anyway, here’s my little piece of satisfaction 😀

MVC: reading LinkedIn user profile data

Suppose you have to login your users using an external provider. Maybe LinkedIn. Suppose you have to read the user profile data and do some stuff. Maybe you have to register the guy on your website.
Maybe you know a bit of OAuth and you want to give it a try but maybe you don’t want to bother writing a library from scratch.

What do you do? Search on NuGet of course! I have found a nice library that helps with this, named Sparkle.LinkedInNET 🙂

Here’s a quick’N’dirty gist showing the bare minimum necessary to get the oauth token and reading  the profile data. Then use it wisely!

As you may imagine, the flow begins with the Profile action ( something like “http://mydomain/linkedin/profile” ). The user will be redirected to LinkedIn and from there back to your website, this time with a token that you can use to interact with the LinkedIn APIs.

Here’s the full code, enjoy 🙂

AngularJS + MVC : how to pass model from server to Angular controller

Here’s a quick tip on how to pass a complex model from server side to an AngularJS controller.
The idea is to serialize the model to json (I am using the majestic Newtonsoft library for that), store it into a javascript variable and then create a provider with it on the AngularJS application that will be injected in the controller.

Here’s the controller code:


myApp.controller(‘myController’, [‘$scope’, ‘viewModel’,
   function ($scope,  viewModel) {
        $scope.viewModel = viewModel;


and here’s the MVC View

<div ng-controller="myController"> …. </div>
<script type="text/javascript">
var model = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model));
myApp.value("viewModel", model);

© 2019 Davide Guida

Theme by Anders NorenUp ↑