In MongoDB, there’s no way to lock a database, collection, or document. The ability to work without locking is a requirement for any db that wishes to be horizontally scalable, and obviously this imposes some limitations and/or possibilities (depending on your point of view :) ).

If you want all the goodness that document-orientation brings, it seems we need to cope with this non-locking database.

So how DO you update stuff in MongoDB? And, more importantly: how do you update stuff without race conditions?

In one of my previous posts on MongoDB, I mentioned that the unit of atomicity is a document – i.e., either a document gets saved/updated/deleted or it doesn’t. That must mean that we can count on updating one document only (or not), so we should build our applications so they can work without requiring multiple documents to be updated to be consistent (1).

First, let’s take a look at how to actually update a document.

Naïve attempt to update a document

Well, we could do this:

> use myblog
switched to db myblog
> var doc = {'headline': 'Just checking', 'tags': ['nifty']}
> db.posts.save(doc)
> // omgwtfbbq, we forgot to tag with 'test' as well... let's correct it:
> doc.tags[1] = 'test'
test
> // let's go get a cup of coffee...
> // ....
> // - aand now we're back - let's hit save
> db.posts.save(doc)
> db.posts.find()
{ "_id" : ObjectId("4b965229bf4a0000000043bc"), "headline" : "Just checking", "tags" : [ "nifty", "test" ] }

That is, if you go and save a document that already has an ID, any existing document with that ID will be updated.

This would work if we were the only client on the db. But what if someone was editing the post in that same moment, adding another tag as well? Well, if he was unfortunate enough to save his edits when we were out for coffee, his changes would be lost.

One way to actually do it

By using the update function!

update accepts the following four arguments:

  1. criteria – document selector that specifies which document to be updated
  2. objNew – document to save
  3. upsert – bool to specify auto-insert if document does not exist (“update if present, insert if missing”)
  4. multi – bool to allow updating multiple documents that match the criteria (default is only first document)

Actually, as you can now probably see, save(doc) is just a shorthand for update({}, doc, true, false) – an upsert with the document we’re saving.

This way, we could easily add an incrementing version field to our documents to make sure that the version we’re saving is the version we retrieved.

Let’s try it out:

> post = {'headline': 'has a version field', 'tags': ['nifty'], 'version': 1}
{
        "headline" : "has a version field",
        "tags" : [
                "nifty"
        ],
        "version" : 1
}
> db.posts.save(post)
> // now we're editing it
> post['tags'][1] = 'test'
test
> post['version']++
1
> post
{
        "headline" : "has a version field",
        "tags" : [
                "nifty",
                "test"
        ],
        "version" : 2,
        "_id" : ObjectId("4b9884d4b54e000000006c69")
}
> // now someone else retrieves the post
> someoneElsesPost = db.posts.findOne()
{
        "_id" : ObjectId("4b9884d4b54e000000006c69"),
        "headline" : "has a version field",
        "tags" : [
                "nifty"
        ],
        "version" : 1
}
> // and we save it, setting criteria to the version we retrieved
> db.posts.update({'version': 1}, post); db.$cmd.findOne({getlasterror: 1})
{ "err" : null, "updatedExisting" : true, "n" : 1, "ok" : 1 }
> // as you can probably tell, n:1 means that 1 document was updated...
> //
> // now that other guy makes an edit and tries to save it
> someoneElsesPost['tags'][1] = 'json'
json
> db.posts.update({'version': 1}, someoneElsesPost); db.$cmd.findOne({getlasterror: 1})
{ "err" : null, "updatedExisting" : false, "n" : 0, "ok" : 1 }
> // 0 documents were updated! Good!

- good thing he didn’t get through with that one! :)

As you can see, we can easily implement optimistic concurrency on each document by constraining updates to the version we checked out. But, as I will show next, you can actually do a lot of things on the server.

Server-side document updates

Instead of retrieving an entire document, modifying it, and saving it back again with the risk of overwriting someone else’s edits, we can ask the server to make edits as smaller operations. E.g. our attempt to add a missing ‘test’ tag to the document could have been done like this:

> db.posts.update({'_id': ObjectId("4b9884d4b54e000000006c69")}, { $push: { 'tags': 'test' } });
> db.posts.find()
{
        "_id" : ObjectId("4b9884d4b54e000000006c69"),
        "headline" : "has a version field",
        "version" : 2,
        "tags" : [ "nifty", "json", "test" ]
}

See how the code $push modifier was used to push a value into the array… this stuff is great. But here, we have a race condition again – what if someone added the ‘test’ tag almost the same time as we did? Then two ‘test’ tags would be present in the array.

One way is to constrain the update by id and the absence of ‘test’ in the tags array:

> db.posts.update({
    '_id': ObjectId("4b9884d4b54e000000006c69"), 
    $neq { 'tags': 'test'} 
}, { 
    $push: { 'tags': 'test' } 
});

- another is to use the $addToSet function, which makes MongoDB treat the array as a set:

> db.posts.update({'_id': ObjectId("4b9884d4b54e000000006c69") }, { $addToSet: { 'tags': 'test' } });

Nifty!

My conclusion (so far) is that an application can get a huge benefit from using the various modifier operations – performance-wise (obviously), but probably also UX-wise as well… It’s a step in another direction from the usual CRUD scenarios that I usually compulsively associate with the word “update”, and I imagine it could be made to reflect the user’s interactions with the system.

I am thinking that the majority of the user’s interactions with the system could (and probably should) be put on the form

db.collection.update({
        [assumed state before]
}, {
        [modifier operations to "migrate" one or
         more documents to the new state]
}, upsert, multi); db.$cmd.findOne({getlasterror: 1})

- and then one could implement a more-or-less generic mechanism on the client side to handle unsuccessful updates (e.g. by asking the user what to do then, reloading some data to allow [assumed state before] to be something else, or by handling certain situations with some kind of merging function, etc.).

How to perform updates across multiple documents

My first thought is that this situation should be avoided when working with a document-oriented db. I think most people will agree with this one.

I am pretty unsure of this, actually… The rest of this post is just a few thoughts on my first take on this, should I need to do this. Comments are greatly appreciated!

The problem in updating multiple documents is that we can perform an update on one document at a time, each time checking if the update went well or not. But there’s no way to (consistently) roll back update #1 if update #2 fails. So this means that there’s only one way: Forward! But how to proceed then, when an update failed?

How do we usually do stuff reliably across boundaries of multiple things that may or may not succeed, allowing us to handle errors as gracefully as possible and proceed thereafter?

I’m thinking that asynchronous reliable one-way messaging is the answer to this.

So if an application ever needs to update multiple documents in the most reliable way possible, it should probably perform one document update per “transaction” – in NServiceBus terminology that would be updating one document per message handler. And then the handler should throw an exception if an update unexpectedly fails.

But again: I’m thinking that this situation should be avoided at all costs with a document-oriented db. If ACID is required, the application should probably have a RDBMS on the side, or implement some kind of transactional mechanism in the document store.

Conclusion

That concludes my little learning series of MongoDB posts.

I must say that I am intrigued by all the NoSQL discussions currently going on in the communities, and I think it is always a sign of health that we question the technologies we use.

I am entirely convinced that document dbs could and should have been used for some parts of systems that I have experienced, and I am blown away by the lack of friction when starting up a project on top of a schemaless db.

As a .NET dude, I am convinced that the future will see more .NET systems built with more than one db underneath – e.g. with MongoDB for all the “soft parts” of the system, NHibernate on SQL Server for the few things that by nature require ACID, and then some NHibernate Search/Lucene/Solr.NET for full-text indexing and searching capabilities etc.

  1. Which is good practice anyway! In my experience, long and wide db transactions are often used, not to enforce a strict consistency as much as to allow scenarios like: “when this happens, this should also happen”. But that kind of logic can often be handled by something else, e.g. by a publishing events reliably to other processes (logically and/or physically), that handles the side-effects.

In my first post about MongoDB, I touched querying very lightly. Querying is of course pretty important to most systems, so it’s fair to dedicate a separate post to the subject.

Querying in MongoDB works by sending a document to the server, e.g. in the following snippet I create a document with a post ID

var somePost = db.posts.findOne({"_id": ObjectId("00112233445566778899aabb")})

- which can actually be even shorter, as the find and findOne functions accept an ObjectId directly as their argument, like so:

var somePost = db.posts.findOne(ObjectId("00112233445566778899aabb"))

But how can I find a post with a specific slug? Easy, like so:

var somePost = db.posts.find({slug: "this-slug-probably-comes-from-a-url"})

But how does this perform? It’s easy to examine how queries are executed with the explain() function, like so:

> db.posts.find({slug: "this-slug-probably-comes-from-a-url"}).explain()
{
        "cursor" : "BasicCursor",
        "startKey" : {
 
        },
        "endKey" : {
 
        },
        "nscanned" : 10000,
        "n" : 1,
        "millis" : 11,
        "allPlans" : [
                {
                        "cursor" : "BasicCursor",
                        "startKey" : {
 
                        },
                        "endKey" : {
 
                        }
                }
        ]
}

- yielding some info about the execution of the query. I don’t know exactly how to interpret all this, but I think I get that "nscanned": 10000 means 10000 documents were scanned – and in a collection with 10000 documents, that’s not entirely optimal as it implies a full table scan. Now, let’s make sure that our query will execute as fast as possible by creating an index on the field (_id is always automatically indexed):

db.posts.ensureIndex({slug:1})

Now lets explain() again:

> db.posts.find({slug: "post-no-454"}).explain()
{
        "cursor" : "BtreeCursor slug_1",
        "startKey" : {
                "slug" : "post-no-454"
        },
        "endKey" : {
                "slug" : "post-no-454"
        },
        "nscanned" : 1,
        "n" : 1,
        "millis" : 0,
        "allPlans" : [
                {
                        "cursor" : "BtreeCursor slug_1",
                        "startKey" : {
                                "slug" : "post-no-454"
                        },
                        "endKey" : {
                                "slug" : "post-no-454"
                        }
                }
        ]
}

Wow! That’s what I call an improvement!

What about posts with a specific tag? First I tried the following snippet, because I learned that the special $where field could be put in a query document to evaluate a predicate server-side:

var niftyPosts = db.posts.find({$where: function() { return this.tags != null && this.tags.indexOf("nifty") != -1; }})

- and this actually works. This syntax is sort of clunky though. Luckily, MongoDB provides a nifty mechanism for arrays that automagically checks if something is contained in it. So my query can be rewritten to this:

var niftyPosts = db.posts.find({tags: 'nifty'})

Nifty!

Now, to take advantage of indexes, the special query document fields should be used. I showed $where above, but there are more – to name a few: $gt (greater than), $gte (greater than or equal), $lt (less than), $lte (less than or equal), $ne (not equal), and many more. For example, to count the number of non-nifty posts in February 2010:

> db.posts.find({
    date: {$gte: 20100201, $lt: 20100301},
    tags: {$ne: 'nifty'}
}).count()
0

Check out the manual for some great documentation on available operators.

Conclusion

Querying with MongoDB actually seems pretty cool and flexible. I like the idea that it’s possible to execute ad-hoc queries, and for most usage I think the supplied operators are adequate. The ability to supply a predicate function via $where seems really cool, but it should probably only be used in conjunction with one or more of the other operators to avoid a full table scan.

This post will touch a little bit on the mechanism used for references, and then a few thoughts on how document-orientation relates to OO.

Now – if you, like me, are into OO and normalized object models – the weirdness begins….. or maybe not?! (actually, I am not sure yet :) )

In an OO world (and in a normalized RDB world as well), you reference stuff, thus reducing the amount of redundant information as much as possible. E.g. the names of countries should not be put in a column in your address table, each country should have a row in the countries table, and then be referenced by a countryId in the address table.

In a document-oriented world, you generally embed objects instead of referencing them. This is done for performance reasons, and because there’s no way to join stuff – which means that a stored ID/foreign key merely remains free for the client to manually use in additional queries.

When you do need to actually reference another document, use DBRef to create a reference, supplying the collection and the ID as arguments. E.g. like so:

> var author = {name: 'joe'}
> db.authors.save(author)
> author
{ "name" : "joe", "_id" : ObjectId("4b8ed200384e0000000065d8") }
> var post = {headline: 'hello there', author: new DBRef('authors', author._id)}
> db.posts.save(post)
> post
{
        "headline" : "hello there",
        "author" : {
                "$ref" : "authors",
                "$id" : ObjectId("4b8ed200384e0000000065d8")
        },
        "_id" : ObjectId("4b8ed4c8384e0000000065d9")
}

This way, the reference is represented in a consistent manner which may – or may not – be picked up by the driver you are using. The C# driver can create DBRefs and follow them, but you don’t get to join stuff – you still need an extra query.

Embedding objects may seem a little clunky at first, but actually this plays nicely with some common OO concepts – take aggregation, for example: a blog post has an array of comments, each of which makes no sense without an aggregating post – i.e. comments live and die with their post. That’s an obvious sign that comments should be embedded in the post. So, instead of:

posts:
{'_id': ObjectId('11223344556677889900aabb'), headline: 'A post'}
 
comments:
{'_id': ObjectId('bbaa00112233554477669988'), text: 'hello there', post: { "$ref": "posts", "$id": ObjectId('11223344556677889900aabb') }}
{'_id': ObjectId('881122335544776699aa00bb'), text: 'hello again', post: { "$ref": "posts", "$id": ObjectId('11223344556677889900aabb') }}

- you should do this:

{
    '_id': ObjectId('11223344556677889900aabb'), 
    headline: 'A post',
    comments: [
        {text: 'hello there'},
        {text: 'hello again'}
    ]
}

Actually this makes me think about the concept of an aggregate root in DDD: an aggregate root “owns” the data beneath it, and is responsible for maintaining its own integrity. If you were to delete an aggregate root, all the data beneath it would dissappear.

This also fits kind of nicely with the fact that there’s no database transactions in MongoDB – i.e. there’s no way to issue multiple statements and have them rolled back in case of an error – there’s only documents, and either a document gets inserted/updated/deleted, or it doesn’t. So obviously, the document is the unit of atomicity, which fits (sort of nicely) with the aggregate root and its responsibility of keeping itself internally consistent.

Conclusion

The observations stated here pretty much make a document an aggregate root in the DDD sense – especially since only documents get an _id. There’s no obvious way to reference a particular comment inside the second post shown above.

If MongoDB’s performance is up to the task, data should probably be aggregated as much as possible into large documents. MongoDB’s limit is 4 MB per document, but I am unsure of how large documents should be before you should consider splitting them.

Maybe I am thinking too much about these things? Maybe I should just try and build something and see where my document modeling goes? Suggestions and comments are welcome :)

Having experienced a lot of pain using RDBMSs (1) as a default choice of persistence, having read a couple of blog posts about MongoDB, and being generally interested in widening my horizon, I decided to check out MongoDB.

This post is a write-as-I-go summary of the information I have gathered from the following places:

More posts may follow…. :)

Getting MongoDB

Piece of cake! Download MongoDB from the download center and shove the binaries away somewhere on your machine. Default is for MongoDB to store its data in /data/db which translates to c:\data\db if you are using Windows – go ahead and create this directory. The MongoDB daemon can be started by running mongod.exe, which will accept connections on localhost:27017.

It will probably look something like the screenshot shown below.

An alternative data path can be specified on the command line, e.g. like so: mongod --dbpath c:\somewhere\else.

Accessing it with JavaScript

Run mongo.exe to start the Mongo Shell. It will probably look something like this:

In the MongoDB prompt, you can use JavaScript to access the db. Here’s a sample session of some commands I have found useful:

// lists the dbs in this mongo
> show dbs   
admin
local
>
> // change database context to some db ("myblog" - will automagically be created)
> use myblog   
switched to db myblog
>
> show collections
system.indexes
>
> // save a couple of documents in a collection named "posts"
> // (collection will be automagically created as well...)
> db.posts.save({
    headline: 'Notes to self about MongoDB', 
    slug: 'notes-to-self-about-mongodb', 
    tags: ['mongodb', 'nosql', 'nifty', 'c#']
 })
> db.posts.save({
    headline: 'Someday I want to check out CouchDB as well', 
    slug: 'someday-i-want-to-check-out-couchdb-as-well', 
    tags: ['couchdb', 'nosql', 'nifty', 'c#']
 })
>
> // show documents in a collection (returns a cursor, which will be iterated for the first 10 or 20 results - next pages can be retrieved with the 'it' command)
> db.posts.find()
{ "_id" : ObjectId("4b8e4281781b000000005cfc"), "headline" : "Notes to self about MongoDB", "slug" : "notes-to-self-about-mongodb", "tags" : [ "mongodb", "nosql", "nifty", "c#" ] }
{ "_id" : ObjectId("4b8e42cc781b000000005cfd"), "headline" : "Someday I want to check out CouchDB as well", "slug" : "someday-i-want-to-check-out-couchdb-as-well", "tags" : [ "couchdb", "nosql", "nifty", "c#" ] }

Now, I have successfully added two documents representing blog posts in a collection named posts. As you can see, MongoDB assigns some funky IDs to the documents.

> // lets get the first post ('find' and 'findOne' accept a query document as their first parameter)
> db.posts.findOne({'_id': ObjectId('4b8e4281781b000000005cfc')})
{
        "_id" : ObjectId("4b8e4281781b000000005cfc"),
        "headline" : "Notes to self about MongoDB",
        "slug" : "notes-to-self-about-mongodb",
        "tags" : [
                "mongodb",
                "nosql",
                "nifty",
                "c#"
        ]
}
>
> // now let's find all IDs ('find' and 'findOne' accept as their second parameter a document
> // specifying which fields to return)
> db.posts.find({}, {'_id': true})
{ "_id" : ObjectId("4b8e4281781b000000005cfc") }
{ "_id" : ObjectId("4b8e42cc781b000000005cfd") }
{ "_id" : ObjectId("4b8e4595781b000000005cfe") }
>

That was a brief demonstration of the JavaScript API in the Mongo Shell. Now, let’s do this from C#.

Getting started with mongodb-csharp

Now, go to mongodb-csharp dowload section at GitHub and get a debug build of the driver. Create a C# project and reference the MongoDB.Driver assembly.

On my machine, punching in the following actually works:

[Test]
public void CanAddPost()
{
  using(var mongo = new Mongo())
  {
    mongo.Connect();
 
    var db = mongo["myblog"];
    var posts = db["posts"];
 
    posts.Insert(new Document
                   {
                     {"headline", "Post added from C#"},
                     {"slug", "post-added-from-csharp"},
                     {
                       "tags", new[]
                                 {
                                   "c#",
                                   "nifty"
                                 }
                       }
                   });
  }
}

Now, I can verify that the document is actually in there by going back to the console and doing this:

> db.posts.find()
{ "_id" : ObjectId("4b8e4281781b000000005cfc"), "headline" : "Notes to self about MongoDB", "slug" : "notes-to-self-about-mongodb", "tags" : [ "mongodb", "nosql", "nifty", "c#" ] }
{ "_id" : ObjectId("4b8e42cc781b000000005cfd"), "headline" : "Someday I want to check out CouchDB as well", "slug" : "someday-i-want-to-check-out-couchdb-as-well", "tags" : [ "couchdb", "nosql", "nifty", "c#" ] }
{ "_id" : ObjectId("4b8e4b72091abb14e4000001"), "headline" : "Post added from C#", "slug" : "post-added-from-csharp", "tags" : [ "c#", "nifty" ] }
>

Nifty! Now lets show the posts from C#. On my machine the following snippet displays the headlines of all posts:

[Test]
public void CanShowPosts()
{
  using(var mongo = new Mongo())
  {
    mongo.Connect();
 
    var db = mongo["myblog"];
    var posts = db["posts"];
 
    Console.WriteLine("Posts");
    foreach(var post in posts.FindAll().Documents)
    {
      Console.WriteLine("    {0}", post["headline"]);
    }
  }
}

- which is documented in the following screenshot:

Random nuggets of information

Document IDs

All MongoDB documents must have an ID in the _id field, either assigned by you (any object can be used), or automatically by MongoDB. IDs generated by MongoDB are virtually globally unique, as they consist of the following: 4 bytes of timestamp, 3 bytes of machine identification, 2 bytes of process identification, 3 bytes of something that gets incremented.

As a nifty consequence, the time of creation can be extracted from auto-generated IDs.

The ID type used by MongoDB can be created with ObjectId('00112233445566778899aabb') (where the input must be a string representing 12 bytes in HEX).

How are documents stored?

I you have not yet figured it out, documents are serialized to JSON – with the minor modification that it’s a BINARY version of JSON, hence it’s called BSON.

String encoding

UTF-8. No worries.

What about references?

I will research this and do a separate post on the subject. As MongoDB is non-relational, a “join” is – in principle – an unknown concept. There’s a mechanism, however, that allows for consistent representation of foreign keys that may/may not give you some extra functionality (depending on the driver you are using).

What about querying?

I will research this as well, posting as I go.

OR/M? (or OD/M?)

It is not yet clear to me how to handle Object-Document Mapping. Will require some research as well. As an OO dude, I am especially interested in finding out what a schema-less persistance mechanism will do to my design.

What else?

More topics include applying indices, deleting/updating, atomicity, and more. Implies additional blog posts.

Conclusion

My first impression of MongoDB is really good. It’s extremely easy to get going, and the few error messages I have received were easy to understand.

I am especially in awe with how little friction I encountered – mostly because of the schema-less nature, but also because everything just worked right away.

  1. Usually because of abusing RDBMSs, actually. Storing an object model in a RDBMS is not painful as long as the tooling is right – e.g. by leveraging the amazing NHibernate. The pain comes when developers suddenly start implementing overly complex queries and doing reporting on top of a pretty entity model, modeling stuff OO style… ouch!

…but it does impose limitations on your domain model.

Most of these limitations, however, like the need for public/internal/protected members to be virtual, and the requirement for a default constructor to exist with at least protected accessibility, are not that hard to adhere to and usually don’t interfere with what you would do if there were no rules at all.

One of the limitations, however, can be pretty significant – Ayende describes the problem here, using the term “ghost objects”.

But, as I am about to show, this significance only arises if you follow a certain style of coding, which you should usually avoid!

Short explanation of the problem

When NHibernate lazy-loads an entity from the db (i.e. when you call session.Load<TEntity>(id) or when an entity in your session references something through a lazy-loaded association), it does so by providing an instance of a runtime-generated type, which acts as a proxy.

The first time you access something on the proxy, it gets “hydrated”, which is just a fancy way of saying that the data will be loaded from the database.

This would be fine and dandy, if it weren’t for the fact that the proxy is a runtime-generated subclass of your entity, which – in cases where inheritance is involved – will be a sibling to the other derived classes. Consider the simple inheritance hierarchy on the sketch to the right which in code could be something like so:

public abstract class LegalEntity
{
    public virtual Guid Id { get; set; }
}
 
public class Person : LegalEntity
{
    public virtual string FirstNames { get; set; }
    public virtual string LastName { get; set; }
}
 
public class Company : LegalEntity
{
    public virtual string CompanyName { get; set; }
}

- and then NHibernate will generate something along the lines of this (fake :) ) class signature:

public class LegalEntityProxy1234AndSomeMoreStuff : LegalEntity
{
   // ... secret stuff to access db in here
}

See the problem? Here’s the problem:

var legalEntity = session.Load<LegalEntity>(someKnownId);
Assert.IsTrue(legalEntity is Person
              || legalEntity is Company);  //< AssertionException! will never be Person or Company

This means that this kind of runtime type checking will fail in those circumstances where the entity is a lazy-loaded reference of the supertype, and the following will FAIL:

var legalEntity = session.Load<LegalEntity>(someKnownId);
if (legalEntity is Person)
{
    var person = (Person) legalEntity;
    return person.FirstNames + " " + person.LastName;
}
else
{
    // we know it's a company then, right? WRONG!
    var company = (Company) legalEntity;  //< InvalidCastException!
    return company.CompanyName;
}
One possible solution

The other day, Ayende blogged about a recent addition to NHibernate, namely lazy-loaded properties. This allows an entity to be partially hydrated, intercepting calls to certain properties to lazy-load the relevant fields on demand.

This feature is great when storing LOBs alongside the other fields on an entity, but it also laid the ground for his most recent addition, which is the ability to lazy-load an association by setting lazy="no-proxy" on it.

This way, NHibernate will not build a proxy, but instead it will intercept the property getter and load the entity at that point in time, thus being able to return the exact (sub)type of the loaded entity.

Now this seems to solve our problems, but let’s zoom out a bit … why did we have a problem in the first place? Our problem was actually that we failed to write object-oriented code, but instead we wrote a brittle piece of code that would fail at runtime whenever someone added a new subtype, thus violating the Liskov substitution principle. Moreover it just feels wrong to implement business logic that reflects on types!

What to do then?

Well, how about making your code polymorphic? The logic above could be easily rewritten as:

public abstract class LegalEntity
{
    public virtual Guid Id { get; set; }
 
    public abstract string Name { get; }
}
 
public class Person : LegalEntity
{
    public virtual string FirstNames { get; set; }
    public virtual string LastName { get; set; }
 
    public override string Name
    {
        get { return FirstNames + " " + LastName; }
    }
}
 
public class Company : LegalEntity
{
    public virtual string CompanyName { get; set; }
 
    public override string Name
    {
        get { return CompanyName; }
    }
}

- which moves the logic of yielding name as a oneliner into the class hierarchy, allowing us to always get a name from a LegalEntity.

What if I really really need a concrete instance?

Then you should use the nifty visitor pattern to extract what you need. In the example above, I would need to add the following additions:

public interface ILegalEntityVisitor
{
    void Visit(Person person);
    void Visit(Company company);
}
 
public abstract class LegalEntity
{
    // ...
 
    public abstract void Accept(ILegalEntityVisitor visitor);
}
 
public class Person : LegalEntity
{
    // ...
 
    public override void Accept(ILegalEntityVisitor visitor)
    {
        visitor.Visit(this);
    }
}
 
public class Company : LegalEntity
{
    // ...
 
    public override void Accept(ILegalEntityVisitor visitor)
    {
        visitor.Visit(this);
    }
}

This way, we’re taking advantage of the fact that each subclass knows its own concrete instance, thus allowing it to pass itself to the visitor we passed in.

This is the preferred solution when the logic you’re writing doesn’t belong inside the actual entitiy class, like e.g. when you want to convert the entity to an editable view object, because this will make your code break at compile time if someone adds a new specialization, thus requiring each piece of logic to handle that specialization as well.

Oh, and if you’re a Java guy, you might be missing the ability to create an inline anonymous visitor within the scope of the current method, but that can be easily emulated by a generic visitor, like so:

public class LegalEntityVisitor : ILegalEntityVisitor
{
    Action<Person> handlePerson;
    Action<Company> handleCompany;
 
    public LegalEntityVisitor(Action<Person> handlePerson, Action<Company> handleCompany)
    {
        this.handlePerson = handlePerson;
        this.handleCompany = handleCompany;
    }
 
    public void Visit(Person person)
    {
        handlePerson(person);
    }
 
    public void Visit(Company company)
    {
        handleCompany(company);
    }
}

- which would allow you to write inline typesafe code like this:

double risk = CalculateInitialRisk();
 
// multiply some number depending on type of legal entity
legalEntity.Visit(new LegalEntityVisitor(person => risk *= GetRiskExperienceForPeople(),
                                         company => risk *= GetRiskExperienceForCompany(company));

Only thing missing now is the ability to return a value in one line depending on the subclass. Well, the generic visitor can be used for that as well by adding the following:

 
public class LegalEntityVisitor : ILegalEntityVisitor
{
    // ...
 
    public static TResult Func<TResult>(LegalEntity legalEntity, 
                                        Func<Person, TResult> handlePerson, 
                                        Func<Company, TResult> handleCompany)
    {
        TResult result = default(TResult);
 
        legalEntity.Visit(new LegalEntityVisitor(p => result = handlePerson(p),
                                                 c => result = handleCompany(c)));
 
        return result;
    }
}

- allowing you to write code like this:

 
public string GetReportTypeCodeFor(LegalEntity legalEntity)
{
    return LegalEntityVisitor.Func(legalEntity, p => "P00000", c => "C" + GetReportingCode(c));
}

- and still have the benefit of compile-time safety that all specializations have been handled.

When to reflect on types?

IMO you should only reflect on types in business logic when it’s a shortcut that doesn’t break the semantics of your code. What do I mean by that? Well, e.g. the implementation of the extension method System.Linq.Enumerable.Count() looks something like this:

public int Count<T>(this IEnumerale<T> items)
{
    if (items is ICollection<T>)
    {
        return ((ICollection<T>)items).Count;
    }
 
    var count = 0;
    // iterate and count manually
 
    return count;
}

This way, providing the number of items is accelerated for certain implementations of IEnumerable<T> because the information is already there, and for other types there’s no way to avoid manually counting.

Conclusion

I don’t think I will be using the new lazy="no-proxy" feature, because if I need it, I think it is a sign that my design has a bad smell to it, and I should either go for polymorphism or using a visitor.

Jan 152010

…web development changed forever! (to me at least :) )

It’s amazing how the CSS-selector-based approach has proven its effectiveness and durability, and combined with the incredible wealth of extensions, choosing jQuery is simply a no-brainer.

By the way, jQuery 1.4 has been released. Check out this post for a walkthrough of some of the new features.

Short preface: at a job interview, Zach Cox was told to aggregate words and word counts from a bunch of files into two files, sorted alphabetically and by word count respectively, which he did in Ruby and Scala. This led Lau Bjørn Jensen to do the same thing in Clojure, which apparantly sparked other people to do it in Java, Python etc.

Inspired by the aforementioned problem, and an extended train ride home (thank you, Danish National Railways!!), I decided to see what a C# (v. 3) version could look like:

namespace NewsReader
{
  using System;
  using System.IO;
  using System.Linq;
  using System.Text.RegularExpressions;
  using System.Diagnostics;
 
  class Program
  {
    static void Main()
    {
      const string dir = @"c:\temp\20_newsgroups";
      var stopwatch = Stopwatch.StartNew();
      var regex = new Regex(@"\w+", RegexOptions.Compiled);
 
      var list = (from filename in Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories)
                  from match in regex.Matches(File.ReadAllText(filename).ToLower()).Cast<Match>()
                  let word = match.Value
                  group word by word
                  into aggregate
                    select new
                             {
                               Word = aggregate.Key,
                               Count = aggregate.Count()  ,
                               Text = string.Format("{0}\t{1}", aggregate.Key, aggregate.Count())
                             })
        .ToList();
 
      File.WriteAllLines(@"words-by-count.txt", list.OrderBy(c => c.Count).Select(c => c.Text).ToArray());
      File.WriteAllLines(@"words-by-word.txt", list.OrderBy(c => c.Word).Select(c => c.Text).ToArray());
 
      Console.WriteLine("Elapsed: {0:0.0} seconds", stopwatch.Elapsed.TotalSeconds);
    }
  }
}

Weighing in at 36 lines and executing in 10.2 seconds (on my Intel Core 2 laptop with 4 GB RAM), I think this is a pretty clear and performant alternative to the other languages mentioned.

tomatoday logoAs mentioned in my previous post, my colleague Troels Richter is working on a Pomodoro application. What I did not say was that it is actually already available for people to try out – it’s called Tomatoday, it’s based on Silverlight 3 (SL4 version on the way), and it can be found at www.tomatoday.dk.

It consists of an online activity inventory and a simple timer application that is pretty nifty when run out-of-browser. It will of course be even niftier when it gets fully ported to Silverlight 4 when the timer gets to run in chrome-less out-of-browser mode.

If you try out the application, and you have suggestions or ideas, we will be very grateful if you submit them to Tomatoday’s forum at Uservoice.

pomodoro-techniqueAt Trifork where I work, one of the hot new things is The Pomodoro Technique, as it seems more and more of my colleagues are experimenting with it. If you don’t know anything about it, I can tell you (in my own words) that it’s a personal mini-process to make you more productive, thus more happy and fulfilled.

It goes like this (from www.pomodorotechnique.com):

  1. Choose a task to be accomplished
  2. Set the Pomodoro to 25 minutes (the Pomodoro is the timer)
  3. Work on the task until the Pomodoro rings, then put a check on your sheet of paper
  4. Take a short break (5 minutes is OK)
  5. Every 4 Pomodoros take a longer break

- where tasks are chosen from your “todo today”, which you assemble in the morning by picking tasks from your “activity inventory”. There’s a few more tricks in it, e.g. a form of notation that fits the process well and how to track disturbances.

Having done this for little more than one month, I think I can safely say that it is almost guaranteed to either

  • make you more productive
  • make you very conscious about why you’re not that productive

focus-boosterPractitioners of the technique usually prefer to use a real egg timer (in the shape of a tomato of course), because of the tactile feedback you get from actually manipulating a physical object – but as I am sitting in an open office with 6 other developers, and some of them are doing pomodoros as well, we are using the next best thing: a Pomodoro timer app.

Right now I am using the Focus Booster app, because it’s pretty and doesn’t take up that much space on the screen. One of my colleagues, Troels Richter, is currently working on a more complete Pomodoro app (in Silverlight) that will help in all the aspects of the Pomodoro Technique.

NServiceBus for dummies who want to be smarties 6… sixth and last post in the series will show an example using the publish/subscribe functionality provided by NServiceBus.

One could image all kinds of cool integration scenarios where interested parties somehow receive events from our system regarding stuff that may be interesting to them. Let’s imagine that our user registration from the fifth post is an extremely interesting event to the sales department and the Über Boss of our enterprise.

To accomodate this, I create a separate assembly meant for holding messages that are not private to our application. This way, I can easily distinguish between messages that I can change whenever I feel like it, and messages that I must put more care and effort into crafting, because they will most likely live forever when a dozen applications in our enterprise are suddenly depending on them.

This means that my saga from the fifth post will handle an email confirmation like this:

    public void Handle(ConfirmRegistration message)
    {
      Console.WriteLine("Confirming email {0}", Data.Email);
 
      NewUserService.CreateNewUserAccount(Data.Email);
 
      MailSender.Send(Data.Email,
                      "Your registration request",
                      "Your email has been confirmed, and your user account has been created");
 
      Bus.Publish(new SomeoneActuallyRegistered {Email = Data.Email});
 
      MarkAsComplete();
    }

As you can see, I Bus.Publish(...) a message saying that someone with a particular email has registered.

To be able to publish stuff, some kind of subscription storage must be configure for the endpoint. Therefore, I change my backend’s EndpointConfig to be configured with this:

      Configure.With()
        .CastleWindsorBuilder(container)
        .Sagas()
        .NHibernateSagaPersisterWithSQLiteAndAutomaticSchemaGeneration()
        .MsmqSubscriptionStorage()
        .XmlSerializer();

- which means my backend will be storing subscriptions in a message queue.

If you are somehow unclear on the difference between sending and publishing, I can tell you that the conceptual difference is this: sending is like saying “do this stuff”, whereas publishing is like saying “this stuff happened” – see the difference in tense?

Functionally, the difference is that sending will put a message in the queue specified as recipient for that particular message whereafter one recipient will consume that message off its input queue (possibly competing with others), whereas publishing will put a message in the input queue of each one of whoever subscribed to that particular message (possibly many – possibly zero).

Now, to simulate the sales department and the über boss, I create two new projects containing an app.config that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
  </configSections>
 
  <MsmqTransportConfig InputQueue="salesDept"
                       ErrorQueue="error"
                       NumberOfWorkerThreads="1"
                       MaxRetries="5" />
 
  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="PublicMessages" Endpoint="backend" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
</configuration>

- and similarly for the über boss.

Note how a subscriber must have a message endpoint mapping telling where messages will be published – in the example above, my mapping specifies that all messages from my PublicMessages assembly can be subscribe to on the endpoint named “backend”. And obviously, a subscriber must have an input queue, through which subscribed messages will be received.

The EndpointConfig can look like this:

namespace SalesDept
{
  public class EndpointConfig : 
    IConfigureThisEndpoint, 
    AsA_Server, 
    IWantCustomLogging, 
    IWantToRunAtStartup
  {
    public IBus Bus { get; set; }
 
    public void Init()
    {
    }
 
    public void Run()
    {
      Bus.Subscribe<SomeoneActuallyRegistered>();
 
      Console.WriteLine("Sales department running...");
    }
 
    public void Stop()
    {
    }
  }
}

- and similarly for the über boss as well.

Note how the service subscribes to the messages that it wants to receive by calling Bus.Subscribe<...> in the Run() method. What happens, is that NServiceBus somehow communicates to whoever is configured as the publisher of messages of type SomeoneActuallyRegistered, that messages should be sent to this service’s input queue.

And then, I implement a message hander in each service… first, the sales department:

namespace SalesDept
{
  public class SendInformationAboutProducts : IMessageHandler<SomeoneActuallyRegistered>
  {
    public void Handle(SomeoneActuallyRegistered message)
    {
      Console.WriteLine("Sending stupid emails to {0}", message.Email);
    }
  }
}

and then, the über boss:

namespace ÜberBoss
{
  public class NoteEmailInLittleBlackBookOfEvilSchemes : IMessageHandler<SomeoneActuallyRegistered>
  {
    public void Handle(SomeoneActuallyRegistered message)
    {
      Console.WriteLine("Noting email {0}", message.Email);
    }
  }
}

To make this run, I go to “Set StartUp projects…” of my solution and make all my services start up when I press F5.

Picture showing how to configure multiple startup projects

Now, we can check things out by pressing F5, thus running the backend, the sales department, and the über boss at once. On my machine, it looks like this:

Running three services at once

Now it doesn’t take too much imagination to come up with all kinds of cool solutions to integration scenarios.

Conclusion

That concludes the sixth and last post in my “NServiceBus for dummies who want to be smarties” series. I hope the series can provide some information on how to get started using NServiceBus, and perhaps fill out some of the gaps that comes from documentation in the form of sparsely available blog posts from different sources.

© 2010 mookid on code Suffusion WordPress theme by Sayontan Sinha