Getting started with NBuilder
What Will I Learn?
- You will learn Getting NBuilder
- You will learn Using NBuilder to generate data for our UI
Requirements
NBuilder https://github.com/nbuilder/nbuilder
Difficulty
Intermediate
Tutorial Contents
- Getting NBuilder
- Installing NBuilder
- Using NBuilder to generate data for our UI
- An ASP.NET MVC app for our presentation
- Mocking the system
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.
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
You can visit the NBuilder download link to download the latest published release. If you are interested in playing with the source code of this project you can also get that at Github by navigating to here https://github.com/nbuilder/nbuilder.
Installing NBuilder
I downloaded the 3.0.1 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.
IProductService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NBuilderCore
{
public interface IProductService
{
List<Product> GetProducts(int pageNumber, int pageSize);
Product GetProductByID(int productID);
}
}
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.
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NBuilderCore
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Sku { get; set; }
public double Cost { get; set; }
public List<string> Tags { get; set; }
}
}
ProductServiceMock
The ProductServiceMock is where we will place our NBuilder code to generate our return objects. Here is the skeleton for that class.
ProductServiceMock.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NBuilderCore
{
public class ProductServiceMock : IProductService
{
public List<Product> GetProducts(int pageNumber, int pageSize)
{
return null;
}
public Product GetProductByID(int productID)
{
return null;
}
}
}
With these files created we can focus on the meat and potatoes of this discussion: the dynamic creation of generated objects.
In this tutoral 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 tutorial 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.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @cues I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x