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. :)
SQL Server 2005 Beta 2 now on MSDN
SQL Server 2005 Beta 2 is now available on the MSDN subscriber download site.
Enjoy!
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.
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.
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?
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.
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.
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.
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.


