September 2009 - Posts

LINQ and Homework

My daughter asked me to check her homework today.  One of the math problems was:

A book has 352 pages.  How many 4's were used to print all of the page numbers.

She got 35.  She explained how she arrived at that number and while her logic was good, it sounded too low to me.  I started thinking about it a little and then decided I could just write a few lines of C# code to figure out the answer.

As Visual Studio was starting up, I pictured the code in my head: initialize a counter to zero.  Loop through an int from 1 to 352 and see if the ToString() contains a 4.  If so, increase the counter.  Just as I was about to start typing, I thought, "Oh wait – I could do all of this with LINQ!".

var answer = Enumerable.Range(1, 352).Count(p => p.ToString().Contains("4"));

How did we ever manage before LINQ?  ;)

Technorati Tags: ,,
Posted by PSteele | 14 comment(s)
Filed under: ,

SRT's Bill Wagner wins "Emerging Leader of the Year" Award from Automation Alley

Last night, SRT vice president and co-founder Bill Wagner received the "Emerging Leader of the Year Award" from Automation Alley.  This annual event is Southeast Michigan’s most prestigious, competitive honors for technology organizations and their leaders.  As an employee of SRT solutions, this comes as no surprise to me.  Bill's continuous emphasis on new technologies is an everyday occurrence.  Whether he's speaking at User Groups (or running them!), updating Effective C# for .NET 4.0 or working on tough technology problems with clients, Bill is always the go-to guy with the answers.

And this is not simply me doing a "suck up" post for my boss.  It's a post about how lucky I am to work at SRT.  To be able to bounce ideas off of Bill Wagner any time I want is awesome!  It's about being a part of the company he and Dianne Marsh created.  The company that convinced Chris Marinos to come work with us instead some other large, Redmond-based software company and give him the opportunity to create Elevate – a .NET version of the popular Boost library for C++.  These things, along with all of the other great talent at SRT, make it a great place to work!

Thanks to Bill and Dianne for giving me the opportunity to work at SRT and congratulations to Bill on the award.  Well deserved!!

Technorati Tags: ,,
Posted by PSteele | with no comments
Filed under:

Strongly Typed Session Variables in ASP.NET MVC

This post was originally going to be a comment on Jarret@Work's blog post about "Using Extension Methods for Session Values", but I decided to make a full blog post out of it.  Jarret employed extension methods such Bar()/SetBar() and Foo()/SetFoo() to have strongly-typed session data.  While his method certainly works, I've taken a different approach that I think is a bit more flexible and reads better.

Like Jarret, I maintain a "SessionKeys" class – there is no easier way to ask for trouble than using a magic string buried across 4 or 5 web pages.  But I expose my session data in a base class that all controllers in my application derive from.  Here's a sample from my post on unit testing ASP.NET MVC controllers:

public abstract class BaseController : Controller
{
    protected SpeakerInfo SpeakerInfo
    {
        get
        {
            var info = Session[SessionKeys.SpeakerInfoKey] as SpeakerInfo;
            if( info == null)
            {
                info = new SpeakerInfo();
            }
 
            return info;
        }
        set
        {
            Session[SessionKeys.SpeakerInfoKey] = value;
        }
    }
}

Now all of my controllers inherit from BaseController and they can easily access the data stored in the Session by utilizing the SpeakerInfo property.  I like this over Jarret's implementation for two reasons:

  1. Properties make the code more readable than get/set methods (in my opinion).
  2. I can change where/how SpeakerInfo is stored without affecting the rest of my code (perhaps to use a database instead of Session).

To be fair, #2 is also possible with Jarret's implementation.  He can change the code in his get/set methods.  But his code would still read "Session.Bar()" and "Session.SetBar()" when (if the implementation was changed to use a database), it wouldn't really be using the Session.

Technorati Tags:
Posted by PSteele | 18 comment(s)
Filed under: ,

Handy Extension Methods for ASP.NET MVC's UrlHelper

Mickael Chambaud posted three extension methods he created for UrlHelper: Image(), Stylesheet() and Script().  They make it pretty easy to keep your images, stylesheets and scripts organized in a single location – without the need for you to remember where they are placed.  And if you need to move things around for some reason, you only have to change the extension methods.

Posted by PSteele | 1 comment(s)
Filed under: ,
More Posts