Why learning new technology is important

I was originally going to focus on build or buy when I started this blog entry. This morning, however, I was running through some website code and found a custom skinning engine and a custom membership/roles methodology (not a set of custom providers). The whole thing uses rather elabortate code to determine the look and feel of the site, as well as determine menu options (based on roles).

As usual, I am more inclined to head towards the K.I.S.S. principle (Keep It Simple Stupid). And, since Microsoft took time to add funcationality to ASP.NET for these types of items, I wanted to see if it was simpler, and potentially less error prone, than the current methodology.

First step: Try to skin the site

For this part of the exercise, I created two theme folders with simple css files. Nothing extravagant at this time. I then looked at the requirements of the site (how to determine theme) and started to work.

First, I added the following urls to my hosts file (%windir%/system32/drivers/etc).     site1.mySite.com     www.mySite.com     beta.mySite.com     site2.mySite.com     mySite.com

I then added the above host headers to my local IIS instance. I could then test the site with www.mysite.com/myweb or beta.mysite.com/myweb, etc.

The next step was a bit of code. I had to pull the host name and get the actual host (in normal domain nomenclature, the site is set up in the format: host.domain.topLevelDomain – Example: in site1.mySite.com  site1 = host, mySite = domain, com = topLevelDomain.). I just wanted the host. This was done easily with the following code:

        Uri uri = Request.Url;
        string host = uri.Host;

The host, in ASP.NET contains the full string (site1.mySite.com). This has to do with the concept of "host headers" in IIS, thus the word has two names. I only want the first part, so I am going to split it.

        if (host != null)
            string[] hostSplit = host.Split(".".ToCharArray());

 I can then grab item 0 and use it to figure out which host the person is using. As I might want to reuse this functionality elsewhere (determining host), I am placing this routine into a helper library. Currently, this is not static (for ease) as I do not want to instantiate a Session object to set things. I would normally have a static routine, but this is just a simple test to see if this is possible.

    public void SetTheme(string host)
        switch (host.ToLower())
            case "www":
            case "beta":
            case "test":
            case "mysite":
            case "localhost":
                Session["Theme"] = "microtrak";
                Session["Theme"] = host;

Ultimately, I will switch this to pull from a database, but the current app has no concept of an application in the database schema, so I will have to add it to pull from there. As it requires a schema change and refactoring should be done in baby steps, I am not going to tackle this monster right now.

To set the theme, I create a default.aspx page and add a PreInit method.

    protected void Page_PreInit()
        string theme = Session["Theme"].ToString();

        if (theme == null)
            Session["Theme"] = "microtrak";
            theme = "microtrak";

        this.Theme = theme;

Finally, I open the body {} style in the css files and give it a different background color in each. I now have a working site that is themed.

Next: Providers

The next task is to set up providers for membership, roles and profile to match the database schema.

The Point?

The concept of themes has been around for quite a few years. The same is true of the Provider model. If you learn technology, rather than plod on ignorantly (which might be called stupid), you would know what is out there. Don’t get me wrong, there are times when you need to "reinvent the wheel" as the current wheel does not fit your needs and tailoring it to your needs would just create a kludgy wheel. But, more times than not, using technology will help you avoid the pain, and expense, of writing tons of custom software.

The story is actually a bit worse than what I have touched on thus far. I was not going to go into details, but I feel this is a good learning opportunity, as well.

The site I am retooling was actually based on another site’s code. Much like "based on a true story" in Hollywood, there is little resemblance to the original site in the current site. The original HAD providers. They could have afforded a refactoring to custom providers, but they were working. And, they were ripped out. The original site could have been refactored with themes, but the developer decided to build his own. The moral of this story: Don’t rip things out just because you do not understand them; gain some understanding first. You might still rip some stuff out, but you will be doing it in an intelligent manner.

One Response to Why learning new technology is important

  1. law12hot says:

    send lots of code to this group AND JOIN ALSO ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: