RSS 2.0
# Wednesday, March 10, 2010

Yesterday we created our first method in the MongoDbProvider, our implementation of BlogProvider. We created a few supporting classes, but we don’t have test for those classes. We recognize that we went a little bit too far in our coding. We got carry away and we started to implement a little bit more than needed to make the test pass.

So let’s fix that. First we need to see our first test passing. We run it expecting to fail to save and load the post but we have a different Exception thrown.

server_null_failing_test

If we look at the code we notice that we made a big mistake in the Mongo class. We declared a _server private field but we are initializing a local server variable.
So when calling Disconnect on _server inside the Dispose method we get the NullReferenceException.
Let’s write a test to reproduce that bug at the unit level and see what else we can fix in that class.

Looking at it we discover a few dependencies that can be brake. First we create an IMongoMapperFactory interface and we make MongoMapperFactory to implement it.
There is another dependency, the name of the database to use. We made both parameters for the constructor inverting the dependencies.

  1:         public MongoDb(IMongoMapperFactory mongoMapperFactory, string dbName)
  2:         {
  3:             _mongoMapperFactory = mongoMapperFactory;
  4:             _dbName = dbName;
  5:         }

We also changed the Insert method:

  1:         public void Insert<TEntity>(TEntity entity)
  2:         {
  3:             var document = _mongoMapperFactory.GetMapper<TEntity>().Map(entity);
  4:             Db(db=> db.GetCollection(entity.GetType().Name+"Docs").Insert(document));
  5:         }

Notice that the private Db method now takes an Action<Database>

  1:         private void Db(Action<Database> action)
  2:         {
  3:             using (var server = getServer())
  4:             {
  5:                 var db = server.getDB(_dbName);
  6:                 action.Invoke(db);
  7:             }
  8:         }
  9: 
 10:         private Mongo getServer()
 11:         {
 12:             var server = new Mongo();
 13:             server.Connect();
 14:             return server;
 15:         }

And the newly created getServer() helper method to clean up the code. We also made some changes on the query method but I will leave that for the next post.

Our passing test result indicates some success.

passing_test

Next: Mapping from Document to Entity and back.

kick it on DotNetKicks.com Wednesday, March 10, 2010 2:00:16 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
MongoDB | Programming
# Monday, March 08, 2010

The code in this post is of demo quality and should not be taken as production ready code. It’s a first take on the project and will evolve during a series of posts.

Blogengine.net is an open source blogging platform written in asp.net. The engine comes with two Data store providers a DB provider and an XML provider. Since the engine is using the provider model, it should be fairly easy to write a new data store provider.

As you may have noticed by some of my previous posts, I’m really interested in alternatives to relational databases, the so called NoSql movement.

Getting all the pieces together.

First download the Blogengine.net source code from codeplex.com, a mongodb driver for .net and mongodb.

At the moment I’m aware of two drivers for .net, the more mature mongodb-csharp and a more resent one NoRM. NoRM is trying to provide a more OO experience, avoiding the use of strings as much as possible and embracing Linq, it’s still very young but very promising. You can check an article by Rob Connery to learn some of the capabilities. We will be using mongodb-csharp in this example. (Build from 2010-03-01).

Be certain of download the proper version of MongoDB for your OS 32 or 64 bits.

Installing MongoDB

Unzip the files downloaded into a folder of your choice. I’m using c:\MongoDB.

Create a data and db folders under c, you should have something like c:\data\db

Run mongod.exe from the mobogodb bin folder.

c:\MongoDB\bin>mongod.exe
mongod.exe --help for help and startup options
Fri Mar 05 23:09:29 Mongo DB : starting : pid = 0 port = 27017 dbpath = /data/db/ master = 0 slave = 0  64-bit
Fri Mar 05 23:09:29 db version v1.2.4, pdfile version 4.5
Fri Mar 05 23:09:29 git version: 5cf582d3d96b882c400c33e7670b811ccd47f477
Fri Mar 05 23:09:29 sys info: windows (6, 0, 6002, 2, 'Service Pack 2') BOOST_LIB_VERSION=1_39
Fri Mar 05 23:09:29 waiting for connections on port 27017

To stop the daemon just type Ctrl+c.

Setting up the project.

You can download the project as I have it set from my codeplex repo using Mercurial to save you some time.(Changeset 59b63f72169f)

What I did was to create a new folder \lib under the solution root (of the BlogEngine.Net code), added Nunit, Rhino Mocks and the MongoDb drivers libraries. Them from within VS I added an Specs project to hold my test and a second project to hold the code for the provider.

You should have something like this right now.

initial_solution_set_up

And the following directory structure.

initial_dir_structure

You may not have the Resharper files and folders, that depends if you have Resharper installed or not.

Lets write the first test for our MongoDBProvider and generate the class, it needs to inherit from BlogProvider.

Even when I have the source I’m treating BlogEngine.Net as a close system. This change a little bit the way I will develop such a provider. I have control of the code there are a bunch of stuff that I will probably refactor or move around. But in this case I can’t since I don’t want to fork the project.

The first test will be to insert a new post. The code will look something like this.

        public override void InsertPost(Post post)
        {
            using (var db = new MongoDB())
            {
                db.Insert(post);
            }
        }

Let’s take a look at the MongoDB class.

  1:     public class MongoDB : IDisposable
  2:     {
  3:         private Mongo _server;
  4:         private Database _db;
  5: 
  6:         public MongoDB()
  7:         {
  8:             _server = new Mongo();
  9:         }
 10: 
 11:         private Database Db()
 12:         {
 13:             if (_db != null) return _db;
 14:             var server = new Mongo();
 15:             server.Connect();
 16:             _db = server.getDB(BlogSettings.Instance.Name.Replace(" ", "_"));
 17:             return _db;
 18:         }
 19:         public void Insert<TEntity>(TEntity entity)
 20:         {
 21:             Document document = DocumentMapperFactory.GetMapper<TEntity>().Map(entity);
 22:             Db().GetCollection(entity.GetType().Name+"Docs").Insert(document);
 23:         }
 24:         
 25:         public void Dispose()
 26:         {
 27:             _server.Disconnect();
 28:             _server.Dispose();
 29:         }
 30:     }

In line 27 I made sure we disconnect from the db. Note in line 21 that I’m getting a mapper from a factory. This is an obvious requirement since we need a mechanism to map from our objects to the documents and back.

In the next article we will take a look at the Mapper class for Post, save and update our first entity.

kick it on DotNetKicks.com Monday, March 08, 2010 11:31:11 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
MongoDB | Programming
# Tuesday, March 02, 2010

If you use Github (Git) or Bitbucket (Mercurial) you are familiar with the ability to fork a project into your own repository in those sites.

 github_fork

bitbucket_fork

 

Once forked you can start working in your own repo and once you are ready to integrate you will send a pull request to the master repository.
Codeplex recently started using Mercurial but it doesn’t have (or I couldn’t find) a way to clone a project directly from inside Codeplex.

UPDATED March 5: Yesterday Codeplex added support for forks, check this article here.

First, cloning to your local computer

You need to have TortoiseHg installed on your computer before going forward with this tutorial.

First you need to check out the project you want to fork using the clone url:

Go to the source control page of the project and click on the source control connection instructions.

sc_connection_instructions

Copy the clone url and open the TortoiseHg clone window.

hg_clone

Enter the repository url as the source path and select a folder where to clone the repository. If you don’t have a folder already created for this repo, just add the name in the destination path text field and the folder will be create.

clone_dialog

 

Second create a project on Codeplex and add as a Sync source.

Log into Codeplex (or create an account) and create a project where you want to work on, make sure you select Mercurial as your source control.
You should be able to also use Google code to host your clone, everything should work the same, just make sure you use Mercurial as well.

Once the project is created go to your new project source control page and copy the address to your repository.

In your computer open the properties of your cloned repository (the one we cloned in the previous step).

repo_settings

Go to the sync tab and click in the Add button.

sync_dialog

Notice that the default (an only) repository is the original one, we want to add the repo to our newly created project and set it as default.

add_remote_source

If you are using https authentication make sure you add your username and password following this form: https://username:password@hg01.codeplex.com/repository
Add any Alias name but not “default”, not now. Click Ok.

Select the Remote repository marked as default and click the edit button, change the alias from default to “origin”

change_default_to_origin

Now in the sync dialog select the new repository (the one pointing to your project) and click the Set as default button.

click_default

Once done you should have something like this, the name of the projects should reflect the projects you are working with.

default_sets

 

At this moment you are ready to push the code to your new project, just open the synchronize window from TortoiseHg and click push.

commit_to_your_project

Once this is done, go to the source code tab in Codeplex and take a look, notice that you should have all commits from the original project plus any new commits done into your clone, in this case I added three commits after cloning.

 new_changeset

 blog_engine_sc

From inside Visual Studio

Or course this works from inside Visual Studio as well if you have the Hg plug-in installed

from_inside_vs

kick it on DotNetKicks.com Tuesday, March 02, 2010 11:48:20 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [1] - Trackback
Programming | Tools
# Thursday, February 25, 2010

If you, like me, haven’t been able to go to PyCon don’t despair. The videos of the sessions are available at blip.tv, just visited http://pycon.blip.tv/ or http://pycon.blip.tv/posts?view=archive&nsfw=dc for a list of all the videos.

Some of my recommendation so far.

Test and Testability, http://pycon.blip.tv/file/3261272/

Selenium RC with Python, http://pycon.blip.tv/file/3264579/

What every developer should know about database scalability: http://pycon.blip.tv/file/3261223/

kick it on DotNetKicks.com Thursday, February 25, 2010 9:15:03 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
General | Programming

This blog is going mobile. I will be using the services of Mippin. To visit the mobile version of the site just go to: http://mippin.com/dynamicprogrammer

kick it on DotNetKicks.com Thursday, February 25, 2010 8:35:11 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
General
# Tuesday, February 23, 2010

I’m a proponent of dynamic languages and I love Ruby and Cucumber but I have to admit that this is cool. I was writing a set of specifications with SpecFlow today and suddenly I saw this error in Visual Studio.

Scenaio

Double clicking the line takes you to the misspelling error in the Specification file.

 

Mispelling

I though that was really cool, specially for the slightly dyslexic programmers like myself. :-)

kick it on DotNetKicks.com Tuesday, February 23, 2010 10:39:55 AM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Testing | Tools
# Friday, February 19, 2010

I recently posted about how to integrate Git with Visual Studio. Of course Git is not the only DVCS out there.

Mercurial is another source control system similar to Git that is having more and more relevance. Google code have been supporting it for quite a while and recently Microsoft announced that Codeplex is adding it as an option for new projects. (Codeplex already supported TFS and Subversion).

At work we use Fogbugz as our bug tracking/project management tool. A few months ago the people at Fogcreek started a beta for Kiln a code review/source control system that runs on top of mercurial.

I sign up for the beta and have been running a pilot project on it. Since I really like it I decided to start moving all our projects to Kiln and ditch Subversion. The piece that I was missing was a good integration into VS for the rest of my team.

A few day ago I found Mercurial VSS an integration package for Mercurial into VS.

The installation is very simple, just select the version of VS you want to install against (version 1.0.7 released today integrates without problems with both VS 2008 and 2010 RC).

select_vs_version

 

After installed go to Tools –> Options and make sure you select display all options (only in VS 2008).

select_show_all_options

Select the Source Control option and in the Plug-in Selection option change it to Mercurial.

set_scs_plug_inn 

After open a project (even one that is already under source control) you will need to select the Add to source control option from the contextual menu inside the Solution explorer.

add_to_sc 

After added you will se a tilde besides each project.

solution_added

This are the options available from the contextual menu.

hg_menu_options

 

And a few screen captures of the different options.

 

Change log

Change_log

 

Commit

 commit_window

 

History

 history

 

Pull

 Pull

 

Push

 push

 

Revert

 revert

 

Tags

 taggingt

 

Update

 Update_window

 

Conclusion

WIth the support from Codeplex and Google code to Mercurial and the tight integration that this plug in presents I see no reason why people will not use it as the DVCS of choice for .net open source projects hosted on those sites.

kick it on DotNetKicks.com Friday, February 19, 2010 11:57:28 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Programming | Tools
# Thursday, February 18, 2010

This is a very short book on a great practice for any agile team. The book walk you through a lot of techniques an exercises that will help you and your team to have successful retrospectives.

The first 3 chapters provide information on what retrospectives are about. How to create one for your team and what you need to learn to lead them.

The next 5 chapters provide techniques that will help you to go through the different stages of the retrospective. Each technique is clearly explained, with step by step guides and diagrams (when necessary) to provide more information. In some cases, when some techniques are complimentary, this is clearly indicated.

The authors also provide guidance on the scope where to apply the activities and a time estimate.

The previous to last chapter talk briefly about release retrospectives and the last one give some pointers in how to act on the actions, resolutions and experiments the team agree to follow.

If you work as a team lead in an agile team and you are in charge of facilitating the retrospectives, this book is a good tool to have.

kick it on DotNetKicks.com Thursday, February 18, 2010 11:00:16 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Books | Methodology
# Wednesday, February 17, 2010

Yesterday I received another box of books from Amazon.ca with 6 more books in it. After taking them out and putting them down into my already filled shelves I sat down and look at the pile of books. 17 computer books in all that I haven’t started or haven’t finished.

I decided that it was time to make a list and put some kind of priority/order on the reading ahead.

So here it goes, this is the list (in order for now) of books that I plan to read in the next 6-9 months.

Computer programming and methodology)

Agile Retrospectives: Making good teams great (in progress  Finished) (BTW: I just found this presentation from the authors at Google)
Growing object-oriented software guided by test (in progress)
User stories applied.
Testing ASP.NET Web applications.
Debug it!
Ship it!
IronPython in Action (finish this one. It’s a great book. I’m just busy)
Beginning Scala.

[The order after this one is just tentative]

Designing the obvious.
C.O.D.E The hidden language of computer software and hardware (in progress – fantastic book, geeky, deep and incredible well written)
Designing the moment.
Expert F#.
Release it!
DSLs in Boo.
Web application hackers handbook. (probably need to read this sooner)
Agile principles, patterns and practices in c#.
ASP.NET MVC framework unleashed.
Foundation Expression Blend 3 with Silverlight. (Finish this one, but since Silverlight 4 is coming I may just wait).

General reading.

Brain rules.
Once more around the park.
The boys of summer.

I will try to keep this list updated (I make no promise though) and post a brief review of the books that I like the most.

kick it on DotNetKicks.com Wednesday, February 17, 2010 10:55:56 PM (Eastern Standard Time, UTC-05:00) by Hernan Garcia #    Comments [0] - Trackback
Books
Add The Dynamic Programmer Mippin widget
Navigation
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
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: 197
This Year: 15
This Month: 1
This Week: 0
Comments: 70
Themes
Pick a theme:
All Content © 2010, Hernan Garcia