Object Reference .NET

ref this.Object

Learning new Programmming Languages and Technologies

clock June 11, 2009 09:31 by author DaveTheKnave
These days, it seems there are so many new programming languages and technologies to learn I’m starting to find it difficult to even keep track.

Recently, I’ve read article after article about how part of being a good software developer is about being open to new technologies, and to “learn new things” – and I’d be foolish not to agree. The classic example is that of the COBOL programer – the dinosaur – who never updated their skills, and slowly became unemployable as the market changed around them.
How could an 80’s COBOL programmer have stayed relivant? The most probable answer back then would have been “Learn C++”, or, at least “Learn Pascal”.

However, I feel that software developers of the current age are facing a slightly different problem. I’m starting to feel like we’re approaching a kind of “Software Development Singularity” – where things are starting to move too quickly than is possible to keep track.

Currently, outside of my day-job and any projects I’m working on in my spare time, I consider learning the following list of things (at least to a passable level) essential to placing myself in a position where I’m moving forward, so as to avoid becoming extinct.

  1. .Net 4.0
  2. MVC
  3. LINQ
  4. IronRuby
  5. IronPython
  6. F#
  7. Entity Framework
  8. nHibernate
  9. Azure
  10. Windows 7 Development
  11. Silverlight 3.0
  12. WCF, WPF, WF

But wait! That’s just a Microsoft based list! I’m a .Net developer, sure – but I should really broaden my horizons to the open-source community and any Microsoft competitors. (not to mention vendor-ambiguous technologies)

  1. HTML 5.0
  2. Javascript 2.0
  3. Python
  4. Ruby
  5. Google App Engine
  6. Google Gears
  7. Mono
  8. jQuery
  9. CSS 3.0
  10. Google Wave?
  11. iPhone Development

I’m sure there are many more things I could add to these lists.

Whilst I do have some experience with a lot of things on this list – it’s becoming harder for me to refer to myself as an “expert”, or even “well versed” in some of these emerging, and in some cases established technologies.

It’s starting to become scary.

So – I guess the motivation of this article was simply to ask if anyone else is starting to feel things are moving a little faster than they did, well, only a few years ago – and more importantly to ask how you go about choosing the technologies you are going to explore, and to master.

Wish me luck.

kick it on DotNetKicks.com


We made it to asp.net front page!

clock May 23, 2008 08:00 by author Naz
Daves article on Implementing Generic Caching hit asp.net front page as article of the day for a week!



Implementing Generic Caching

clock April 28, 2008 18:40 by author DaveTheKnave
I develop for a large, high-availability website, with hundreds of thousands of daily users. As such, we need to cache a lot of data in our web-server memory (which is cheap) to save numerous hits to our main database cluster (which is very expensive). I would imagine the desire to improve performance by saving on database hits is common across many web applications – and caching frequently used data is often seen as one of the best ways to solve this problem.

There are two further specific problems I face in every-day life, and these are:
  1. We have lots of silly little objects and lists of objects we wish to cache to improve performance
  2. Populating the cache (on request) needs to be thread-locked

Why thread locked?

When the loading of a particular cached item is particularly expensive, say, a fifteen-second database query, there is the possibility that multiple requests can try to populate the cache at the same time – so for example, if ten people simultaneously request a resource, with something like below – you can run into problems due to the required null check:

public List<int> CachedWidgetIds
{
    get
    {
        if (Cache["CachedWidgetIds"] == null)
        {
            Cache["CachedWidgetIds"] = SomeProvider.GetWidgetIds();
        }
        return (List<int>)Cache["CachedWidgetIds"];
    }
}

If this property is accessed more than once quickly, and if SomeProvider.GetWidgetIds(); takes a few seconds to complete – there is every possiblity that multiple requests will attempt to populate this cached object at the same time, until at least one of these completes and assigns a value to the cache.

At a database level, this could potentially cause row-locking and deadlocks if multiple requests are all needlessly running the same expensive query, meaning the query could fail thus making the problem much worse.

In order to solve these problems, and standardize or cache access, we have implemented a generic class as follows:

public interface ICachable
{
    string GetUniqueCacheName();
    int GetCacheTime();
    void FillSelf();
}
 
public static class SafeCache<T> where T : ICachable
{
    private static T singletonObject;
    private static object classLock = new object();
 
    public static T Object
    {
        get { return singletonObject == null ? CacheMethod() : singletonObject; }
    }
 
    public static T CacheMethod()
    {
        if (HttpContext.Current == null)
        {
            // if the current httpcontext is null then we are in a non-web app and can't use web caching
            if (singletonObject == null)
            {
                singletonObject = Activator.CreateInstance<T>();
                singletonObject.FillSelf();
            }
            return singletonObject;
        }
        else
        {
            //Create an instance of a generic type T for a reference type. 
            //Using default(T) will return null
            T cachedObject = Activator.CreateInstance<T>();
 
            if (HttpContext.Current.Cache[cachedObject.GetUniqueCacheName()] != null)
            {
                cachedObject = (T)HttpContext.Current.Cache[cachedObject.GetUniqueCacheName()];
            }
            else
            {
                lock (classLock)
                {
                    //Why check if the cache is null for a second time?
                    //If person #1 enters this statement, they will lock "classLock" and start 
                    //to run cachedObject.FillSelf(); - the lock will mean any requests that take 
                    //place during the time it takes to exectue cachedObject.FillSelf() operation 
                    //will wait at the line "lock (classLock)" - then, once Person #1 releases the lock, 
                    //they'll all come pouring into this satement - and we dont want them to run FillSelf() again!
                    if (HttpContext.Current.Cache[cachedObject.GetUniqueCacheName()] != null)
                    {
                        cachedObject = (T)HttpContext.Current.Cache[cachedObject.GetUniqueCacheName()];
                    }
                    else
                    {
                        cachedObject.FillSelf();
                        HttpContext.Current.Cache.Insert(cachedObject.GetUniqueCacheName(), cachedObject, null,
                            DateTime.Now.AddHours(cachedObject.GetCacheTime()), TimeSpan.Zero);
                    }
                }
            }
            return cachedObject;
        }
    }
}

This class can be used with any object that implements the simple interface “ICacheable” (decalred above) to provide a standard way of populating itself with data. So, to make a long story short, this can be used like:

DropDownList1.DataSource = SafeCache<Widgets>.Object.WidgetIds;
DropDownList1.DataBind();

As you can see, this removes all the workings of the cache from the calling resource, and provides a clean and thread-safe way for it to be accessed.


[Edit: This code sample has been updated with ideas from www.DotNetKicks.com to make it slightly more efficient, also implementing a singleton version]

kick it on DotNetKicks.com



RecentComments

Comment RSS

Sign in