Friday, March 14, 2008

What is ALT.Net

One of the bloggers I follow regularly in the .Net community is Jeremy D. Miller. I have used his post on the model view presenter as a reference many times when explaining the pattern to developers who have not yet been exposed to it.

Jeremy has an article on the back page of March's MSDN magazine explaining what ALT.Net is. It's a very concise and informative explanation. The tenets of being an ALT.Net developer that he digs into more detail are:

  1. You're the type of developer who uses whatever works while keeping an eye out for a better way.
  2. You reach outside the mainstream to adopt the best of any community.
  3. You're not content with the status quo.
  4. You realize that tools are great, but they only take you so far.

After just getting back from Mix '08 it is really encouraging to see Microsoft begin to embrace the ALT.Net community by doing things like hiring Scott Hanselman and creating the ASP.Net MVC framework.

My IDataRecord Extension Methods

I decided it was high time I did a post with some actual code samples in it, so here we go.

I am really enjoying some of the new C# 3.0 language features, one in particular is extensions methods. If you are not yet familiar with extension methods, ScottGu has a good overview. I want to highlight some specific extension methods I've created for IDataRecord that I think are extremely handy, especially when you spend a lot of time working in the data layer with DataReaders like I do. I feel like I've written variations on the same data layer 100 times. Every time it evolves, when I went from .Net 1.1 to 2.0 I took heavy advantage of generics. Now in .Net 3.5 I am simplifying it even more with extensions.

When working with DataReaders you might often have a ton of code that looks like this:


string myString = record.GetString(record.GetOrdinal("MyField"));


Where record is an IDataRecord coming from a SqlDataReader or something similar. Now that's not a terrible line of code, but it gets pretty annoying typing record.GetOrdinal all the time. So to save me that annoyance I've added the following to my IDataRecordExtensions class:


public static long GetString(this IDataRecord record, string field)
{
return record.GetString(record.GetOrdinal(field));
}


An now all I have to call in my record handling code is this:


string myString = record.GetString("MyField");


Not ground-breaking, but better, and you can write an extension for all the types you are getting data for. Here's a better example of how extension methods for your IDataRecord can be more useful. Sometimes you might be dealing with a nullable type, IDataRecord doesn't handle this too gracefully, you end up having to write code that looks something like this:


string myString = null;
int ordinal = record.GetOrdinal("MyField");

if(!record.IsDBNull(ordinal))
{
myString = record.GetString(ordinal);
}


Again, not a ton of code, but if you have to do this hundreds of times it's going to get annoying and you are going to make mistakes. Now check this out, in my IDataRecordExtensions class I add the following two methods:


private static T GetNullable<T>(IDataRecord record, string field, Func<int, T> func)
{
T result = default(T);
int ordinal = record.GetOrdinal(field);

if (!record.IsDBNull(ordinal))
{
result = func(ordinal);
}

return result;
}

public static String GetNullableString(this IDataRecord record, string field)
{
return GetNullable<string>(record, field, x => record.GetString(x));
}


OK, what the heck is going on here. Lets look at GetNullableString first. This is an extension method for an IDataRecord that takes a field name and then calls this private GetNullable method. Check out that third argument though. In GetNullable its defined as Func<int, T> func. What we are doing is taking advantage of lambda support in C# 3.0 and we are saying, pass me a function that takes an int and returns me a generic T, and in this case T is a string. x => record.GetString(x) is saying passing the record's GetString method as that parameter. Now in our data handling code we can just write:


string myString = record.GetNullableString("MyField");


Nice. What's even better, is now we can easily add other extensions for other more interesting nullable types, such as double by adding code like this to the IDataRecordExtensions class:

public static double? GetNullableDouble(this IDataRecord record, string field)
{
return GetNullable<double?>(record, field, x => record.GetDouble(x));
}


So there you go. Hopefully my experiences with extension methods and IDataRecord are helpful to others.

One a Week Challenge

Joel and I were talking about ways to give ourselves more incentive to blog more. We decided on a friendly challenge that would require us to both post at least once a week. You can read Joel's post for this week highlighting Silverlight 2 from our recent trip to Mix '08. Don't worry Joel, I'm not counting this as my post for this week, check back later for my "real" post.

Sunday, March 9, 2008

Back from Mix '08

I attended the Mix '08 conference in lovely Las Vegas last week. Mix is Microsoft's conference for discussing and launching web technologies held at the Venician Hotel and Casino.

The opening keynote by Scott Guthrie (who's blog I recommend subscribing to) was pretty good. There were two primary announcements:

Silverlight 2

At Mix '07 Microsoft launched Silverlight, their competitive technology to Adobe's Flash. This year they launched Sliverlight 2 Beta 1 during the keynote. Not having ever worked with Silverlight 1 I cannot speak with much authority on the subject. I can say that the demos were impressive, especially the Hard Rock Memorabilia Deep Zoom demo. In addition, my colleague Than, who has been working with Silverlight 1 for the last year, informs me that the 2.0 release is an enormous improvement on the prior version.

Internet Explorer 8

Also during the keynote Microsoft launched Internet Explorer 8 beta. I did not expect to care much about this announcement considering my primary browser has been Firefox for years now, even before I made the move to the Mac. However, my tune quickly changed as they demoed the new version. Having been a web developer for many years now, I have come to accept the constant battle with browser compatibility. I was not prepared for their first demo to involve a basic CSS styled site, being shown in Firefox. The site looked fine, and then the speaker switched to Safari and again, it looked just fine. Then he switched to IE 7, and a giant red square appeared on the page, obviously highlighting a standards compatibility issue that IE has had for ages. And finally, he switch to IE 8, and the sight looked just like it did in Firefox and Safari. I am truly impressed with Microsoft's acceptance of the fact that yes, they are not standards compliant, and one of their major goals in IE 8 is to fix that. This feels like a complete 180 from the Microsoft of the past, and it is a welcome change in my opinion. The rest of the IE 8 demo was pretty good as well. They showed off built in development tools that should make anyone's life much easier as we all continue to build sites that must work across various browsers.

I attended a couple of session that I found to surprising as well.

Developing Data Driven Web Applications Using ASP.Net Dynamic Data

I walked into this session not really knowing what I was about to see. The presentation was about how Dynamic Data allows you to generate a UI scaffolding based on your data model for instant CRUD based UIs, sound familiar? It should, this is clearly Microsoft's answer to Ruby on Rails, or at least part of it. What they demoed was fairly good and comprehensive, and moreover, it wasn't being pushed as the end-all-be-all of frameworks as MS is known to do, but rather as yet another option in your ASP.Net arsenal. This again, is not not something I would have expected to hear out of the Microsoft of the past.

The ASP.Net MVC Framework

By far my favorite presentation was Scott Hanselman's session on the new MVC framework for ASP.Net. This was true for a couple of reasons, the first being that Scott is a truly engaging and entertaining speaker, which is a pretty impressive feat when you consider the content and audience. Secondly I enjoyed it because again, this framework was not being pushed as a replacement of anything, but rather another alternative. MVC makes up the second piece of competing with the Ruby on Rails framework, with the model-view-controller architecture becoming baked into the tools. It is still in its early stages, but looks very promising. I have been doing a lot of iPhone web application development lately, and one of my biggest issues is keeping may page size small. Due to the MVC framework's natural lack of your standard viewstate/postback architecture, it seems to me that the views you generate can be much more easily optimized for a device like the iPhone that can handle full HTML, but may sometimes be limited by bandwidth.

Overall I had a blast at Mix. Thanks Microsoft for hosting the party at Tao in the Venician, I can't imagine what the bar bill looked like for that.

The NGChart Project

In a previous post I mentioned that I was beginning work on a .Net library for wrapping the new Google Chart API. As it turns out, a couple of other people had the same idea. Today I joined the NGChart project on Google Code in order to help enhance this existing library. I was able to easily integrate it into my current project, I urge you to check it out if you need a simple way to add basic charts to your web projects.

Tuesday, December 25, 2007

The Mac, a Viable .Net Development Platform?

If you know me, you know I'm an avid Apple fan. This might seem a bit odd considering I pay my bills as a .Net developer, however, I am going to argue that the Mac is a fantastic .Net development platform. About six months ago I picked up a new Intel-based MacBook Pro. Combined with a copy of Parallels I began doing my development work in this environment to see how well it stood up. Now, six months into this experiment, I have to say I am happy with the results. I'll try and outline some of the specific benefits of doing this.

1. Environment Separation

Working as a consultant I often have multiple clients. Parallels lets me create separate Windows instances for each project. It keeps things separate and once a project is complete I can archive the VM off onto an external drive in case I need to bring it back later (this has saved me more than once).

2. The "Best" of Both Worlds

Parallels Coherence mode makes working in both environments nice and seamless. This is great when you are using sites that required Internet Explorer (I'm looking at you MS CRM and Changepoint). What I really like about this mode is the ability to open documents in applications on the Mac side when I prefer a Mac based tool such as TextMate, or the ability to take advantage of the UNIX core of OS X and quickly grep a text file.

3. The Hardware

Hands down the MacBook Pro is the best piece of computer hardware I have owned. Even when taking OS X out of the picture and running windows with BootCamp it is fantastic.

4. Performance

Overall performance of Windows within Parallels is really good. As expected there can be slow-downs. When installing SQL Server within the VM performance can be effected, I tend now to avoid installing the full blown SQL Server and stick with SQL Express or connecting to external machines unless it is absolutely necessary to have it local.

Now, of course like most things this hasn't been completely smooth sailing. Parallels has had some ups and downs with there recent releases, but as of the latest it seems to be pretty solid again. And since I don't often install MS Office within the VM (unless I need it) I use Office for the Mac and it definitely falls short, especially since it is only built for PowerPC (I can't wait for 2008). But overall I am very happy with the outcome and I will definitely continue to work this way.

Tuesday, December 11, 2007

Google Chart API - Exactly What I Needed

I've been looking around for a simple solution for generating charts for hand held devices that didn't involve a ton of processing and produced charts in a basic image format, not Flash.

Yesterday Google was nice enough to give me a great solution. The new Google Chart API seems to be exactly what I needed.

I am now in the processes of writing a .Net library that wraps these into controls that can just be placed within your ASP.Net application.