If only I were

Building Great Software

Posts Tagged ‘C#

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

Visual Studio 2008 RTM on MSDN – for real

with 2 comments

Frans Bouma says VS 2008 is on MSDN now and Vitaly confirms it.  It’s 7:30am CST and the MSDN download site is already locked up.

As soon as I can get my hands on the bits I have a project to upgrade; I guess I need to make some room on my laptop as well.

Written by Chris Sutton

November 19, 2007 at 7:53 am

Posted in Uncategorized

Tagged with , , , , ,

David Yack at CRIneta

leave a comment »

David Yack gave an excellent talk last night at CRIneta about C# 3.0 and VS 2008. We looked at Type Inference, Partial Methods, Extension Methods and much more.  There also really seemed to be a lot of interest in the new VS 2008 features.

We also had some great BBQ that went over really well. It was a big step up from our usual pizza or subs. TekSystems went above and beyond in their sponsorship this time.

Written by Chris Sutton

November 13, 2007 at 8:39 pm

So Don clarifies his comments

leave a comment »

Seriously, who would ship on a Thursday?

Written by Chris Sutton

November 13, 2007 at 12:01 am

Posted in Technology

Tagged with , ,

CRIneta Meeting with David Yack tonight – C# 3.0

leave a comment »

David Yack is speaking tonight(11/12/2007) at CRIneta on C# 3.0 and Visual Studio 2008.  We are serving BBQ and are giving away some ReSharper licenses and a bunch of books and software.

Hope you can make it to hear this great talk.

Written by Chris Sutton

November 12, 2007 at 10:02 am

Official VS 2008 RTM Date?

leave a comment »

Update 11/19/2008: Here is news on the VS 2008 MSDN release.

This is still speculation, but it looks like Don Box is saying that VS 2008 and .Net 3.5 will release on next Thursday. It’s possible that he is talking about some other product release, but it wouldn’t make much sense at this point.

He said “Lots of .NET families will have something extra to be thankful for next thursday.”  “Next Thursday” could be either November 15th or November 22nd.

Either way it’ll be good to have the bits in the next few weeks.

Update on 11/13/2007: Don posted a clarification on his blog saying he didn’t intend to suggest the RTM date.  He actually was referring to Thanksgiving (and turkey).  He narrowed it down for us saying that Microsoft only ships on Tuesdays and Fridays. :)

Written by Chris Sutton

November 11, 2007 at 12:30 am

Posted in Tech Community, Technology

Tagged with , ,

Visual Studio 2008 RTM

leave a comment »

Update 11/19/2007: Visual Studio has RTM’d now and is available in you MSDN subscriptions.

Through Ben Hoelting through Tim Heuer through Soma the news is that Microsoft will release Visual Studio 2008 by the end of November 2007.  Along with VS 2008 comes .Net 3.5, C# 3.0, VB9, Linq and much more.

The big launch parties and official release are scheduled for early 2008, mainly in February.

kick it on DotNetKicks.com

Written by Chris Sutton

November 5, 2007 at 1:17 pm

Posted in Tech Community, Technology

Tagged with , , ,

SubSonic, MVC and IronRuby

leave a comment »

Well Microsoft has been snatching up people from the .Net/ASP.Net community.  First Scott Hanselman, next Phil Haack and now Rob Conery.

I think Scott Guthrie has made some great decisions here.  Scott Hanselman’s blog is one of the most widely read in the .Net community. It’s certainly one of the most popular and he is a interesting and well thought out sort of guy. Scott did one of the first demos of the MVC framework at ALT.Net in Austin.

Phil Haack is now working on the MVC framework.  He has great ASP.Net experience and has a good following of readers as well.

I think Mr. Guthrie’s most interesting and exciting acquisition is Rob Conery of SubSonic fame.  I’ve had client projects in production with SubSonic since its 1.0.4 release and have enjoyed what it brings to the table. At 1.0.4 there were definitely some pieces that needed to be ironed out, but it is very stable at this point, version 2.0.3.

There are really two tools/tool suites that save me vast amounts of time in my web development.  SubSonic is definitely one of them. The other is not relevant here but happens to be Red Gate’s SQL tools.

So Rob is going to keep doing his open source work on that project which ironically is hosted on Google’s project site.  But I’m sure they are going to make it work well with the MVC framework.

I wonder how long Eric Kemp is safe considering he is Rob’s right hand man and has been doing much of the work on the SubSonic project?

The last piece, but certainly just as interesting and important here, is Microsoft’s hiring of John Lam to work on IronRuby.

So imagine what you’ll soon be able to do in VS 2008.  Set up a new MVC web project.  Use SubSonic(or another project) for your models, Phil, Scott Guthrie and others are building the support for views and controllers. Now you need the glue to pull this all together.  John Lam’s IronRuby will fit in here nicely and now the need for Ruby on Rails in the .Net world is greatly diminished.  Of course C# will work just fine as well.

I like what Ruby on Rails has done (I was just out at RailsConf in May), but if this MVC framework ends up being built well, we’ll have every thing we need without going outside of VS 2008.

I’m excited about the possibilities here.

kick it on DotNetKicks.com

Written by Chris Sutton

November 2, 2007 at 9:28 am