Updating Data in CouchDb from c#/Asp.Net Mvc

One of my repositories is working against the REST interface of a CouchDb set of data.  Reads have been awesome, and the “Paste JSON as classes” feature of Visual Studio 2012 makes getting POCOs up and running a treat.

However I ran into the following error doing a POST to update a document in the database:

couchdb {“error”:”bad_request”,”reason”:”Referer header required.”}

For anyone working in the REST space, this will likely be an error you would never run into as you have a great handle on your HTTP verbs.  For us crossover guys – especially relational database, Asp.Net guys – this is a little harder to figure out what is going on.

I was fortunate enough to have had a conversation the other day with our partner on the project who had mentioned that we need to remember to keep our PUTs and POSTs in order.  When I saw the “bad request” message above, I knew what I was doing wrong, especially after having a great conversation about REST with Darrel Miller earlier this year.

When a document exists in CouchDb you send updates to the document URL with a PUT. To create a new entry, you use a POST to the document container and CouchDb will determine the newly created item’s URL.

Here is a generic method to update a document from c#:

image

Via Gist:

Easily Build A Time Selector

I’m working on a project where I need the user to select a time in 24 hour format.  It would have taken me a couple of minutes to punch out the lines to build a select option for each of the items that were to appear in the drop down list, but I elected to use the Enumerable.Range method instead and build the list dynamically.

image

Why approach it this way?  Well, if the requirements change, for example, and I need to change that to every 10 minutes instead of every 30 minutes, I don’t need to go and write another 96 lines of code. All I have to do is change the call the select projection where I compute the minutes (right now at the top and bottom of the hour).

A Picture is Worth Zero Lines of Code. Apparently.

So…I’m getting flack either way…either I post a code-image or I post a Gist. With a Gist, RSS readers can’t render the code. If I post an image, no one can copy and paste. So, there’s the image for you in your RSS readers, and here’s the Gist for those copy and paste aficionados.

Breaking Down the Code

This is actually more verbose than it needs to be but it makes it a little easier to walk through it.  The first two statements are simply capturing some integer values to represent the hours and minutes that I want to render.


    IEnumerable<int> hours =
        Enumerable.Range(0, 24);

    IEnumerable<int> minutes =
        Enumerable.Range(0, 2)

          .Select(x => x * 30);
The call to Enumerable.Range takes two parameters: the first is where to start counting and the second is how many steps to take.  In the second statement I am also projecting the minutes out by multiplying them by 30\. If I wanted, instead to have the list include 6 intervals on the 10, I would change the code to the following:
    IEnumerable<int> minutes =
        Enumerable.Range(0, 6)
        .Select(x => x * 10);

All that’s left to do is build our strings.  We’re going to loop over our hours, and then format those intro a string along with each of the minutes that we’ve projected into our list:

    List<string> timeOptions = new List<string>();

<span class="kwrd">foreach</span> (var hour <span class="kwrd">in</span> hours)
{
    <span class="kwrd">foreach</span> (var minute <span class="kwrd">in</span> minutes)
    {
        timeOptions.Add(<span class="kwrd">string</span>.Format(<span class="str">"{0}:{1:00}"</span>, hour, minute));
    }
}</pre>


That’s it! The timeOptions variable now contains a list of any of the valid options we want our user to select from. You can now easily use this collection to build a drop down list for whatever client you’re building for.

 

Want the condensed version? Check here: Build a list of valid times

Bootstrap Icon Classes as Strings

I’m working on the Twitter.Bootstrap.Mvc package with Eric Hexter and one of the things that we’re going to be doing is templating around forms and controls as an extension to the NavigationRoutes project that we’ve broken out.  We’re going to be adding support to include automatic prepending/appending of icons to known form elements.

This has led me to break out an old class I had created a while ago which exposed all the Glyphicons used in Twitter.Bootstrap as strings.  I just created the gist (located here) which looks something like this:

It’s nothing terribly complicated, but it might help if you don’t want to sling the code yourself.

System.Web.WebPages Aren’t the HtmlHelpers You’re Looking For…

Had a bit of a frustrating morning, one of those times when your tools work against you by helping you out but cost you 35 minutes and an extra cup of coffee.  And maybe a sanity check or two.  The goal was to create a very simple HtmlHelper extension method.  I kept getting the error:

‘System.Web.Mvc.HtmlHelper<dynamic>’ does not contain a definition for ‘MethodName’ and the best extension method overload ‘Namespace.MethodName(System.Web.WebPages.Html.HtmlHelper)’ has some invalid arguments

TL;DR – Check your using statements.  If you’re sharp, the error’s quite indicative of what’s going on.

Here’s another hint as to why this went south on me:

image

I was a little quick on the CTRL + . this morning, and since the first item in the list was System.Web.WebPages.Html, the tools didn’t help me out much at all.  But hey…shouldn’t ReSharper be able to figure this one out for me and give me the light bulb of awesome? 

image

I digress…

Regardless of trying to put using statements in my views for my namespace, or adding the namespace to my web.config in the views folder, the problem was I had this guy:

using System.Web.WebPages.Html;

…but I really wanted this guy:

using System.Web.Mvc;

Fixing that using in the top of my helper class resolved the issue, as now I was actually targeting the proper HtmlHelper class, in the correct namespace, that is being used by my views.  Hope this helps someone else out there as well.

Web Camp–Vancouver

I am geeked to announce that my videos for MVC4 and Web API from Web Camp in Vancouver have been made available.  All of the videos in the collection are located here, and the videos I contributed are below.

Building and Deploying Web Sites with ASP.NET MVC 4

Watch the video on Channel 9

Disclaimer here: While recording this video I neglected to mention that you will need to properly configure your connection strings.  I inadvertently used two data contexts pointed at the same database, you’ll want to NOT do that!

Building a Service Layer with ASP.NET Web API

Watch the video on Channel 9

Enjoy!

VS2012 Code Snippet for Test Methods

I don’t post about unit testing, but when I do, lather, rinse, repeat.

Here’s a snippet to generate a AAA-friendly test method that can be invoked by typing aaa and then pressing tab. Better if you do it in a test class #JustSaying.  Tested in VS2012, works here Smile. The great thing about VS2012 – and to be honest I’m not sure what version this awesome was introduced – you can edit code snippets on the fly in Visual Studio and test them as you save them, without closing the file or having to restart Visual Studio.  Brilliant. I have been doing the VS restart for years, and just tripped over this feature.

Oh, right, the code snippet!

(View Gist on GitHub)

This snippet is based on Roy Osherove’s recommended test naming convention/theories. There’s no general consensus on what constitutes the “correct” test name, but this is pretty sound as far as conventions go.