RSS 2.0
# Tuesday, November 10, 2009

The NoSql movement is  gaining more and more inertia. A year or two ago I took a look at CouchDb but this time I wanted to try MongoDB. I have been hearing a lot of nice things about MongoDB, mostly about how simple is to query it and how fast is. So I decided that it was time to actually take it for a spin.

A quick search point me to this odetocode post that was a great starting point to have Mongo running in my dev machine using the defaults and the Driver for C#. So, go and read K. Scott Allen’s post if you want to follow along with the code in the rest of this post.

Detour (some background on NoSql and MongoDB):

If you already know about this you can skip all this paragraph.

The so called NoSql movement is based on the notion that using RDBMS may not be the right choice in some cases. They propose using alternative databases, like key-value pair storages and document databases. To perform operations on the data some make use of Map-Reduce, others like MongoDB use a query mechanism that resembles Sql. One of the most attractive features of this db’s is the ability to easily run clusters and do data sharding. Most of them support both features without a lot of fuss, or that I heard.

MongoDB is a document based db, that stores the information as Binary Json.

End of detour.

What’s a document?

A document is not the equivalent to a Row. A document can be seen as a key-value pair collection of objects. It’s actually a complete object Graph, persisted as Json. Documents are organized into Collections.

Now, to be usable in C# we need to have some mapping from the Document object to our object graph. We could use the Document directly but will be like going back to use DataTables, not very pretty.

  1: var movieName = movie["Name"].ToString();
  2: var actors = JsonConvert.DeserializeObject<Dictionary<int,string>>(movie["Actors"].ToString());

The simple way to go from Json to an object should be to pass the load of a document to a JsonDeserializer like Json.Net and just get the object back, like this:

  1: var movie = JsonConvert.DeserializeObject<Movie>(document.ToString());

But while trying to do so I got an error in the id of the document. Looking at the paymoad of the document the Id looks like this:

  1: "_id": ObjectId("4eebf0006829c72c1d000000")

The problem is in the ObjectId function. But do not despair, there is an alternative.

From a document to a fully usable object graph.

The alternative is not pretty but works and is easy to implement. Just pass the document to the root of your object graph, an access the underlying collection via the objects properties. Let’s see how this can look like. (Note: Yes there are some hardcoded strings in there that should be removed. I’m also never closing the connection to the Db, when I probably should).

  1: using System;
  2: using System.Collections.Generic;
  3: using Newtonsoft.Json;
  4: using MongoDB.Driver;
  5: 
  6: public class Repository
  7: {
  8:     private Database _db;
  9: 
 10:     public Database Db()
 11:     {
 12:         if(_db != null) return _db;
 13:         var server = new Mongo();
 14:         server.Connect();
 15:         _db = server.getDB("DynamicProgrammer");
 16:         return _db;
 17:     }
 18: 
 19:     public void Insert(Document document, string collectionName)
 20:     {
 21:         var collection = Db().GetCollection(collectionName);
 22:         collection.Insert(document);
 23:     }
 24: 
 25:     public IEnumerable<TDocument> getListOf<TDocument>(string whereClause, string fromCollection) where TDocument : IMongoEntity
 26:     {
 27:         var docs = Db().GetCollection(fromCollection)
 28:             .Find(whereClause).Documents;     
 29:         return docsToCollection<TDocument>(docs);
 30: 
 31:     }
 32: 
 33:     private IEnumerable<TDocument> docsToCollection<TDocument>(IEnumerable<Document> documents) where TDocument : IMongoEntity
 34:     {
 35:         var list = new List<TDocument>();
 36:         var settings = new JsonSerializerSettings();
 37: 
 38:         foreach (var document in documents)
 39:         {
 40:             var docType = Activator.CreateInstance<TDocument>();
 41:             docType.InternalDocument = document;
 42:             list.Add(docType);
 43:         }
 44:         return list;
 45:     }
 46: 
 47: }
 48: 

IMongoEntity is a simple interface to force our entities to expose the Doc property.

  1: public interface IMongoEntity
  2: {
  3:     Document InternalDocument { get; set; }
  4: }
  5: 

And our Movie Entity will look like:

  1: public class Movie : IMongoEntity
  2: {
  3:     public string Name
  4:     {
  5:         get { return InternalDocument["Name"].ToString(); }
  6:         set { InternalDocument["Name"] = value; }
  7:     }
  8: 
  9:     public int ProductionYear
 10:     {
 11:         get { return (int)InternalDocument["Year"]; }
 12:         set { InternalDocument["Year"] = value; }
 13:     }
 14: 
 15:     public Dictionary<int, string> Actors
 16:     {
 17:         get { return JsonConvert.DeserializeObject<Dictionary<int, string>>(InternalDocument["Actors"].ToString()); }
 18:         set { InternalDocument["Actors"] = value; }
 19:     }
 20: 
 21:     public Document InternalDocument { get; set; }
 22: }

Notice I’m using JsonConverter to return a complex type, we should probably encapsulate that functionality in some kind of base class, maybe convert IMongoEntity into an abstract class that provides some basic utilities.

I will try to dig deeper into MongoDB, seems to be really suited to a Domain first approach. I wasn’t able to have anything but the most simple queries working, so I will have to take a closer look at the syntax.

kick it on DotNetKicks.com Tuesday, November 10, 2009 12:04:54 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [4] - Trackback
Programming | Tools
# Sunday, November 08, 2009
kick it on DotNetKicks.com Sunday, November 08, 2009 12:43:56 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback

# Wednesday, October 28, 2009

The tag line of the book is refactor your wetware, and after reading it, you will.

Andy Hunt explores how the brain works and try to point us (the readers) into ways to improve our use of it.

For me the first five parts; Introduction, Journey from novice to expert, This is your brain, Get in your right mind and Debug your brain; is where I found the book most useful. This is not to say that the last four parts weren’t interesting, but for whatever the reason I took some effort on my part to concentrate and finish them.

The book had several a-ha moments. The Dreyfus model was specially interesting and the idea of carry around a pad to write any idea you may have has serve me well so far. (Note: My journalism teachers all insisted that we always carry a pad to take notes and make observations, but I never extended the practice as a developer.)

If you are into continuous improvement this is a book that you should read. I’m sure you will find some or all of it useful.

kick it on DotNetKicks.com Wednesday, October 28, 2009 4:31:00 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Books
# Sunday, October 25, 2009

I got my tickets the first week the conference was announced, not really knowing what to expect but exited on the opportunity to learn new things and talk to people that work in other languages/technologies beside the .net world.

Once the sessions were announced my excitement decreased a little bit.

Friday morning was miserable in the weather department, so going to the conference was a nice proposition. I print my ticket, pack my laptop into the bag and took the TTC. I arrive a little after 8:00, the registration process was fast and everything was really well organized. There was coffee, water and muffins in the breakfast area. Good vibes in the air and a general feeling of excitement in the public. I crossed path with some faces I know from other similar gatherings while getting my second cup of java of the day.

After entering the auditorium Joel Spolsky addressed the audience with a key note about designing software products. He talk about how to make good products, the constant struggle between simplicity and features and how to achieve a balance. It was a vey interesting keynote.

The first session was on Asp.Net MVC, Joe DeVilla and Barry Gervin give us a good overview. Nothing knew to learn for me, but the presentation was good, and a lot of the developers in the audience weren’t .net developers.

After each brake, the conference resumed with some video from FogCreek, short but good, presenting some aspects of FogCreek or software development in general.

The other session I enjoyed and the one that justified the ticket price was the one by Greg Wilson. He is not just an excellent presenter but he also has something to say. He talked about doing scientific analysis of development methodologies and use real data to make decisions on what practices are really beneficial to our profession.

As a summary: one of five sessions + the keynote were relevant for me. For $99 it’s not bad value and I probably will go again next year if they decide to come back to T.O. Just hoping they rise the level of the sessions, maybe 2 at the 100 level, 2 at the 200 level and 1 at the 300 level.

kick it on DotNetKicks.com Sunday, October 25, 2009 8:14:02 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
General
# Thursday, October 15, 2009

Some time ago I heard about the intent of putting together a package manager for .net ala gems or pear. I wasn’t aware that this project was actually under way. The name of the project, Horn.

Please check the links in the project home page on how to get started with Horn. It’s very simple and actually works as promised. One thing you need to remember is that you will have to install a subversion client and a Git client for Horn to check out the source code from the projects you want to build.

I had two issues in my Win7 machine, where I tested. The first issue is related to Git really and not to Horn and/or Win7. Make sure that you have the path to your git.cmd into the PATH of your computer.

It’s usually %PROGRAMFILES%/Git/cmd, if you don’t when trying to do a horn –install on a project using Git, Windsor (the Ioc container used by Horn) will throw an exception.

The other issue seems to be a concurrency issue when trying to read a Temp file. Not sure how this files is been created just yet. This happens while trying to build complex projects like MvcContrib. Re-issuing the install command will “resume” the build. It took me three attempts to build MvcContrib.

Even with this issue (that may not be a Horn issue at all) I think that this is a very important project for the .Net community. It makes building this complex projects with all the dependencies a routine task. You don’t have to spend hours tracking down the project, the source code and pray that everything builds and works well together.

kick it on DotNetKicks.com Thursday, October 15, 2009 9:19:19 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Frameworks | Programming | Tools
# Thursday, October 01, 2009

The other day at the office my boss took a look at our configuration files and he expressed his concern about breaking DRY with all the ConnectionStrings. One for Ado.Net, one for NHibernate, one for our caching db and another for Log4Net. Not only that but they are all over the place in the file, not all together.

On top of that,, some of those are the same, so changing a username or a password means changing them all over. Of course he was right.

NHibernate solution

For NHibernate I just use the connection.connection_string_name property instead of the connection.connection_string. This points to a connection string defined in that section of the configuration file with the provided name.

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
	<session-factory>
		<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
		<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
		<property name="connection.connection_string_name">ConnectionString</property>
		<property name="show_sql">true</property>
		<property name="use_proxy_validator">false</property>
	        <mapping assembly="ACME.Model"/>
	</session-factory>
</hibernate-configuration>

Log4Net solution

For Log4Net was not so easy. I found references to a patch that provides similar functionality to the one observed in NHibernate and references to that patch been applied to the latest release, but It doesn’t seem to be there.

So the solution I came with is to extend the default AdoNetAppender and in the constructor of the new class try to read the connection string from the configuration file, the only problem with this approach is that I’m hard coding the expected name of an AppSetting that will contain the name of the connection string.

Note: Utils.GetAppSetting and Utils.GetConnectionString are utility methods that I use to read from the configuration files.

using log4net.Appender;
namespace ACME.Logging
{
	public class CustomAdoNetAppender : AdoNetAppender
	{
		public CustomAdoNetAppender()
		{
			var stringKey = Utils.GetAppSetting("Log4NetConnectionStringName");
			if (string.IsNullOrEmpty(stringKey)) return;
			var connectionString = Utils.GetConnectionString(stringKey);
			if (!string.IsNullOrEmpty(connectionString)) ConnectionString = connectionString;
		}
	}
}

In your configuration file you need to have and appSetting that look like this:

<add key="Log4NetConnectionStringName" value="ConnectionString"/>

Them you will need to tell Log4Net to use this appender for logging so in your log4net.config file you should do something like this:

<appender name="AdoNetAppender" type="ACME.Logging.CustomAdoAppender">

kick it on DotNetKicks.com Thursday, October 01, 2009 9:16:18 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Programming
# Saturday, September 19, 2009

Hopefully gone are the days were you considered ok the use of single letter names for your variables or your arguments and you are giving informative names to all the artifacts in your code.

But what about generics? What about the <T> in them? Are you paying attention? More and more you will see that programmers are shying away from the single letter placeholder and are using more explanatory names. If you look at the code in FubuMvc you will see things like this.

public TFlashModel Load<TFlashModel>() where TFlashModel : class, new()

That is more informative than Load<T>.
I used to do the single letter naming for generics in my code, but I’m trying to avoid it as much as possible today. The same way I won’t tolerate bad names for my functions anymore. I still have a problem with the T in there. I was also doing that, but lately I have dropped the T altogether as well. I see the use of the T as a kind of Hungarian notation.

In the Lambdas department the single letter variables are all over the place.

var cities = citiesRepository.GetAll();
cities.Select(c => c.Name.Length > 10);

Why “c”? Why not say?

cities.Select(city => city.Name.Length > 10);

This is a simple example, where c may be sufficient since you are properly naming the collection (cities) but I still think there is something there, specially when you have longer expressions. And certainly don’t use x. Like in

cities.Select(x => x.Name.Length > 10);

What’s x? What do you think?

Why should you care?

Making your code readable will make your code better. When you have to think about the proper name for a variable or a method, you also have to think about what that method do and what that variable represents. You can catch possible errors or misconceptions just because the name is not right. It will make your methods shorter and will help you to make your methods do just “one thing and one thing only” (since is very difficult to fins a name for something that does to many things). Oh, by the way, just in case you didn’t get the memo, anything with the word manager in it, is wrong.

Oh, I forgot, maybe the most important think of all. In a year from now when you need to do some maintenance in the code, you will be up and running in seconds and not pulling your hair of trying to understand what the hell you were thinking when you wrote that. And that, my friend, is a good thing.

kick it on DotNetKicks.com Saturday, September 19, 2009 7:02:24 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback

# Thursday, September 10, 2009

Last week I was reading Alistair’s post about the Toronto Code Retreat. I was planning to attend but I had plane tickets for the same day so I miss the opportunity, what I regret. In that post Alistair refers to the book The Passionate Programmer by Chad Fowler. Next you know, I was ordering the book from Amazon.

On Saturday I pick it up from the post office and next day I was sitting in the backyard going through the pages while preparing a BBQ. The book reads fast. Its divided in Five sections and each section consist of a series of very short chapters that, most of the time, are no more than two or three pages .

I found that I’m doing a lot of the things that Chad recommends and I identify myself with some of the situations he mentions in the book. I particularly enjoyed the section about marketing yourself something I’m not good at doing.

There are a few chapters that I market down to re-read later on, not because they are so deep that need a re-write, but mostly because are the ones that talk to my weakness and the stuff that I need to remind myself to work on to improve.

Sparkled along the book there are easies from some renowned programmers on how they build their careers. They make for an enjoyable and fun read, and provide a break from the more “to the point” writing that Chad uses.

All in all a very enjoyable book that can provide some help on achieving exactly what the tag line for the book is “creating a remarkable career in software development”.

kick it on DotNetKicks.com Thursday, September 10, 2009 6:25:51 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Books
I think this has been the longest hiatus since I started the blog. I have been busy with life, talking some time of, finishing tons of renovations at home and re-focusing my priorities from a development point of view.

I'm looking forward to renew the posting pretty soon with the usual frecuency of one to two post a week.

kick it on DotNetKicks.com Wednesday, September 09, 2009 11:13:43 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
General
Add The Dynamic Programmer Mippin widget
Navigation
Archive
<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
What I'm reading
Shelfari: Book reviews on your book blog
About the author/Disclaimer
Hernan Garcia I have been a software developer for the last 16 years or so.
I was a journalist before and I still consider myself one.
Besides baseball, programming is my other big passion.

Me on twitter. @TheProgrammer
Certified Scrum Master
© Copyright 2010
Hernan Garcia
Sign In
Statistics
Total Posts: 198
This Year: 16
This Month: 2
This Week: 1
Comments: 70
Themes
Pick a theme:
All Content © 2010, Hernan Garcia