Published: 13 Jan 2010
By: Andrew Siemer

In this article we will take a look at a fairly new open source project called NBuilder (http://www.nbuilder.org and http://code.google.com/p/nbuilder/) and how it can be used to provide us with fake data out of the gate. NBuilder allows you to quickly stand up generated objects based on standard .net types in an easy fluent manner. And that is just the start!

Contents [hide]

The Using NBuilder to mock up a data driven UI Series

  • Part 1 In this article we will take a look at a fairly new open source project called NBuilder (http://www.nbuilder.org and http://code.google.com/p/nbuilder/) and how it can be used to provide us with fake data out of the gate. NBuilder allows you to quickly stand up generated objects based on standard .net types in an easy fluent manner. And that is just the start!
  • Part 2 In this article we will continue our discussion by filling out the implementation in our service class with some NBuilder code. Once we have the working service class in place we can then create a working UI (in the ASP.NET MVC project we created in the last article).
  • Introduction

    If you work in a large team where the DBA builds out the database and the developer builds out the business layer and the designer builds out the UI (or something similar) then you are probably used to working in a manner where everyone agrees on a set of interfaces that will act as the glue from one layer to the next. For the presentation developer, having a set of interfaces only really solves a very small part of the problem. You now know what is coming from your business layer but in order to build a UI and work through all the small problems you have to deal with when rendering data you will eventually need some real working data to write your HTML around.

    We will frequently stand up some mocked representation of the real thing that internally will pass out some data. And if you have done this you know exactly how big of a pain in the butt this quickly becomes. A method such as GetProducts off of a ProductServiceMock might have something as simple as a for loop that quickly spins up 10 instances of fake product objects. Cool. But what happens when you need to specify the page size to get? A quick tweak to the for loop. And when you need to step through a set of paged set of data? Another tweak. Eventually you find yourself maintaining a helper application to build the actual application you initially set out to build.

    In this article we will take a look at a fairly new open source project called NBuilder (http://www.nbuilder.org and http://code.google.com/p/nbuilder/) and how it can be used to provide us with fake data out of the gate. NBuilder allows you to quickly stand up generated objects based on standard .net types in an easy fluent manner. And that is just the start! This tool can be used for generating data to build UI's around, for testing, and any other place where you might need a working set of data.

    Getting started with NBuilder

    The first obvious bits that we need to tackle in order to use NBuilder are to download it to our system and then install it into one of our projects. Let's see how easy that is to do.

    Getting NBuilder

    There are two ways to get NBuilder. You can visit the NBuilder.org site to download the latest published release at http://nbuilder.org/Download. Or you can mosey on over to Googles code site to get the same set of code at http://code.google.com/p/nbuilder/downloads/list. If you are interested in playing with the source code of this project you can also get that at Google by navigating to here http://code.google.com/p/nbuilder/source/browse/.

    Installing NBuilder

    I downloaded the 2.1.9 (beta) edition. This resulted in getting a single FizzWare.NBuilder.dll which I am going to place in a trunk/dependencies directory. From there we will need to create a new project and add a reference to that dll so that we can use it.

    Using NBuilder to generate data for our UI

    Now that we have the NBuilder software we will need to create a test project to demonstrate how to use NBuilder.

    An ASP.NET MVC app for our presentation

    My current preference for demo apps that need a visual UI that is somewhat greater than a little black window is the latest iteration of ASP.NET MVC 2. For that reason, let's quickly create a new ASP.NET MVC 2 application and place it in our trunk/source directory. I am naming my web app NBuilderWeb (which will create an NBuilderWeb folder in source). I won't be creating a unit test project this time around (though using NBuilder in your unit tests could be quite handy too!). The first thing I am going to do after creating the new project is to add a reference to the FizzWare.NBuilder.dll in our dependencies directory.

    Mocking the system

    In the beginning of this article we described a scenario where all the developers would work against an agreed upon set of interfaces. In order to demonstrate this scenario we need to create an interface that we can code against. We will build this interface with the idea that we will be displaying and interacting with a list of products that we can page through. And once we click on a product we will want to see the details of that product. Assuming that you work in a tiered environment you should be coding against a service or factory style class. For that reason we will quickly create an IProductService from which we will define our ProductServiceMock.

    Business Layer

    If you are truly working in a tiered environment you will most likely have separate projects for your business layer. For that reason (to be complete) lets go ahead and create a new class library project named NBuilderCore. We will put our IProductService interface and ProductServiceMock class into this project.

    Once we have the presentation and business layer projects up and running we can then focus on the creation of our core business layer files (which we will be mocking). We will assume that someone will give you the interface and a loose definition of a product. With that in mind we can then create the ProductServiceMock. Here are the files that we will be creating.

    IProductService

    The IProductService interface will act as our contract between our layers. In this interface we will describe two methods: GetProducts and GetProductByID. These two methods are pretty self-explanatory with the exception that the GetProducts will ultimately provide pagination support.

    Listing 1: IProductService.cs

    Product

    The product class is our domain object to represent a product. This won't be a big heavy object as this is purely a demo.

    Listing 2: Product.cs

    ProductServiceMock

    The ProductServiceMock is where we will place our NBuilder code to generate our return objects. Here is the skeleton for that class.

    Listing 3: ProductServiceMock.cs

    With these files created we can focus on the meat and potatoes of this discussion: the dynamic creation of generated objects.

    Summary

    In this article we discussed what NBuilder can be used for. We then downloaded NBuilder and included it into our project. Next we discussed the pieces needed to complete the demonstration of NBuilder.

    In the next article we will take a look at the various functionalities provided by NBuilder. We will then populate our methods with NBuilder code to generate the data that we need to build out a simple UI.

    The Using NBuilder to mock up a data driven UI Series

  • Part 1 In this article we will take a look at a fairly new open source project called NBuilder (http://www.nbuilder.org and http://code.google.com/p/nbuilder/) and how it can be used to provide us with fake data out of the gate. NBuilder allows you to quickly stand up generated objects based on standard .net types in an easy fluent manner. And that is just the start!
  • Part 2 In this article we will continue our discussion by filling out the implementation in our service class with some NBuilder code. Once we have the working service class in place we can then create a working UI (in the ASP.NET MVC project we created in the last article).
  • <<  Previous Article Continue reading and see our next or previous articles Next Article >>

    About Andrew Siemer

    I am a 33 year old, ex-Army Ranger, father of 6, geeky software engineer that loves to code, teach, and write. In my spare time (ha!) I like playing with my 6 kids, horses, and various other animals.

    This author has published 29 articles on DotNetSlackers. View other articles or the complete profile here.

    Other articles in this category


    Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
    A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
    jQuery Mobile ListView
    In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
    Exception Handling and .Net (A practical approach)
    Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
    JQuery Mobile Widgets Overview
    An overview of widgets in jQuery Mobile.
    Book Review: SignalR: Real-time Application Development
    A book review of SignalR by Simone.

    You might also be interested in the following related blog posts


    Introducing Versatile DataSources read more
    Successive Method Calls With MoQ read more
    Building a class browser with Microsoft Ajax 4.0 Preview 5 read more
    Scenarios for WS-Passive and OpenID read more
    Self-reference hierarchy with Telerik TreeView for Silverlight read more
    Custom Panels in Silverlight/WPF Part 2: ArrangeOverride read more
    Linq to Mocks is finally born read more
    ASP.NET MVC V2 Preview 1 Released read more
    Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 7: ADO.NET Data Services Based Data Store read more
    WPF Release History : Q2 2009 (version 2009.2.701) read more
    Top
     
     
     

    Please login to rate or to leave a comment.