If only I were

Building Great Software

Archive for December 2007

Live Writer and Microsoft Update

leave a comment »

My install of Live Writer got an automatic update through Microsoft Update the other day.  Sure is nice to have Live Writer get first class treatment so we don’t have to manually keep updating the bits.

Written by Chris Sutton

December 13, 2007 at 12:30 pm

Posted in Technology

Tagged with

Clarity with var in C# 3

I was in a class the other day and the instructor introduced the var keyword and then showed us some of the standard fare examples of how to use it.  Then the discussion turned to when to appropriately use var.

An obvious place to use var is when you do a LINQ query and you are selecting a limited set of columns back from the original type:

NorthwindDataContext north = new NorthwindDataContext();
var emps = from e in north.Employees
           where e.City == "London"
           select new { e.FirstName, e.LastName };

There isn’t much choice here since emps is an anonymous type made up of the employee’s FirstName and LastName. The custom or partial projection (everything after the select keyword) forces the result to be an anonymous type.

Then the suggestion came up that you should only use var when you don’t know the type. Here is where I differ in opinion and usage. Look at the following snippet:

using System.ServiceProcess;
.
.
.
var procs = from p in ServiceController.GetServices()
                where p.Status == ServiceControllerStatus.Running
                select p;
procs.ToList().ForEach(p=> Console.WriteLine(p.ServiceName));

procs is certainly IEnumerable<ServiceController> but it doesn’t matter to me. I primarily care that procs is a list and that the individual items in the list have a Property called ServiceName. The underlying type is important to the compiler, but the people that have to read code aren’t compilers right?

I’m very glad that the compiler and runtime do what they do with types, but I want the reader’s focus on the simple LINQ query and iterating over the list it to get a property, not on the type. Replacing var with IEnumerable<ServiceController> is harder to read and less useful.

I think carefully picking your focus can improve your code’s clarity.

Update 5/24/2008: Dare Obasanjo has written one of the best posts expressing caution about abusing var. If var genuinely makes it harder to read your code then you should consider replacing it with an actual type.

Written by Chris Sutton

December 13, 2007 at 10:36 am

Posted in Technology

Tagged with , , ,

ASP.Net MVC Quickstart

with 3 comments

The MVC Toolkit has finally been released as a CTP. Go here to start out – this is the official page with all the links and info you need. Here is the official quickstart link.

kick it on DotNetKicks.com

Update: 1/17/2008

Jeffery Palermo has a great list of his links to get you started.

Links for ASP.Net MVC Reading:

Scott Guthrie is the grandmaster of this project and has posted extensively on different aspects of the MVC framework. Here are some quick links to get you going.

Original post about ASP.Net MVC: http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

MVC Part 1: http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

MVC Part 2(URL Routing): http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

MVC Part 3(Passing ViewData): http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views.aspx

MVC Part 4(Form editing and posting): http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx

Jeffery Palermo of CodeBetter and Party with Palermo Fame has written some about the MVC bits as well. He has an mvccontrib project.  Read about the guidelines for participation.

Rob Conery talks about how to use use a RESTful architecture with ASP.Net MVC.

Nikhil Kothari has a great post showing how to build a basic app.

Links for Listening:

Polymorphic Podcast with Jeffery Palermo: http://polymorphicpodcast.com/shows/aspnetmvc/

If you want the best information on the ASP.Net MVC there are a couple of other people you should be paying attention to: Phil Haack and Fredrik Normén.

I will be updating this post with more information as I find it out.

Well I have some digging to do. I need to get the bits installed for some client work that will be using MVC early next year.

 

kick it on DotNetKicks.com

Written by Chris Sutton

December 9, 2007 at 9:39 pm

Posted in Uncategorized

Tagged with , , , , , , ,

CRIneta Meeting – Arian Kulp and LINQ

leave a comment »

If you are interested in learning the direction that Microsoft is going with data access you really should come out to our meeting tonight in Cedar Rapids, IA (if you live in the region of course).

Arian Kulp is going to be showing us the ins and outs of how LINQ works – how to query, filter and update data. Btw, Arian wrote the LINQ Hands on Labs for the 2005 PDC, well over 2 years before its official release, and probably long before most of us were aware the technology was being developed.

Written by Chris Sutton

December 3, 2007 at 3:46 pm

Follow

Get every new post delivered to your Inbox.