Oct 22 2009

Simplify jqGrid JSON Generation

Category: .Net Framework | Web DevelopmentAlexRobson @ 15:09

So I’m trying to learn this jqGrid thing and so far it seems pretty cool but one thing was really bothering me: every example I saw that was using JSON was using anonymous types to create the required JSON format. Yuck city. Yes. I’m insane and whiney but there’s method to my madness.

Here’s the anonymous type approach:

var jsonData = new
{
    total = totalPages,
    page = page,
    records = totalRecords,
    rows = (
        from record in records
        select new
        {
            i = record.Id,
            cell = new string[] {
               record.Name, 
                record.Date.ToShortDateString(), 
                record.Description.ToString() 
            }
        }).ToArray()
};

Imagine you have to change any of these properties or the “schema” of your JSON output. What if you had to do that application wide? No refactoring tool can save you. Not only that, but you have to memorize/lookup the schema every time you create it. Typos will ruin you but there’s no compile time checking. These are just a few of the reasons I really dislike that approach.

What if you could do this instead?

var gridData = new jqGridData<Task>(
                tasks, 
                t => t.Id, 
                t => new object[]
                       {
                           t.Name,
                           t.Date,
                           t.Description
                       })
                       {     
							page = 1, 
							total = tasks.Count/pageSize,
							records = tasks.Count};

Now that’s more like it. Strongly typed, intellisense, compile time checked and refactor friendly. So here’s the code to make it happen (it’s very simple):

public class jqGridData<T>
	where T : class
{
	public int total { get; set; }
	public int page { get; set; }
	public int records { get; set; }
	public IList<jqGridRow<T>> rows { get; set; }

	public jqGridData(IList<T> list, Func<T, object> idMember, Func<T, object[]> columns)
	{
		rows = list.Select(i => new jqGridRow<T>(i, idMember, columns)).ToList();
	}
}
	
public class jqGridRow<T>
	where T : class
{
	public string id { get; set; }
	public string[] cell { get; set; }
	
	public jqGridRow(T rowInstance, Func<T, object> idMember, Func<T, object[]> columns)
	{
		id = idMember(rowInstance).ToString();
		cell = columns(rowInstance).Select(c => c.ToString()).ToArray();
	}
}

Right, not an earth-shattering discovery, certainly not going to change your life, but it does make using jqGrid with JSON simple.

Tags: ,

Oct 22 2009

The Bourne Framework – A High Level Introduction

Category: .Net Framework | Open SourceAlexRobson @ 03:16

For a little over a month now, I’ve been contributing to an open source project started and architected by Evan Hoff. After the week of the project, I started bugging him about when I could blog about the project. The project is called the Bourne Framework, and it’s changing the way I write code*. The one thing I should make abundantly clear is that Bourne is new and subject to change. The good news is that it’s also very usable in its current state.

Technology Stack
A lot of the framework comes from Evan’s past experience with several open source projects. Most of them are fairly widely known:

  • NHibernate
  • FluentNhibernate
  • NHibernateLinq
  • StructureMap
  • MassTransit
  • TopShelf (part of MassTransit)
  • log4Net

The framework uses these open source libraries in order to provide out-of-the-box infrastructure for the following types of .Net applications:

  • ASP.Net MVC
  • WCF
  • Windows Services

What It Does
It’s difficult to summarize without just throwing around buzz-words. I would say that it does an excellent job of tying together leading open source frameworks by providing an integrated infrastructure for configuration and application of these libraries. I feel that that’s particularly invaluable to developers who want to use the best technologies available but don’t necessarily have the time and/or resources available to do deep dives on all of them. It doesn’t and can’t completely abstract everything away. in fact, anyone who has experience in these open source frameworks knows that you’ll still need to understand what they do and how to use them in general. The difference between using them on your own and using Bourne is that Bourne gives you a really nice structure and simplifies the configuration experience while reducing LoC, something that I’m a big fan of.

How Do You Learn It?
Bourne has a fairly respectable set of unit tests as well as some demo code included in the source. That’s a good place to start. I am going to start a blog series where I go through several different (very simple) types of applications which show off some of Bourne’s features. I also plan to make the source for all of these demos available on GitHub.

Where Can You Get It?
Bourne Framework is on GitHub. Evan’s url is git://github.com/therealhoff/BourneFramework.git and mine is at git://github.com/arobson/BourneFramework. Evan hasn’t had time to review all the code I’ve added to it, so if you want the purest Bourne, check out his repository first.

 

*Which, if you’ve seen my code, is a really good thing : )

Tags: ,