Building a photoblog with Nancy and Simple.Data Part 1: Setting up the project

This is the first part in my Building a photoblog with Nancy and Simple.Data series:

  1. Setting up the project
  2. Defining the routes
  3. Rendering some views
  4. Adding the database
  5. Updating Simple.Data
  6. Adding comments
  7. The archives

As an amateur photographer, I’m currently running a photoblog based on WordPress. While that’s working just fine, I have a feeling that WordPress is a bit too over-featured for a simple photoblog. Since I’m not only an amateur photographer but also a professional web developer, I have the perfect excuse to build very own little photoblog!

As a .NET developer, the evident choice would be between ASP.NET WebForms or ASP.NET MVC. If those two would be my only options, I would pick MVC without a doubt, but there are some other, open source, options. One of them is called Nancy and it’s been getting quite a lot of attention lately.

For data access I could go with a nice ORM like NHibernate or Entity Framework, but if WordPress is overkill for a photoblog, then NHibernate and Entity Framework are triple overkill. Instead I’ll go with another open source alternative: Simple.Data.

Both Nancy and Simple.Data claim to be lightweight and easy to work with. In this series of blog posts, I’m going to describe my experiences with them while building my photoblog.

Creating the solution in Visual Studio

As far as I know, the easiest way to get started with Nancy is to create a new empty ASP.NET Web Application. So start Visual Studio, click File > New Project…, select the ASP.NET Empty Web Application template, enter a name for the project (I’m bad with names, so I’ve chosen MyPhotoBlog), make sure that Create directory for solution is checked and click OK.
Create a new project

This gives a nice clean base to start from. Now we need to add references to the necessary Nancy and Simple.Data libraries.

Adding Simple.Data

Adding Simple.Data is easy. All we need is available through NuGet. Just fire up the Package Manager Console, enter Install-Package Simple.Data.SqlCompact40 and press enter. This will add the Simple.Data.SqlCompact40 library, but also the Simple.Data.Core library since that last one is listed as a dependency. You will also notice that some Rx (Reactive Extensions) libraries are added as well. Isn’t NuGet awesome?

Adding Nancy

The different Nancy functionalities are split up in multiple libraries and unfortunately, not all of them are available through NuGet. So to add Nancy, we will download the source from Github, build it manually and copy the required libraries over to our solution.

  1. Go to the Nancy repository on Github and download the source. Of course you can also fork the project pull everything in using Git.
  2. If you have Ruby 1.8.7 or later installed, you can just run the rake command in the Nancy directory. This will run all unit tests, build all projects and put the libraries in a build directory. If you don’t have Ruby installed you can also open the Nancy.sln file in Visual Studio and build everything manually. Either way, you end up with a lot of dll-files.
  3. Open the directory of the photoblog project in Windows Explorer and create a new directory called Libs. Copy these Nancy libraries to the Libs directory: Nancy.dll, Nancy.Hosting.Aspnet.dll and Nancy.ViewEngines.Razor.dll.
    Directory structure
  4. Add the three libraries as a reference to your photoblog project in Visual Studio. Add the Nancy references

Configuring Nancy

Nancy can be configured using some Web.Config settings. To enable the default configuration, which is OK for this project I believe, just modify your Web.Config file so that it looks like this:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpHandlers>
            <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
        </httpHandlers>
    </system.web>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <validation validateIntegratedModeConfiguration="false"/>
        <handlers>
            <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
        </handlers>
    </system.webServer>
</configuration>

That’s all there is to it! Nancy is ready to go!

Adding our first NancyModule

If we would have used ASP.NET MVC, we would now need to setup routing in the Global.asax file, create at least one Controller class with at least one Action method and make sure that the Action method corresponds to a route. Using Nancy, it all becomes a little simpler. A NancyModule class replaces the Controller, and the Action method and route become integrated into that NancyModule.

To end this first post, we’ll create such a NancyModule to verify that everything is working correctly (Simple.Data excluded, well get to that later).

Add a class to the root of your project. I’ve called mine MainModule.

Let the class inherit from NancyModule (located in the Nancy-namespace):

public class MainModule : NancyModule
{
    public MainModule()
    {
    }
}

Now we’ll make sure that the root URL (“/”) returns something to the browser. Adjust the class so that it looks like this:

public class MainModule : NancyModule
{
	public MainModule()
	{
		Get["/"] = parameters =>
		{
			return "<h1>Welcome to My Photoblog!</h1><p>Nothing to see here at the moment.</p>";
		};
	}
}

That’s doing exactly what it looks like: it is telling Nancy to listing to incoming GET-requests corresponding to “/” and telling it to return a string of text to the browser. Don’t believe me? Press CTRL+F5 and be amazed!
It works!

Now let’s add another route. Let’s add one with a parameter! Modify the class so that it looks like this:

public class MainModule : NancyModule
{
	public MainModule()
	{
		Get["/"] = parameters =>
		{
			return "<h1>Welcome to My Photoblog!</h1><p>Nothing to see here at the moment.</p>";
		};

		Get["/photo/{slug}"] = parameters =>
		{
			return String.Format("<h1>I'm sorry</h1><p>We're having some problems finding photo '{0}' for the moment.</p>", parameters.slug);
		};
	}
}

What do you think that this will do? Correct! It tells Nancy to listen for incoming GET-requests starting with “/photo/” followed by anything. Because we have defined it as {slug}, whatever comes behind the “/photo/” part, gets put in the slug property of the parameters variable. See how we pass it to the String.Format() method? It all works dynamically! To see this in action, just surf to “/photo/big-dog” for example. Isn’t that awesome?
That's too bad. I really wanted to see a big dog...

Conclusion

That’s it for the first post of the series. As you have seen it is really really easy to start using Nancy. From what I’ve read, using Simple.Data is equally easy, but we’ll save that for a future post.

In the next post, we’ll think about what we want our photoblog to do and add all the routes we’ll need for that.

Interesting links

11 thoughts on “Building a photoblog with Nancy and Simple.Data Part 1: Setting up the project

  1. Pingback: Building a photoblog with NancyFX and Simple.Data Part 2: Defining the routes | Kristof Claes

  2. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #827

  3. Pingback: A one file Asp Net web application with NancyFx and SisoDb « Coding in the cloud

  4. Pingback: Building a photoblog with Nancy and Simple.Data Part 2: Defining the routes | Kristof Claes

  5. Pingback: One file CRUD sample with NancyFx, SisoDb and HtmlFormHelper « Coding in the cloud

  6. Pingback: Building a photoblog with Nancy and Simple.Data Part 3: Rendering some views | Kristof Claes

  7. Pingback: Building a photoblog with Nancy and Simple.Data Part 4: Adding the database | Kristof Claes

  8. Kristof Post author

    Oops, you got me there. This is indeed the first part. I guess I was a bit too enthusiastic when copy-pasting the links to the other parts :-)

  9. Pingback: Building a photoblog with Nancy and Simple.Data Part 5: Updating Simple.Data | Kristof Claes

  10. Marcus Hammarberg

    Great post – great series.

    I realize the hustle to keep them in in sync but the Adding Nancy and Configuring Nancy sections above can, of course, nowadays be replaced with these two NuGet-commands:
    Install-Package Nancy.Viewengines.Razor
    Install-Package Nancy.Hosting.Aspnet

    For that matter the whole setup could be done using the Install-Package SuperDuperHappyPath but that’s another story :)

    Looking forward to follow the series further on

Comments are closed.