Archive

Archive for January, 2009

Smart or Silly?

January 26th, 2009

I find that I constantly have to write code to first check for an item in the ASP.NET cache and if it is not found then create the item for the first time and insert it into the cache for subsequent look-ups. I began to wonder if I could simplify that process and came up with the following code:

public delegate object Create();
 
public static T GetOrCreate(string key, Create create)
{
        Cache cache = HttpRuntime.Cache;
        object value = cache[key];
 
        // If there is no object in the cache run the create delegate
        if (value == null && create != null)
        {
                value = create();
                if (value != null)
                    cache.Insert(key, value);
        }
 
        // Cast the return object
        return value == null ? default(T) : (T)value;
}

You could then make a single call to either get the item in cache or create it for the first time like this:

DataSet ds = GetOrCreate<DataSet>("users", delegate()
{
    DataSet ds = new DataSet();
    // Get data from database...
    return ds;
});

Using an anonymous delegate the syntax is pretty easy to either get the item if it’s already cached or create it for the first time. It has the added benefit of placing the code that creates the cache item right next to the code that gets it from the cache to make comprehending the code easier for other developers.

So my question is, is this a clever approach to an old problem, or am I going overboard?

Patterns , ,

Taming Large Solutions

January 17th, 2009

A buddy of mine had a great suggestion on how to work with large Visual Studio solutions. At my work it’s not uncommon for us to have Visual Studio solutions with as many as 45, 65, or even 75 projects. This year we hope to better isolate some of the various system and get back to file references, however, until then I find that working with solutions that large can slow down Visual Studio and consequently slow me down.

Using Davy’s suggestion, I created a new solution with the single project that I work with 85% of the time. I can open the solution and compile in just seconds now instead of minutes.

Tips

.NET Mass Downloader

January 16th, 2009

After a couple tries I was able to use the .NET Mass Downloader to get all the .NET reference source code at one time. Before that, if I wanted to look at some source I would have to setup a test project that calls the code I’m interested in so that I could debug into it. Now I can just search all the files at once for what I’m looking for.

As an added bonus I was able to follow the instructions to get symbols setup on Visual Studio 2005 (which I still use at work). Besides the downloader, I don’t know of any other way of doing that.

Tools ,