kdmurray.blog

The crossroads of life and tech

Accessing HttpContext objects from other classes

I could swear I wrote about this at some point in the distant past, but I couldn’t find the article this week when I needed it to help troubleshoot an issue with another developer. The issue he was having was how to access objects from the executing web page’s HttpContext object from a class other than the CodeBehind of the executing web-forms page. Essentially he was looking for a way to map a web-path to a physical folder path without needing to hard-code it or know where the application was deployed on the server in question.

If done correctly, an application can reside anywhere in the file system and be deployed to a virtual directory at any depth without causing a problem with URL resolution. In the code-behind of a web-forms page, the code is simple:

string physicalPath = Server.MapPath("~/somefolder/myfile.xml");

However doing this from another page involves just a little bit more work:

Using System.Web;
string physicalPath = HttpContext.Current.Server.MapPath("~/somefilder/myfile.xml");

It’s really quite straightforward when you see it, and I can’t believe that I forget how to do it. This method will also provide you access to lots of other useful objects which makeup the “state” of the application from an HTTP perspective.

Back to Basics

Over the past year my personal life as undergone some fairly major changes. I started a new job a little over a year back and there were the obvious changes that go along with that. But more importantly my wife and I welcomed our first child into the world and that was a life changing moment. Now, most of you know that I don’t talk about my personal life in the blog so suffice to say that we have thoroughly enjoyed our first year as parents. It is a wonderful experience and we eagerly await every new day to see what will happen next.

One of the things that changes when you have a new baby is the amount of time you can spend on yourself and your own hobbies and pursuits. I used to spend upwards of 4-6 hours every day outside of work on the computer blogging, coding, or otherwise toiling in one digital adventure or another. Now I find that the number ranges somewhere in the range of 0-2 hours per day. That is a pretty drastic reduction no matter how you slice it (about 80% for those of you scoring at home).

There are a number of projects that I have started and stopped over the past few years each of them trying to build a better mousetrap, or re-make something from scratch just to see if I could do it. With the limited time available to me now, I have become more focused on wanting to actually do more with the time I have — this means not reinventing the wheel every chance I get.

My wife and I have both found that we have become far more effective with our time, getting more done with less time than we ever have before. In the past couple of months I have started to extend that to my digital life as well. Gone are the days when I focused on a writing a to-do list, a backup utility, a blogging engine, a photo manager or a disk-erasing tool. There are lots of great (free) tools out there which can handle those tasks very well, even if they don’t satisfy all my neurotic desires (like how my historic completed work tasks should be handled, cataloged and stored for reporting purposes (you know, for when I will pull metrics on my completed work)).

I have also decided that diving in to learn a new, modern programming language is probably something that would realistically take more time than I’m willing to devote to the enterprise. Python, Ruby, Java, and the ASP.NET MVC framework are all on my list, but are undergoing changes and enhancements so frequently that I’m having trouble keeping up with what’s out there, nevermind trying to actually learn the stuff. But I do want to become a productive programmer in some language outside the rather constrained, and somewhat self-imposed, .NET bubble in which I have spent the majority of my professional career. Ideally I would like to write in something that I can port between operating systems without too much headache. Being able to produce code that will run on anyone’s machine is a great asset — especially when you have Windows, Mac and Linux machines in your own house to start with.

So the question is what can I learn that will allow me to:

  1. write code for multiple platforms
  2. grow as a developer
  3. not have to keep up with constant enhancements

The answer I came to was 42 C. It seems to satisfy all of the criteria above for me in a way that other languages don’t.

C is by nature intended to be a multi-platform system. If you’re able to confine your applications to CGI or the command-line this is made even easier.

C also requires developers to know much more about how computers and compilers work than more contemporary languages like C#, Java or Python. Though it arguably makes programming more difficult, I think it will help me become a better programmer over time as I learn some of the trickier parts of getting a computer to do what I want it to do.

The current ANSI standard specification for C was introduced in 1999. This means that for the past 12 years, the standard for C programming has remained essentially unchanged. This makes C a good choice for someone who doesn’t have a great deal of time to keep up with changes and enhancements in the specification.

For all these reasons, and my own simple curiosity I’m embarking on an adventure to learn and become proficient in C. I make no assertions that I’m trying to master the language as I can’t see myself getting beyond the hobbyist or perhaps open-source contributor stages. I do have some ideas for the first couple of projects I would like to tackle once I get the basics out of the way. Hopefully I’ll be able to release some source code back into the world over the next year or two — after all, I’m in no hurry.

C# IsNumeric implementation

Here’s a quick and dirty implementation of “IsNumeric” in C#. This is one of those methods that just seems to be missing from C# which appears in so many other languages.

UPDATE 12-Apr-2011: After some fantastic discussion elsewhere I’ve modified the code to handle a number of additional scenarios. A point was also raised that a combination of Int64.TryParse() and Decimal.TryParse() would accomplish the same thing. They would, almost, but those methods test for valid 64-bit integers and valid 64-bit decimals — they don’t test whether a string is numeric. Feed them a long enough string of numbers and they’ll return false. It’s a pretty fine distinction, I grant that, but I figured since I was writing the code I might as well make it as robust as possible.

        public static bool IsNumeric(string s)
        {
            return IsNumeric(s, false);
        }

        public static bool IsNumeric(string s, bool allowDecimal)
        {
            bool result = true;
            if (String.IsNullOrEmpty(s))
            {
                return false;
            }

            if (s.StartsWith("-"))
            {
                s = s.Substring(1);
            }

            char[] chars = s.ToCharArray();

            if (allowDecimal)
            {
                bool decimalFound = false;
                foreach (char c in chars)
                {
                    if (c == '.' && !decimalFound)
                    {
                        decimalFound = true;
                    }
                    else
                    {
                        result = result & (char.IsNumber(c));
                    }
                }
            }
            else
            {
                foreach (char c in chars)
                {
                    result = result & char.IsNumber(c);
                }
            }

            return result;
        }

I built 14 39 unit tests for this on the project I built it for throwing all sorts of weird and null data at it, and it seems to run fairly well and reasonably quickly. Any comments/suggestions are welcome.

Announcing EpubSharp

Over the past few days I’ve put some time into working on a library to create EPUB documents in .NET.  When I first did a search for this a few months ago I really didn’t find anything that suited my needs: a library that I could use to create EPUB documents on the fly, in code.

So I said to myself: “Self! You can write code, build the damn thing yourslef!”. So I did.

The initial version of the library has been published up on Google Code and is probably full of holes. If you’re interested, have a look and let me know what you think.  I’ll try to publish some more detailed specs for what the library does in the coming weeks.

For now, it can get got at: http://code.google.com/p/epubsharp/ — and yes, the documentation on that page is as sparse as it is here.  :)

New Podcast: Short Order Code

Over the past several months I’ve mentioned occasionally that I’m working on a new software development focused podcast. The time has finally come for me to make a more official announcement about this new endeavour.

The new show called Short Order Code. The show will be a
series of audio podcasts and video screencasts focusing on a number of
areas of practical software development.

The first episode is out now, and the next few are in various stages of being produced while I put together the final bits and pieces and trying to sort out my process.  Once that’s done and the first few episodes have been put together
I’ll try to provide more information around long-term plans for the
show.

For those who may be wondering, this is not going to affect my
affiliation with the Aussie Geek Podcast (despite our . I love doing the show, and plan to continue in my co-hosting role for the foreseeable future as long as the AGP community is willing to have me. Time zone considerations will probably continue to make a mess of our schedule, but we’ll do our best to produce a great show for the community as often as we possibly can.

So keep your eyes peeled for more information, and head on over to the website at shortordercode.com and subscribe to the feed!

Honing the Craft

We were on vacation for a couple of months back staying with my wife’s family in the US.  I didn’t completely unplug during the trip, though I wasn’t checking work email, my personal accounts were down to a cursory glance once-per-day, and I’ve dialed back on the Twitter usage quite significantly.

In some of my relaxation time (while babysitting my sleeping 7-month old niece) I got the chance to do a bit of software development. My brother-in-law and I got to talking one night about a project he was hoping to get some help with. He had asked me as an adviser, and since the work was similar to things I’d done for work in the past I felt compelled to offer to help.

The project itself was simple enough: take this text file and put its contents into an Access database. The process has reminded me about why I like software development, and why I miss it.

Over the course of the next couple of weeks we went through a half-dozen iterations of the application, much in the same way that I would have worked with end-users inside our business unit at work.  It allowed me to work through a problem, understanding the true nature of the business problem that he was trying to solve, not necessarily just the problem as it was described in the initial requirements discussion.

I had the chance to pull out skills that I hadn’t exercised much in the past couple of years since my job role has changed, which was a great shift for me.  It’s given me the push I needed to get the podcast going, and to dive in to a few of my other projects with a bit more gusto.  Software is a craft, and its one that I need to continue to hone as time moves forward.  I could probably be considered little more than an apprentice right now, but with the entire Internet as my journeyman instructor I should be able to earn my ticket in the months to come.

Regaining my Chops

It’s been a good long while since I’ve written much in the way of production code. I’ve tinkered here and there with some small side projects, but rarely have I given much thought into how those pieces of code were written so long as they got the job done; after all I didn’t expect that the code would ever leave my own desk.

During a recent vacation I had the opportunity to help my brother-in-law with a project. It took a few iterations but we managed to get some code written which fulfilled the need in just a few nights’ work. Though the complexity was about the same as my own small personal projects, this project was different.  It was for a client — albeit a client who I wasn’t charging money for the work.  Someone else was going to read and maintain this code so I knew that I had to take some measures that I hadn’t been used to taking recently.

The first was refactoring. I refactored the crap out of this application — and for the most part things worked out nicely. I was able to turn all my poorly named and one-character variable names into elements and objects with meaningful monikers.

Commenting was the next step.  After the refactoring was completed I could move on to the next step of the application which was to put in some descriptive comments in the applications.  These comments describe not the what, but the why.   This is the approach I’ve tried to take relying on good naming within the application to describe the what, and comments to provide rationale.

The final thing I did was take my brother-in-law through the code.  He has a background in software but has never done much in the way of OO (he writes Cobol) and hasn’t ever worked with the .NET languages.  In doing the walkthrough we came across a few minor issues and a couple of small enhancements for the application.

All told I spent about 25 hours working on this project, and in that short amount of time, I really did begin to regain my programming chops.  I’ve got a ways to go yet before I get to a level that I’d be comfortable with, but it’s a start.

Stack Overflow – Where Experts Exchange Information

soLast year a new Q&A site for developers called Stack Overflow was opened to the public. We covered the site a bit on an episode of the AGP a few months back, but I just realized the other day that I hadn’t posted about it here.

The site is focused on the interaction between software developers which is truly how most of us learn the best — by working with, interacting with and drawing on the wisdom of other developers.

The site draws on a whole bunch of different elements that set it apart from other Q&A sites.  Creator Jeff Atwood describes Stack Overflow as a free Q&A site that’s built and maintained by the community of developers.

While the site was built to answer developers’ questions, it also has some stiff competition in the form of the Experts’ Exchange.  Most developers know about EE, and are annoyed by the fact that the site purports to charge money for access to the answers to development questions.  As it turns out, it’s all available for free, you just need to look around a bit harder to find stuff.

Stack Overflow has been built with transparency and ease of use in mind since its inception; the model is to get as many eyeballs on a question as needed to get a good answer.  Good questions and good answers are up-voted by the community — similar to the way Digg works, except it’s harder to game the system.  Up-votes provide reputation points which at lower levels unlock some of the site’s features.  The site also gives out badges for meeting certain goals on the site, based on the ability to get badges or special goals on Xbox live.

Jeff Atwood’s passion is around developing software from a more human perspective. Much of the design of the site for Stack Overflow, and the code behind it are based on driving positive behaviours within the developer community.  Instead of lots of hard-and-fast rules, there are easy ways to do good things, and more difficult ways to do things that shouldn’t be overdone.

As a side-note, Stack Overflow’s codebase is written in C# using the ASP.NET MVC framework, and has been in use since the very early CTP days of MVC.  It’s a great example of the power that can be brought to bear on the web with this toolset.

I love the site, it’s been a great resource for me for the past year or so, and I highly recommend it to anyone who has a development dilemma that they need to solve.

AnkhSVN and Visual Studio 2008

ankhsvnSource control is one of those things that developers get really polarized about.  Most agree that having source control on projects is a necessity, but that’s typically were the similarities end.  Some folks are of the mind that every line of code, however insignificant, should be under source control.  This provides records of what was written, and a reference for things that were done in the past.  Others believe that source control should be reserved for “real” projects, things that are deliverables for customers, or products to be released to real-world environments.  I really don’t want to get into this debate tonight, so I’m going to stick to the technology.

I was wanting to get some source control in place for a few of my personal projects.  I chose to go with Subversion for my source control server for a few reasons, not the least of which was that my hosting company supports auto-configuration of SVN repositories, so I was able to get that set up in just a couple of minutes.  That left me some time to contemplate how I would access the repository from the client.

newproject_svnI’m running Visual Studio 2008 on my development machine and this gives me the ability to use plugins for the IDE, a feature that is sadly missing from the express editions.  There were a couple of good options available for SVN plugins, VisualSVN which is the 800lb gorilla in this space, and the open-source option CollabNet’s AnkhSVN.  Given the fact that this was for personal exploration of the toolset, the open source (free) option was the obvious choice.

The setup for AnkhSVN was quick and painless, and when the IDE opened up it put options for source control right in the menus where they were nice and easy to find.  I created a project, and selected the “add to Subversion” checkbox, entered the necessary credentials and created the project in my SVN repository.

anhksvnWhen in Visual Studio, the AnkhSVN controls are located on a tab at the bottom of the IDE, alongside other solution-wide functionality like the To-do list, output window etc.  This pane tracks all of the changes (adds, deletes and updates) that you’ve made to the solution files.  This is extra handy as a review when you’re ready to make your commits back to the repository.  By quickly scanning the list of changes you’re able to write solid commit comments to provide some decent documentation for you, or those who come after you.

I’m still relatively new to Subversion and AnkhSVN, but I’m looking forward to exploring them in more detail — maybe I’ll even do a podcast episode about it!

ASP.NET MVC Tutorials

A couple of weeks ago at Mix ’09 the ASP.NET MVC team announced the RTW (release-to-web) version of the MVC framework. I’ve been looking at the framework and playing with pieces of it for a few months now, but due to school & work commitments haven’t really had a chance to give it a good run through, or build anything meaningful with it.

This past week I’ve gone back to the ASP.NET website and discovered that there is now a long list of tutorials which have been put in an order to help make the major features of the MVC framework more learnable, particularly for those of us who haven’t had that MVC-heavy comp-sci education.  The tutorials come in either written or video form (there is some overlap) and do provide some good step-by-step instructions for exploring the new methodology.

Expect me to get into more detail about the ins-and-outs of the MVC framework in upcoming editions of the new podcast (more details soon, I promise!!)

You can, of course, download and use the MVC framework with Visual Studio 2009 without the tutorials, but I would highly recommend giving the first few a once-over.  Have a look at the tutorial site and see what you think.