logo
  • Jobs
  • About Me
  • Contact
  • Home

Archive for July, 2004

« Previous Entries

Rory at Microsoft

Oh my… Looks like Microsoft has a new funny man on campus. Rory Blyth was hired by Microsoft. Congratulaions to him. I’m sure Microsoft will never be the same. :)

1 Comment

SQL Server 2005 Beta 2 now on MSDN

SQL Server 2005 Beta 2 is now available on the MSDN subscriber download site.

Enjoy!

No Comments

This Land

I normally try and stay away from politics on this blog, but once in a while, something comes up that I feel I need to pass on. This is one of those times…

With the upcoming election, JibJab has released their latest animation, which is an absolutely hysterical parody of Woody Guthrie’s This Land.

Whether youre a “liberal weiner or a right wing nut job”, make sure to check this out.

No Comments

ReSharper 1.0

JetBrains has finally released version 1.0 of their fabulous ReSharper add-in.

From their website:

ReSharper provides C# developers with many of the same powerful productivity-boosting capabilities that thousands of Java developers worldwide have come to rely on every day with IntelliJ IDEA.

I’ve used this tool since day one, and absolutely am in love with it. There’s a 30 day evaluation, after which you can buy the add-in directly from their website for $99.

No Comments

Pet Peeve

One of my biggest pet peeves … buying an orange, and then upon attempting to peel it, the peel doesnt come off cleanly.

I usually peel an orange by digging a fingernail into an area of the orange, and then begin removing the peel. A lot of times, the peel seems to be glued to the actual orange, resulting in me removing most of the orange while peeling. My hands end up in a sticky mess, and the orange is completely mutilated. Drives me completely up the wall.

Anyone else have big pet peeves that they wanna share?

4 Comments

Microsoft Security Bulletins

Microsoft has released several security fixes today, 2 of which are considered *critical*, and 4 which are considered important.

Please make sure you do what you need to make sure your PCs are protected. Also, if you so desire, you can sign up for automatic email notifications whenever a security bulletin is issued from Microsoft, using the Microsoft Security Notification Service.

No Comments

A useful Session object wrapper

There are always two things I do when creating a new ASP.NET project. The first is to create a base class for all of my pages to inherit. This is fairly straightforward, and makes quite a bit of sense in that you can centralize your error handling (overriding OnError), among other things.

The second thing that I do is add the following SessionManager class:

using System.Web;
 
[Serializable]
public class SessionManager
{
    private const string SESSION_MANAGER = "SESSION_MANAGER";
 
    private SessionManager()
    {
    }
 
    public static SessionManager Instance
    {
        get
        {
            HttpContext context = HttpContext.Current;
            SessionManager manager =
                context.Session[SESSION_MANAGER] as SessionManager;
 
            if (manager == null)
            {
                manager = new SessionManager();
                context.Session[SESSION_MANAGER] = manager;
            }
 
            return manager;
        }
    }
}

Now, whenever I feel I need to add something to the Session state, I simply add a property get/set or field to this class.

The great thing about this method is that it is an easy way to remove yourself from the “Magic Constant” syndrome, where you have to remember the exact name (and case) of the variables you’ve stored in Session. Session variable access is now provided via strongly typed mechanism.

Now that I have this class, I typically add a protected get accessor to my base class so that derived pages can reference it with SessionManager.SomeVariable instead of SessionManager.Instance.SomeVariable. This is purely aesthetic though, you may want to do this differently.

Lastly, I want to mention that this idea is not mine, however, it’s been so long since I saw the original implementation that I forget where it came from. If you are or know the author, please drop me a line, so that I can make sure to give proper credit.

15 Comments

Design Patterns : Template Method

Let’s take a look at how we can refactor an existing chunk of code to take advantage of the Template Method design pattern. This pattern’s intent is to provide a skeleton of an algorithm in a method, deferring some steps to subclasses.

Our original chunk of code will come from Michael Schwarz’s post about using IHttpHandlers without IIS settings. Just to be clear, this article intends no disrespect to Mr. Schwarz’s post or code quality. He has a perfectly acceptable solution to the problem. I just want to show how design patterns can be used to simplify the solution.

As you look through the code, you can see that the base class exposes a Render event that subclasses need to use to inject their own painting prior to being written to the output stream. Every subclass needs to remember to wire this event up if they wish to use it.

Picking out the template method is fairly simple in this case. Since we want our subclasses to be able to paint onto the graphics object, our base class now defines protected virtual void Paint(Graphics g). We could declare this method as abstract if we wanted our subclasses to be forced to paint something onto the graphics object. However, in our case, a default implementation in the base class is fine. Subclasases can override this to provide additional behaviour, if they require it.

The base class then modifies the ProcessRequest method to use this new Paint method at the point where the event was triggered. The code below is the refactored version of Mr. Schwarz’s code:

public class MyHttpHandler : IHttpHandler
{
    protected int Width = 100;
    protected int Height = 100;
 
    protected virtual void Paint(Graphics g)
    {
    }
 
    void IHttpHandler.ProcessRequest(HttpContext context)
    {
        using (Bitmap bmp = new Bitmap(Width, Height))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(
                    new SolidBrush(Color.White), 0, 0,
                        bmp.Width, bmp.Height);
                Paint(g); // our template method
                bmp.Save(context.Response.OutputStream,
                    ImageFormat.Jpeg);
            }
        }
    }
 
    bool IHttpHandler.IsReusable
    {
        get { return false; }
    }
}
 
public class MyChart : MyHttpHandler
{
    protected override void Paint(Graphics g)
    {
        g.DrawLine(new Pen(Color.Black), 0, 0, 100, 100);
    }
}

By using the template method pattern, we have eliminated one step that needed to occur every time the MyHttpHandler class was subclassed, as well as removed two superfluous items from the code (the Render event and the RenderEventHandler delegate).

Granted that design patterns are not always the easiest things to spot, they are very powerful once you do find and use them. For an excellent overview on design pattern implementations in C#, make sure and visit dofactory.com’s software design patterns page. For additional studying, the GoF book is widely considered the holy grail of design patterns.

1 Comment

Rock, Paper, Saddam?

http://www.rockpapersaddam.com/

“Tiger Hand always beats paper” :)

[via Steve Maine]

1 Comment

VS.NET 2005 and Generate Method Stub

Visual Studio.NET 2005 introduces several handy refactorings, including Extract Interface and Extract Method.

One item on the menu is Generate Method Stub. The idea is that you can type a method name and then right click, select refactor and Generate Method Stub, and it would provide a stubbed method for you (throwing a NotImplementedException).

Can anyone answer why this is on the Refactoring menu?

While this is a superb feature, I’m still trying to figure out how this is related to refactoring. To me, this seems more like a tool to assist in new coding, whereas refactoring improves the design of existing code.

No Comments
« Previous Entries
flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010