Joe Doyle's Coding Blog

A few years ago I wrote about using the MongoDB driver in C#. Its been one of my most popular posts and it really needs an update! Since 2011, the 10gen driver has become the standard. Its been getting updated on a regular basis with features that bring it inline with what we would expect in C#. I've been using MongoDB for all of my personal projects and have been very happy with it. So here's an update for what it looks like today to use the MongoDB driver version 1.8.1. Getting the Driver The source is still located on GitHub at https://github.com/mongodb/mongo-csharp-driver. But now that we have NuGet, the easiest way to get started is by using the NuGet package mongocsharpdriver (http://nuget.org/packages/mongocsharpdriver/). It's everything we need compiled and ready to go. To the Code The best place to get started is still the official 10gen C# driver tutorial at http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial. It covers what you need to get started and helps to keep track of what is new each release. I'm going to stick with the original app which was a simple tool that keeps track of passwords and various notes. I've been using it for a few years and have been happy with it's simplicity. The first change is how we connect. It used to look like this: MongoServer server = MongoServer.Create("mongodb://myserver"); MongoDatabase db = server.GetDatabase("TheDatabase"); That style has been deprecated and instead we should use the new MongoClient class. Here's what that looks like now: var mongoClient = new MongoClient("mongodb://myserver"); mongoServer = mongoClient.GetServer(); var database = mongoServer.GetDatabase("TheDatabase"); Not too different, but the naming is definitely clearer. It no longer looks like we're creating a server which is nice. Getting a reference to the collection is still the same. We specify our CredentialSet class when we get our collection so that we don't need to work with BsonDocuments if we don't want to. Even though MongoDB is a schema-less document store, it does make life easier to have a fixed type to work with. var passwords = database.GetCollection<CredentialSet>("passwords"); And just as a reminder, our model looks like this: public class CredentialSet { public ObjectId Id { get; set; } public string Title { get; set; } public string Username { get; set; } public string Password { get; set; } public string WebSite { get; set; } public string Notes { get; set; } public int Owner { get; set; } public DateTime LastUpdate { get; set; } } One thing that did get fixed is the _id issue from last time. The driver will now use Id as the document id automatically. It also will look for _id, but that's not inline with C# standards. So lets save a new document: var password = new CredentialSet(); // set the property values. passwords.Save(password); Now that we have a saved document, let's query for it. Here's where we get to my favorite new feature; support for Linq. Instead of building up a Query object and using Find or FindAll, we can access our collection as an IQueryable and use Linq against it. Like most custom Linq providers, not every operation is supported, but its typically nothing that can't be worked around. So before we had: var query = Query.EQ("Title", "A password"); var oneDocument = passwords.FindOne(query); Now we can do: var result = passwords.AsQueryable().SingleOrDefault(x => x.Title=="A password"); Of course the older methods are still available for the operations that don't make sense with Linq, such as map/reduce. Next Time: Aggregation Framework, V8, and Full Text Search Another nice feature in MongoDB 2.1 and later is the Aggregation Framework. It provides an easier alternative to map/reduce. I'm still learning about it, but I am using it on this site to generate some statistics for my dashboard view. As of 2.4, V8 now powers MongoDB and we get a few extra benefits such as multiple scripts executing at the same time. We also got the first version of a full text search engine built into MongoDB. I'll dive into these next time.

Getting Started With MongoDB and C# Revisited

A few years ago I wrote about using the MongoDB driver in C#. Its been one of my most popular posts and it really needs an update! Since 2011, the 10gen driver has become the standard. Its been getting updated on a regular basis with features that bring it inline
The World of WordPress About a year and a half ago I made the switch from Posterous to a WordPress for my blog. I figured that I might as well learn how to use the 800 pound gorilla in the room. For the most part, things went well considering that I wanted to run it on Windows under IIS and use SQL Server as the database. I added some plugins for the basics like commenting, syntax highlighting and the like. The import from Posterous was smooth with nothing lost. And it was good. I'm not sure if it was the WordPress upgrade or an update to one of the plugins. One day a few months ago I tried to create a new post only to have 90% of it just disappear upon hitting save. I hit the edit button and retyped a paragraph to see if that would save. It didn't. Typed a little less then previewed the post this time. Gone. I did a few more experiments with creating new posts and editing them in various stages. They all seemed to auto-save early in the entry and then get locked forever. A Ghost in the Darkness I wasn't sure what I wanted to do about my blog. I wasn't in the mood to re-install WordPress. I looked at a few blogs written in .NET, but none of them really appealed to me since most are written using WebForms. Then I saw the KickStarter for Ghost popup on Twitter. It's basically the start of a new platform designed to focus on blogs vs the CMS style product that WordPress has become. Its written in Node.js with SQLite as the default backend database. Markdown is used as the input language with a real-time preview as you create a post. It looks to leverage the best of HTML5 to make a state of the art blogging platform. My initial reaction was probably the same as most developers when they see something cool on the web: I can build that! And so I did. "I see you have constructed a new lightsaber." There's a bit of me that feels writing your own blog is a rite of passage as a developer. I know most people use existing packages because why would you really want to waste time at creating something that has been created hundreds of times before. For me, this is a chance to not only give it my personal touch, but really experiment with new technologies and practice the skills outside of my comfort zone. Some might say it's like a Jedi building his first lightsaber. At work I almost exclusively use ASP.NET MVC 4. And while I really do like using it, I felt this was the perfect time to try building a website in Node.js and Express. I really liked the idea of using Markdown instead of a WYSIWYG editor or plain HTML. I also liked the idea of having the layout update in real time when writing a post. I'm using MongoDB since it's my go-to datastore due to how easy and fast it is. So far the core is done. It's still mostly MVF (minimum viable functionality), but I'll keep tweaking it as I go. Here are some of the highlights that I'm proud of or really happy with. Editing To get the dual Markdown/HTML rendering I'm using Pagedown which is from the folks at Stack Exchange. Its the editor they use on their sites. It was really easy to implement and there's even a 3rd party add (Pagedown.Extra) on which extends the Markdown a bit more for things such as tables and code syntax highlighting. For syntax highlighting I'm using SyntaxHighlighter. For uploading images and files I integrated Dropzone.js by overriding the image dialog in Pagedown. Dropzone is amazingly simple implement and provides thumbnails of the images as you upload. Just eye candy, I know, but the effect is sweet. Here's a screenshot of me writing this post: Styling If there's anything I need more practice at, it's design. Thanks to Twitter Bootstrap, I got a running start. I like the clean and simple look so I tried to keep plenty of whitespace and let it define the sections. I'm using LESS for the CSS. I'm not yet customizing Bootstrap, but its on the list. Font Awesome is used for the icons. I went pretty minimalistic on the colors sticking to the really-dark-grey-and-black on while. I'm still iterating over the layouts, but I think I'm pretty close. Hosting I run my own servers, so I wanted continue to host my blog locally. For now I'm using iisnode with Node.js 0.10. One of the benefits is that I can have IIS host all of the static content and only have Node host the dynamic pages. This is the standard Node configuration I hear about, with the exception that its Nginx used instead of IIS. The concept is the same. I have Grunt setup to do my build and deployment so I can test locally then push out the live site. I really like Grunt and am looking at how feasible it would be to use in the .NET world for things like project scaffolding. Performance I wanted the site to be fast. Really fast. I tried to do all that I could to optimize the site. Grunt combines and minifies my JavaScript and CSS. Express is gzipping the content. The slowest part of the site is Disqus which is used for comments. Without Disqus, page load times are sub-70ms. Someone said on Twitter that a blog without comments is not a blog (and I agree), so its a price I'm willing to pay. One way I make things fast is loading all posts in memory and keeping them there. I don't have thousands of posts, so I can get away with that. Right now Node is only using ~60MB of memory, so I'm not too concerned. Almost there I still have a few behind the scenes sections to create. I want to build up a dashboard for some stats. Probably won't be as amazing as what Ghost will provide, but I'm not sure I need that much. I still have Google Analytics running anyways, and its not like I'm going to beat that. I also want to pretty up the Edit page to use auto-completion for the tags and to have the url get built from the title automatically. Just a bit of extra polish really. I do have an RSS feed, so if you're interested in .NET and Javascript posts, please do subscribe. Until next time...

The Creation of My New Blog

The World of WordPress About a year and a half ago I made the switch from Posterous to a WordPress for my blog. I figured that I might as well learn how to use the 800 pound gorilla in the room. For the most part, things went well considering that
In 2010 I made the choice to use Mercurial instead of Git. That was mostly due to Mercurial having much better Windows support. It had strong tooling for the command line, Windows Explorer, and Visual Studio. It was a simple choice. Ahh, but the times they are a changing… Here we are, a quarter of the way through 2012. The world has moved forward as it tends to do. GitHub has dominated the open source world and elevated Git to be the de facto winner of the DVCS battle. The final victory for Git was the announcement from CodePlex that they too will support Git. It seems Git is really all you hear about now-a-days in the DVCS world. So now that it’s clear that Git is the tool to know, I guess it’s time to make the switch for my personal projects. I can get by cloning a repo with my rudimentary Git skills, now is the time to get familiar enough with it to use it in my normal workflow. I’m not planning on converting existing repos, just starting new ones on Git. Another good sign for us Windows Git users is that Phil Haack has moved over to GitHub. I have much faith in Phil as he and the GitHub gang works towards improving the Git experience on Windows. And I, for one, welcome our new DVCS overlords.

And the winner is… Git

In 2010 I made the choice to use Mercurial instead of Git. That was mostly due to Mercurial having much better Windows support. It had strong tooling for the command line, Windows Explorer, and Visual Studio. It was a simple choice. Ahh, but the times they are a changing… Here
Motivation can be a funny thing. Sometimes you have it when you don’t have time for it. Other times it’s nowhere to be found when you are desperately seeking it. My motivation for blogging has waxed and waned other the last year, as probably is apparent by all three of my posts. This year will be better. Hard to be much worse! I am motivated by moving my blog over to WordPress. I think my layout is cleaner, and my mobile layout is awesome! (Try it!) New Year! Happy New Year! Gregorian and Chinese. New Job! In November I started a new job as a developer at Pinnacle of Indiana. They found me through Careers 2.0 at Stackoverflow.com. I had created a profile when it was still 1.0, but I never really expected to get contacted, let alone a job. I get to work with a great group of people who have a focus on the craftsmanship of programming. I’m excited to learn and hopefully begin to master the Agile methodologies. Most of our projects are .NET, but I’m getting exposure to other Microsoft products like SharePoint, Dynamics GP, and CRM. And of course, lots of web apps. The JavaScript I write today is so much better than just 2 months ago. So far it’s been a great opportunity for me and I’m super lucky to be working here. Depending on what I come up with, I might even do some blogging on our developer blog at www.pinnacleofindiana.com/blog/developers New Stuff! In addition to learning more work skills, I have started brewing my own beer! I’m currently in process of fermenting my second batch. I started out with a Porter which turned out great! My Amber should be ready in about 2 weeks. I would love to blog about it, but I’m not sure what I would say yet. I’ve played with quite a few of the “cool kid” technologies over the last year and I have a bunch of things I want to cover in future posts. Some of the topics are: Node.js MongoDB JavaScript Git More posts are on the way!

New Year, New Job, New Stuff!

Motivation can be a funny thing. Sometimes you have it when you don’t have time for it. Other times it’s nowhere to be found when you are desperately seeking it. My motivation for blogging has waxed and waned other the last year, as probably is apparent by all
I recently picked up Trevor Burnham’s CoffeeScript book. So far it’s a great introduction into CoffeeScript and also Node.js, two topics which I wanted to learn more about. I started running through the first examples to see them run. I downloaded the latest node.exe, and found a way to add the CoffeeScript module without NPM. I wrote up a simple test just to make sure it worked: console.log "Hello World!" I ran this command to run it: node %coffee% test.coffee That worked. Node gladly printed my string, passed through CoffeeScript. Of course there isn’t much that CoffeeScript is doing, but there were no errors. My next step was to try the first full sample in the book. It’s a buildup of the larger app which the book is building up to. I use Notepad++ for most of my plain text editing. I typed in the code, saved it and ran it. Error: In prompt.coffee, Parse error on line 14: Unexpected ‘POST_IF’ The function at line 14 looks like this: promptForTile2 = -> console.log "Please enter coordinates for the second tile." inputCallback = (input) -> if strToCoordinates input console.log "Swapping tiles...done!" promptForTile1() Everything looked correct. I just didn’t get it. Googling for the ‘Unexpected POST_IF’ brings up that it’s a parsing error and most posts have to do with multi-line if statements. I didn’t think that was what I was running into here. Or was I? I read through the multi-line if posts and it dawned on me that maybe it was being more helpful then I thought. I went back though my code and re-counted the spaces just to make sure I was consistent. Turns out I wasn’t exactly. Notepad++ was helping me out my automatically starting the next line at the same indention level as the last line. The I ran into was that Notepad++ was inserting a tab instead of 4 spaces when the indention was 4 spaces or more. CoffeeScript didn’t like the tab to start the line after the if statement. It wants spaces, not tabs. The fix was easy enough. Like all great apps, Notepad++ is flexible. I just had to turn off the option to automatically align the next line. After cleaning out the tabs and changing them to spaces, we were good to go. Since I didn’t really find anything on Google I thought it might help someone else that runs into this. I’m pretty sure it the kind of thing that only us Windows users will run into, with all of our overly helpful tools.

CoffeeScript gently reminds me that tabs are not spaces

I recently picked up Trevor Burnham’s CoffeeScript book. So far it’s a great introduction into CoffeeScript and also Node.js, two topics which I wanted to learn more about. I started running through the first examples to see them run. I downloaded the latest node.exe, and found