I was in Belfast yesterday were I was presenting what I called “CQRS/ES and friends” in NIMTUG . I had a great time thanks to all who came and thanks to the organizers.

I would normally post here my presentation, but not sure its of any value, as it doesnt have much info, I use it for helping me remember what to say, if anyone want it, i can definetly post it, just add a comment here.

The code I was showing is a fork of  Super Simple CQRS code example by Gregg Young

I have a link (bit.ly bundle) with a bunch of links about CQRS and ES, including a few other sample code bases  http://bit.ly/9LC877

If you have any questions, or feedback good or bad (something missing?) please let me know by posting a comment here (if you don’t want me to publish it just say so )

Thanks for coming :)


This post belongs to a series of posts on CQRS/ES

Part 1 – introductory terms and overview

Part 2 – Event sourcing and information about commands

So, we are moving on.

I thought I’d put these two topics close, it seems to me  that they are related, I ve been using BDD to test this

DDDDevision_big

After all, what I m testing is much more than a unit, and I m interested in the whole loop. What I find interesting here is that since we are talking about actions when we talk about commands and we are also talking about past events, the Given, When, Then become very rather obvious.

So, you can say something like:

Given a NewCustomerCommand with "Name"
and a NewCustomerCommandHandler
When I Execute the Command
Then a CustomerCreatedEvent is published
and Customer with "Name" exists

and, if you, like me, are using StoryQ, this story can look like


                .WithScenario("Create new customer")
                    .Given(ANewCustomerCommandWith_, "Name")
                        .And(ANewCustomerCommandHandler)
                    .When(IExecuteTheCommand)
                    .Then(ACustomerCreatedEventIsPublished)
                        .And(CustomerWithName_Exists, "Name")

With this in mind, its not hard to imagine the code to make this happen, and because of that, the focus remains on the modelling, when I see this code, I can’t help but think, Do I really need to create a new customer to be able to process orders? Is the name all that relevant?, maybe the email is more representative… all of these are domain questions, this is exactly the kind of questions that should arise, this focus is on the domain is one of the most interesting advantages of CQRS. And we need to change our code. Your testing points are at the command level, at the query level and/or at the event level.

An interesting advantage of testing in a BDD style is that you have very concise, runnable documentation, and can kick the conversation off, with your domain expert. Here you could say, wait a minute, my domain expert doesn’t know about commands, or queries. Well then perhaps you need to move the layer of testing up a bit higher to the UI.  Testing points go a layer above, another option is test at both levels.

Summary

  • Behaviour Driven tests help you model the domain
  • Your testing points are your commands, command Handler, query services and events. No need to mess around with aggregate roots directly, and hence why no need for getters on the aggregate roots
  • Your tests are executable documentation

UPDATE: Next up Validation


CQRS with Event Sourcing

Ledger

Photo By edinburghcityofprint(flickr)

Looking at CQRS and Event Sourcing as Architectural patterns, when we use them together the impact of that is widespread. For a start, your aggregates should only be exposing behaviour, and that behaviour is related to the commands that use that aggregate root, actually, the entry point to doing anything in the domain is through a command. (this have implications on how we test, but we’ll talk about that later on).

The aggregate root should now expose only behaviour, encapsulating all state, setters are seeing as a code smell. This change is not a direct consequence of CQRS, however, it was very hard to achieve this without it if doing DDD.

One of the questions I got during the talk was about how to replay perhaps millions of events, without a huge delay, and the answer to that is Snapshots, basically you preserve the current state of the aggregate root, serialized . There are a few ways to do snapshots, but the principle is that you get your snapshot, which will be up to a particular version, and then replay any events from that version on ( if there are any).

At the system level, it is important to note that, we now have an explicit state transition, what I m trying to say is that when your customer (ie the person that comes up with the requirements for the system) sais something like, when a customer is created, then that can be mapped directly to CustomerCreatedEvent ie, there is a close relationship between what happens and what caused the state transition and that can be relevant to the business. This, I think, is the biggest advantage of using CQRS

Since the read storage and the Write storage are logically separated  and they can be physically separated too, that means, that they can have different scaling policies.

Another interesting benefit of using event sourcing is that you have an integration point available, without any extra cost. The integration point could serve a Reporting tool, or an external service, or maybe you just want to push some sort of notifications, these are all easily doable, you have a bus publishing messages…

Actually, the way we are using event sourcing in the above stated scenario implies an eventually consistent model. If interested in this topic, have a look at this blog for the cost of eventual consistency. We ll go through some code and then come back to this topic.

I don’t think I mentioned this yet, but, there is one really awesome thing about this whole thing, and that is that we are framework free, there are a few principles and ideas to learn, but once you got them, there is not really much that you need a framework for, maybe if you want you can have an out of the box event store, they are simple in principle but a bit harder to code that it appears first. There are some frameworks to do CQRS with ES however, the code you need to have a system like this running is not so complex, and understanding how it works is crucial.

The trail of the Command

Lets forget about the UI for a second and think of a system from the point of: I have a command, and I need to execute it.

Every time you send a command, the system handles the command, more than likely, an aggregate root will be involved and an operation on it will occur, this will result in one or many events being published; once the event(s ) have been published, the subscribers will get this message. One of the subscribers is the Event store, that will append this new event. There are variations of this description but this will do for now. Lets say the

public void Handle(CustomerMovedCommand command)
        {
				var customer = _repository.Get<Customer>(command.CustomerId);
				customer.Moved(command.AddressLine1, command.AddressLine2, command.Postcode );
				_repository.Save(customer);
        }

the aggregate root (Customer) code for that method could look something like this

public void Moved(string line1, string line2, string postcode)
{
    ApplyEvent(new CustomerMovedEvent()
                        {
                            AddressLine1 = line1,
                            AddressLine2 = line2,
                            PostCode = postcode
                        });
}

In there we can see that we are applying the event, once we apply the event, we will not only set up the internal properties of the aggregate root(that we need for write operations), also through the repository or other commit like mechanism(we’ll look at this in a sec), we will publish this message, making the event store aware of this new event and making the query side aware of this event too, the query side is then responsible of de-normalizing the data to a suitable shape.

An important thing to note is that the event is declared as something that happened, it is in the past, for example: CustomerMovedEvent, that means that as far as our system is concerned, the customer has moved.

Next part will include Modelling and testing

I bundled a few interesting links in this bit.ly bundle http://bit.ly/9LC877


I m preparing for a talk on CQRS and I though I’d support it with a series of posts on the subject. I’d love to get some feedback on this and all the following blog posts. (where I ll be possibly correcting stuff i say here, I can’t promise)

So, I decided to start at the beginning: definitions. When I started to learn about CQRS I just looked at a good few videos, read a bunch of blog posts and then dived into every code example I could find, and then I m working on a system that uses these principles. However, I wanted to roll back and see if I can somewhat formalise what I’ve learned so far. Starting with definitions

What is CQRS?

Based on this blog post and others , the consensus is something like: CQRS is a pattern that describes the separation of command messages , they change state, from query messages that read state.

Please note this is a pattern (i.e. not an architecture) . if you look around online you ll find that some people seem to see CQRS as a software architecture, it makes sense that this is confusing, I wonder if the “term” evolved so much that it lost its meaning, what most people refer to as CQRS is actually a combination of a good bunch of patterns that work well together.

CQRS is normally used with other patterns such as event sourcing, messaging patterns, task based uis, etc. I’d like to make an emphasis on the guideline bit, for me this is a powerful word, it tells me, something like, paint it some shade of yellow, but I have the choice to use the yellow that makes the room the way I need it to be.

Before I started looking at CQRS I was unclear about the relationship between an Aggregate root and the services that serviced it or worked on it. Once I started looking at samples of applications based on CQRS with ES, which was about the time we finished reading the DDD Blue DDD Bible, then it all became clear.  I think the main thing was that the boundaries of the aggregates and the boundaries of each domain were more tangible and made more sense.

Also if using CQRS, you ll find that your aggregate roots change shape, i.e. there are no public properties, at the very least you ll have no public setters, and all the methods in the aggregate sound more like actions, if you have methods such as AsDto(), that definitely means you are doing something wrong. Also you are loosing all properties that you needed for the read side of things, for example if you had an Order aggregate root, do you really need that OrderItems collection? you have access to that collection through the OrderQueryService.

The term CQRS, evolved from CQS(Command Query Separation) there is a CQS definition from  Meyer, that is very similar but it’s separating methods that change state, since the emphasis is on messages for CQRS, this was an important distinction.[ref]

To prepare for the talk I m also preparing an example (more later), the interesting thing is that, I find that using CQRS and Event Sourcing, I find myself asking questions about the system and the domain, and that is great, because it makes you focus on what the system should do, not the how.

That brings me to another important definition.

What is Event Sourcing?

Event sourcing means that you capture changes to an application state as a sequence of events (definition by Fowler) . Imagine most systems your worked with up to now, If you had a customer, and they moved, you would probably go to the user interface (web or whatever), go to some sort of customer edit screen, change the details, and thats it, most likely this would trigger an update operation in a normalized database, and unless there was a specific requirement, you’ve not only lost the previous value of the address, also, you dont know if the customer actually moved or if the address was incorrect, so you are loosing history and intent, two very valuable assets.

An important detail about this way of storing data is that its an append only model , because you are only adding, the write operation its simpler, this means it takes less time. Another consequence of an append only model is that to change something, we would apply compensating actions.  Interestingly, once the events are stored they are immutable.

If this is such a great way to store data then how come we are not using it everywhere? you might ask. Well there is the problem of having to replay all events to build an aggregate root each time you need to work on it, and hey, what if it’s a very busy one, and there are millions of events to replay. For that, we would use snapshots, i.e. every so often we would record the current state of the aggregate root as is and then when we need to get the aggregate root, we would have  to find the latest snapshot, and then replay any outstanding events to get to up to date. The real culprit is the fact that selecting the top 3 orders where the customer name is Bob, is kind of hard. However, since we are using Event sourcing on the write side and we   might just get away with it .

Event Sourcing is a big theme, the Event store is one of the most complex parts of using CQRS/ES. There are a few open source implementations out there. Olivers comes to mind

References and Sources

CQRS and Event Sourcing – Gregg Young

Clarified CQRS – Udi Dahan

Domain Driven Design Website

Command Query Separation – Gregg Young


StoryQ Review

12Nov10

We are evaluating BDD frameworks at the moment and I m going through StoryQ.

Note:

  • Internal DSL for BDD, for me this means I can discover a lot by just following the API (ie its got a nice fluent API)
  • The Code for adding a story is very simple (example below)
  • Readable and nicely formatted report output
  • Supports Nunit, xUnit and MSTests
  • if using xUNit there is no support for incomplete tests ( ie if you are using CI it will break the build) UPDATE: there is a way since xUnit 1.7
  • Also you need to override the settings with your own exception builder.
  • I had one of those Doh! moments because I setup the story and the scenario with the Given().When().Then() and then I called the Execute method, that was not going to work… :(
  • Can report in various format, the report shows up on resharper or in an more html way.
  • There is a wpf app that converts gherkin and it generates the StoryQ test.( couldn’t try because the app failed, I think it s because of a dodgy build I have but will find out and report later)
  • Seems to support well the idea of many scenarios per story within the same file, which is cool.
  • Good to keep in mind not to use underscores in method names Link

So far so good, if there is anything else noteworthy I ll update.

sq


In the Book Club, we are reading 7 Languages in 7 Weeks (for more info on the book club go to Dublin Alt.Net mailing list) so far a really awesome book, Its helping me have a quick view of not only different languages, but also different programing paradigms. Which is really refreshing, but hard. The thing that I like the most about the book so far , is that you do much more than a hello world, but of course, you can learn in depth, however, the author makes a really good job of explaining what’s at the core of each language, sometimes, just trying to really grasp that is a tiny bit hard , when  it’s a paradigm you are unfamiliar with it, but the effort is well worth it.

Anyway, since I haven’t been able to the last two meet ups, I thought I’d do a second read of the Scala chapter and put together some notes. Please note I m writing the notes as I read.

- Scala is a hybrid language (a mix of OO and functional)
- Hints that its a different kind of language
- Runs on the JVM, though given the state of Java, not sure that is any kind of insurance. Scala can use Java libraries natively, just like Iron* languages , can use .net libraries directly
- Statically typed
- type inference (like c#? –> review)
- functional concepts, like c#? it would be awesome if it added some native support for immutable types
- Support immutable types, but its got some modifier ( need to check this out more in depth)
- actors

Scala is a general purpose language, interesting that it seems to fit well many scenarios because of this OO, Functional mix
Type inference seems to be not so strong
Principles of functional programming that apply to Scala:
- programs are made of functions
- functions return a value
- given the same input, a function will return the same output
- side effect free

val is immutable var not …. Likes

There is a full page on the book dedicated to somewhat clarifying strong vs weak and static vs dynamic  languages. I mostly agree with his points there.

My first impression when seeing the language was that it reminded me more to python than to java, maybe it was def .

  • Visibility of methods, functions. Public is default
  • Support for tuples and ranges . In purely functional languages, tuples are used as objects. You can do multi value assignment with tuples
  • Constructors. I like Scala’s  syntax and default
  • Object instances are interesting, at first I was thinking they were like static classes in C#, but apparently not quite. As far as I understand, an object declaration in Scala is basically a singleton instance of a class definition.  However, in C# a static class is a class that can be accessed and used, without creating an instance. That makes me think that there is a cost associated with the instantiation, but I cant see any other difference. (comments on this would be appreciated)
  • traits. Like an interface and an implementation all in one, generally focusing on a single concern, well that’s what the book say, however I can’t see here how you do to declare a trait separetly

As Part of the exercise of Day 1 you need to find a comparison between Java and Scala(I found this), one of the interesting things I learned while checking that out is that XML is supported natively, I also liked the point on moving to a more functional style of programming slowly, but with a nicer syntax.


I was invited to give a talk on Open Source in DIT to a group of students. Link to slides http://www.slideshare.net/roundcrisis/open-source-and-you-5456161.

However here some interesting links:


I heard about this new web framework the other day, the code its available on github/jacksonh/manos the main interesting thing I heard about it were:

  • runs on Mono
  • Has its own server
  • Has no dependecies on Asp.net
  • the main goal of mono is to simplify web application development
  • routing appears to be simpler ( to use and test)
  • command line for managing your apps (deployment included)

At this point, I wanted to see some code, I went to the example on github and I was pleasantly surprised

//
	// A mango application is made of MangoModules and MangoApps
	// There really isn't any difference between a Module and an App
	// except that the server will load the MangoApp first. A mango
	// application can only have one MangoApp, but as many MangoModules
	// as you want.
	//
	public class MangoProject : MangoApp {

		public MangoProject ()
		{
			//
			// Routing can be done by using the Get/Head/Post/Put/Delete/Trace
			// methods. They take a regular expression and either a method
			// or another module to hand off processing to.
			//
			// The regular expressions come after the method/module, because
			// Mango uses params to allow you to easily register multiple
			// regexs to the same handler
			//
			Get (Home, "Home");

			//
			// The Route method will hand off all HTTP methods to the supplied
			// method or module
			//
			Route (new DocsModule (), "Docs/");
		}

		//
		// Handlers can be registered with attributes too
		//

		[Get ("About")]
		public static void About (MangoContext ctx)
		{
			//
			// Templates use property lookup, so you can pass in
			// a strongly typed object, or you can use an anonymous
			// type.
			//
			RenderTemplate (ctx, "about.html", new {
				Title = "About",
				Message = "Hey, I am the about page.",
			});
		}

		public static void Home (MangoContext ctx)
		{
			RenderTemplate (ctx, "home.html", new {
				Title = "Home",
				Message = "Welcome to the Mango-Project."
			});
		}
	}

  • Like the module and App ideas
  • like the simplicity of the code above
  • seems to work at a slightly lower level of abstraction than MVC but I can live with that
  • Interesting that it uses it’s own template engine, the syntax is nice, but I wonder why not spark, razor, nhaml, nvelocity. After all, It would be cool to use what I know already. Will have to find out.

Will have to do some more research on it


This is just how I organize folders and projects on my machine, there are endless possibilities , but I saw someone searching for it online and it did took me a long while before I settled on this, so I thought I would blog about it.

This can be a good or bad way of organizing, I think it depends on the kind and number of projects you are working on

Imagine you have a folder that contains the following folders

  • code (Real code, shipable, or company proof of concepts with its own repo)
  • spike (test to see if something works, Proof of concepts very basic)
  • third-party (OSS libraries and frameworks, etc)

Inside my code folder, each solution has it’s own Lib or Libs or Libraries folder (eg One Lib folder per solution ). The story is very different in spike since I put projects there for stuff I never ever want to ship then I have a shared Lib or Libs or Libraries folder where I put everything I ever needed. In third party I leave as I got from source control .

Inside the Libraries folder I mostly try to keep a folder per project and inside a folder per version, something similar to this

Capture

if in doubt copy the assembly, disk space is cheap.


I had a bit of trouble getting to this, so for self reference more than anything, I thought I d post about it.

Given the following class

    public class MyClass
   {
           private string _name;

           public string Name { get { return this._name;}}

           protected MyClass(string name)
          {
               _name = name;
          }
  }

I thought that the following would be the way to create an instance of it:

Activator.CreateInstance(typeof(MyClass), BindingFlags.NonPublic, null, new object[] { "MyName" }, CultureInfo.CurrentCulture);

However it throws a MissingMethodException: Constructor on type ‘ReflectionCreateInstance.MyClass’ not found. However if we add the Binding Flag for instance then it works.

[Fact]
public void When_creating_with_activator_Must_use_apropriate_binding_flags()
{
	string myname = "MyName";

	var myClass = (MyClass)Activator.CreateInstance(
					typeof(MyClass),
					BindingFlags.Instance| BindingFlags.NonPublic,
					null,
					new object[] { myname },
					CultureInfo.CurrentCulture);

	Assert.Equal(myname, myClass.Name);
}
If you try this with type.GetConstructor or type.GetConstructors the result is the same. 
Checking the documentation for BindingFlag.Instance I find 
Capture
So it was a case of RTFM I guess :D 


Follow

Get every new post delivered to your Inbox.