A while back I wrote a blog about using IronRuby to generate assemblies. Back then the project had a pretty narrow goal which was to read information from a group of database tables and generate C# classes to model the data describe in the three tables. It works nicely for what I needed for my day job. While I was working on the original project I thought it would be nice to add more input sources like text files, URLs, etc. In addition to multiple data sources I wanted to add the ability to produce code for other languages such as Ruby. Given the time constraints I was working in I put those additions off. Recently I had some time free up (yeah, right!) so I created a second project that is aimed at a wider field of use, Linux, Mac as well as windows. I have added Ruby as an output language plus a new URL source. .
In this blog will illustrate how a URL source that returns JSON can be used to create C# code. The generated code will be responsible for retrieving the data and converting it from JSON to C# classes. Next, I will be incorporated into a MVC project that I will use to display the standings from the given season. The goal here is to show how quick you can use the code to display on the page.
Step 0. Create new ASP.NET MVC 3 Project
Go through the usual steps of creating a new project in Visual Studio. Once you have created the project fire up the Nuget console and run:
The Newtonsoft.Json package is used by the service class we will generate in the next step. The library will convert the JSON data and loads the C# class.
If you don’t have Nuget you can go the nuget.org site or install it through the Visual Studio Extension Manager. It is an easy way to install third-party libraries to your project. It feels very much like installing a gem in ruby.
Ok, we have the project the way we need it now its time to generate the classes.
Step 1. Generating the Classes
I have created a batch file to run the code generation script so I don’t have to type out all the options. The more I type the more mistakes I make. Anyway here is the batch file.
The -i and -url parameters are used together to tell the generate that we are using URL as an input source and the –url passes the actual URL to use. The -l c_sharp and -mc switches are used to pass the desired output language and the name of the primary model class. The -mod and -sod options are used to tell the generator where to write the model and service files respectively.
Next step is to run the script which takes all of a second and now I have my classes.
A total of seven classes were generated to model the JSON (View the JSON here) that was returned. The Standings.cs file is the primary model, meaning it is the topmost class in the source. All other classes that end with Model were discovered in the input parsing process. The all represent a property of the primary model or one of its supporting models.
The StandingsService.cs class will be used in our MVC project to retrieve the data and populate the Standings class. We will use that class to display the data in a view.
Now that I have my JSON based classes it is time to start creating the web front end.
Step 2. Incorporating the StandingsService Class
Since I chose to create an empty MVC project the first thing I’m going to do is create a StandingsController class without the CRUD methods. I also created an index view that we will use to display standings. Here is the code that I’ve added to the controller’s index method to retrieve the standings.
As you can see there is much to this method. I am instantiating the StandingsService class and calling the Get method which returns an IEnumerable. Since I know there will only be one object in the list I’m grabbing that and sending it to the view. If this was actual production code I’d have a few more lines here to validate the year parameter, have the URL in the web.config file and check for nulls before I sent the data off to the view. Since its demo code I’m keeping it simple.
The same goes with the view, demo simple. I sort the values by league, division and ranking.
Nothing fancy here but going from nothing to displaying data in less than 10 minutes is a good way to start any project. The generator takes away the tedious part of data access, creating the models and service classes and lets me get down to the business layer of the application.
I started this project off with a URL to a web site that returns MLB standings in JSON format and I wanted to be able to display the results in my web site. Using my generator project I was able to create the model classes and a service class needed to manipulate the data in C# in less than five seconds. After the classes were generated it took me somewhere between 5 to 8 minutes to get a web page up and running that took the data from the URL and displayed it in my MVC view.
The generator project very young and hasn’t had many real world tests. In the near future I will be running it through more rigorous testing. Look for more blog posts on and around the generator project.