Blog

Blogging on programming and life in general.

  • C# In Depth Third EditionWhen working as a programmer, it's really easy to continue coding in the same manner you have done since you picked up a language and made your first program.

    The saying: "Why fix it if it ain't broken?" comes to mind...

    I for one sometimes fail to move with the times (unknowingly to me) and find new and better ways of coding. It's only on the off chance I get introduced to different approaches through my work colleague or whilst Googling for an answer to one of my coding queries.

    After reading some rave reviews on C# In Depth, written by the one and only Stackoverflow god: Jon Skeet. I decided to part with my hard earned money and make a purchase.

    C# In Depth is different from other programming books I've read on C#. In fact it's really good and don't let the title of the book deter you. The contents is ideal for novice and semi-experienced programmers.

    Firstly, you start off by being shown code samples on how C# has evolved through its iterations (v1 - v4). In most cases I gave myself a gratifying pat on the back when I noticed the approaches I've taken in my own projects utilised practises and features of the current language. ;-)

    Secondly, unlike some programming books I've read in the past, it's not intimidating to read at all. Jon Skeet really has a great way to talk about some concepts I find difficult to comprehend in a clear a meaningful way, so I could utilise these concepts within my current applications.

    The only minor niggle I have is that there were a few places where I would have liked specific chapters to go into more detail. On the other hand, it gave me the opportunity to research the nitty-gritty details for myself.

    Since I purchased this book, I found myself referencing it many times and appreciating what C# has to offer along with it misconstrued and underused features.

    All in all, the author truly has a gift in clearly demonstrating his understanding on the subject with finesse and if I am able to comprehend even one-tenth of his knowledge, I will be a happy man.

  • Today, an article that (for some reason or another) resonated with me from yester-year popped up in in my news feed: The well known "Burning Platform" memo written by Stephen Elop - at a time when Nokia's future was uncertain. It describes Nokia's troubles as an oil rig on fire and radical changes will need to be made to ensure the company can continue to succeed in years to come.

    As I look back at this infamous memo, I'm reminded how Elop manages to grab your attention in an artistic and well written way. I am instantly able to relate exactly to dire circumstances the CEO and it's workers face.

    For those who don't know what I am talking about, here's an excerpt:

    There is a pertinent story about a man who was working on an oil platform in the North Sea. He woke up one night from a loud explosion, which suddenly set his entire oil platform on fire. In mere moments, he was surrounded by flames. Through the smoke and heat, he barely made his way out of the chaos to the platform’s edge. When he looked down over the edge, all he could see were the dark, cold, foreboding Atlantic waters.


    As the fire approached him, the man had mere seconds to react. He could stand on the platform, and inevitably be consumed by the burning flames. Or, he could plunge 30 meters in to the freezing waters. The man was standing upon a “burning platform,” and he needed to make a choice.


    He decided to jump. It was unexpected. In ordinary circumstances, the man would never consider plunging into icy waters. But these were not ordinary times – his platform was on fire. The man survived the fall and the waters. After he was rescued, he noted that a “burning platform” caused a radical change in his behaviour.


    We too, are standing on a “burning platform,” and we must decide how we are going to change our behaviour.


    ...

  • As of late, I've been attempting to expand my .NET web application development skills by learning MVC and now have the understanding on how it all works. By Jove, I think I’ve got it!

    After building a few small custom websites, I decided to utilise what I've learnt and start building a Kentico site in MVC. Ever since Kentico supported MVC Razor, I've been itching to try it out.

    The main drive to build a Kentico site in MVC for me has been the ability to easily build a complex site by separating an application into the model, the view, and the controller to give me a lot more control over how I want the application to be built. But the best part has to be the clean unadulterated HTML mark up that rendered on the page!

    I think the mark of a good web developer is based on not only how clean their HTML markup is but also (more importantly) their programming skills. But when visiting sites, we only get exposure to the HTML markup. Unfortunately, there's only so far you can go in cleaning the markup when building a site using Web Forms. Even with View state completely disabled and selectively using .NET controls the output can still be quite mucky.

    As a web developer, whenever I visit a site that interests me from a technical level, I often look at the HTML mark up just to see how clean it is. I'm always intrigued to see if a site that looks and functions great is built just as well as it looks.

    But I digress...

    Kentico's support for MVC is definitely impressive. I felt quite at home when moving from a custom MVC web build to a Kentico build. Of course, there are some differences in terms of where your Models, Views and Controllers reside within the file structure of your website.

    If you plan on building a Kentico site using MVC, take a look at this post by Martin Hejtmanek who gives a basic overview on the steps required to get you up and running.

    From what I have built so far, I haven't noticed any limitations in the MVC framework. Just workarounds are required for some features (which I'll detail in future blog posts).

    Question Raised...

    As much as I like having the option of building an MVC site in Kentico, I ask myself the question: In reality, how many sites I build will actually be in MVC?

    The reason why I ask this is because Kentico provides many useful ready to use features out-the-box, a website could be built in half the time of an MVC build. Just think of the number of web parts Kentico has freely available to use!

    You couldn't justify to a paying client the additional time and cost will create a more scalable website that produces cleaner HTML markup. For smaller websites, an MVC site in Kentico could potentially be viable and any custom controls you do make (such as pagination and login controls) could be rolled out across future sites.

    Nevertheless, I'm hoping to do more MVC in Kentico moving forward.

  • Ever since Twitter ditched version 1 of their API to version 1.1, an additional hurdle created when attempting to get any data from Twitter. Authentication (using OAuth) is now required on all API request endpoints. I can see why Twitter decided to go down this route but it does add a little headache when carrying out the most simplest requests.

    In all the sites I have worked on, I've always relied on third party libraries to help me interact with Twitter, such as Twitterizer (no v1.1 support) and TweetSharp. But due to the increased complexity on some of the sites I've been working on, third party libraries don't seem to cut the mustard any more. A more scalable solution is required...

    I came across a really simple and expandable class library called OAuthTwitterWrapper that stemmed from a StackOverflow question I found. Out-of-the-box, this library contains calls required to retrieve a user's timeline and return searches which is great to get you up and running quickly.

    The OAuthTwitterWrapper provided me a really good basis to add further Twitter features, for example a list of users favourite tweets.

    If you don't plan on doing anything too complex when interacting with the Twitter API, third party libraries such as TweetSharp will meet your everyday needs. However, if you want more control over how you make requests, the OAuthTwitterWrapper provides a good foundation.

  • Postbacks can be annoying. Especially when you have a long page of content with a form at the bottom and on button click causes the page to skip back to the top of the page.

    For a user who is not used to a site, this can be incredibly annoying and disorientating simply due to the fact the page has moved to a different position. This isn't so much of an issue if you happen to have a webpage that is low on content and a form will always be in clear view.

    But there is quite a nice easy way to get back to a specific area of a page by using the following line of JavaScript will be added in the page after a postback has occurred.

    if (Page.IsPostBack)
    {
        ScriptManager.RegisterStartupScript(this, typeof(string), "PostbackFocus", "window.location.hash = 'feedback-form'", true);
    }
    

    By placing this code in your Page_Load method, the user will be taken to an anchor point placed within the page. In this case, a <div> with an ID attribute of "feedback-form".

  • Don't you just hate it when you've created a Document Type in Kentico that is full of data and for some reason you need to rename it. I know I do.

    Even though Kentico does give you the ability to rename your Document Types via its interface, the old table name still exists within the SQL database. This may not bother some developers but this makes me feel damn right dirty.

    I came across an SQL script Matt Lee wrote in his blog, that takes the hassle of renaming the tables manually within a Kentico database.

    Definitely check it out. It has saved me a lot of time.

    Source: SQL Script To Rename A Document Type Table

  • It seems that I have a tendency to blog more about YouTube then any other Social API on this site. So here we go again... This time I want to show how to easily integrate a YouTube CMS Form Control within a Custom Table or Document Type within Kentico.

    As far as I'm aware, Kentico only allows you to insert YouTube markup into their HTML Editable Regions via the CKEditor. But what if you wanted to take things a step further and have the ability to return a video Title, Description and Thumbnail within the comfort of the Form tab?

    YouTube Custom CMS Form Control

    As you can see from my custom form control, a user would paste the URL of a YouTube video and press the "Lookup Video" button that will return basic information about that video, ready for the user to carry out any further copy changes they require.

    So let's get to it.

    Step 1: Create A New User Control

    I have created a user control in "/CMSFormControls/Surinder/" of my Kentico installation. I have named the user control: YouTubeLookup.ascx.

    HTML

    <table><tbody>	<tr>		<td class="TextColumn">			<label for="<%=YouTubeUrl.ClientID >">URL:</label> <asp:textbox id="YouTubeUrl" runat="server"></asp:textbox> <asp:button cssclass="ContentButton" id="LookupVideoDetail" onclick="LookupVideoDetail_Click" runat="server" text="Lookup Video"> </asp:button></td>	</tr>	<tr>		<td class="TextColumn">			<label for="<%=YouTubeTitle.ClientID >">Title:</label> <asp:textbox id="YouTubeTitle" runat="server" width="500"></asp:textbox></td>	</tr>	<tr>		<td class="TextColumn">			<label for="<%=YouTubeDescription.ClientID >">Description:</label> <asp:textbox height="100" id="YouTubeDescription" runat="server" textmode="MultiLine" width="500"></asp:textbox></td>	</tr>	<tr>		<td class="TextColumn">			<asp:image id="YouTubeThumbnail" runat="server"></asp:image></td>	</tr></tbody>
    </table>
    

    Code-behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using CMS.FormControls;
    using CMS.GlobalHelper;
    using System.Web.Script.Serialization;
    
    public partial class CMSFormControls_Surinder_YouTubeLookup : FormEngineUserControl
    {
        private string _jsonValue;
    
        public override Object Value
        {
            get
            {
                return GetJsonMarkup();
            }
            set
            {
                _jsonValue = System.Convert.ToString(value);
            }
        }
    
        private string GetJsonMarkup()
        {
            //Pass all user entered form values to the YouTubeDetail class for serialization in the JavaScriptSerializer
            if (!String.IsNullOrEmpty(YouTubeUrl.Text))
            {
                YouTubeDetail yt = new YouTubeDetail();
                yt.ID = YouTubeHelper.GetVideoID(YouTubeUrl.Text);
                yt.Title = YouTubeTitle.Text;
                yt.Description = YouTubeDescription.Text;
                yt.Url = YouTubeUrl.Text;
                yt.ImageUrl = YouTubeThumbnail.ImageUrl;
    
                JavaScriptSerializer jsSerialize = new JavaScriptSerializer();
    
                return jsSerialize.Serialize(yt);
            }
            else
            {
                return String.Empty;
            }
        }
    
        public override bool IsValid()
        {
            JavaScriptSerializer jsSerialize = new JavaScriptSerializer();
            var jsResult = jsSerialize.Deserialize<YoutubeDetail>(_jsonValue);
    
            if (jsResult != null && !String.IsNullOrEmpty(jsResult.ToString()))
            {
                if (String.IsNullOrEmpty(jsResult.Url))
                    return false;
                else
                    return true;
            }
            else
            {
                return true;
            }
        }
    
        protected void EnsureItems()
        {
            PopulateControls();
        }
    
        private void PopulateControls()
        {
            JavaScriptSerializer jsSerialize = new JavaScriptSerializer();
            var jsResult = jsSerialize.Deserialize<YoutubeDetail>(_jsonValue);
    
            //Check there if JSON is present to populate form controls
            if (jsResult != null && !String.IsNullOrEmpty(jsResult.ToString()))
            {
                if (!String.IsNullOrEmpty(jsResult.Url))
                    YouTubeUrl.Text = jsResult.Url;
    
                if (!String.IsNullOrEmpty(jsResult.Title))
                    YouTubeTitle.Text = jsResult.Title;
    
                if (!String.IsNullOrEmpty(jsResult.Description))
                    YouTubeDescription.Text = jsResult.Description;
    
                if (!String.IsNullOrEmpty(jsResult.ImageUrl))
                    YouTubeThumbnail.ImageUrl = jsResult.ImageUrl;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                EnsureItems();
        }
    
        protected void LookupVideoDetail_Click(object sender, EventArgs e)
        {
            //If YouTube URL is present, get the information
            if (!String.IsNullOrEmpty(YouTubeUrl.Text))
            {
                YouTubeDetail yt = YouTubeHelper.GetVideoInformation(YouTubeUrl.Text);
    
                if (yt != null)
                {
                    YouTubeTitle.Text = yt.Title;
                    YouTubeDescription.Text = yt.Description;
                    YouTubeThumbnail.ImageUrl = yt.ImageUrl;
                }
            }
        }
    }
    

    From looking at my code, you've probably noticed I'm actively using a "JavaScriptSerializer" to pass all my form values as JSON. I find this is the most straight-forward way to store multiple form values in a custom control. In this case, our values will be stored within a Kentico table column in the following format:

    {
        "ID":"fLyoog562x4",
        "Title":"How The Dark Knight Rises Should Have Ended",
        "Description":"Check out HISHE\u0027s spin on the epic conclusion to The Dark Knight Trilogy: How The Dark Knight Rises Should Have Ended.",
        "Url":"http://www.youtube.com/watch?v=fLyoog562x4",
        "ImageUrl":"http://i1.ytimg.com/vi/fLyoog562x4/hqdefault.jpg"
    }
    

    Whenever I need to get those values back, all I need to do is call the JavaScriptSerializer.Deserialize method.

    NOTE: If what I have shown doesn't make any sense, it'll be useful to take a look at an in-depth tutorial on how to create a Custom Form Control in Kentico: http://devnet.kentico.com/docs/devguide/index.html?developing_form_controls.htm

    Step 2: Create YouTubeDetail Class

    In order to serialize and deserialize values when using the JavaScriptSerializer, we need to create a class object with a number of properties to interpret the JSON structure.

    public class YouTubeDetail
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
    }
    

    Step 3: YouTube Methods

    This is the part when we start using Google's YouTube API and in order for this class to work, you will need to download the necessary DLL's. I suggest you take a gander at a post I wrote a while back called "Dynamically Output A List of YouTube Videos In ASP.NET" to get an in-depth introduction into using the YouTube API.

    To get data back from YouTube you will need as a minimum requirement the DLL's and register your application in order to pass an Application Name, Developer Key and Client ID values to your application.

    public class YouTubeHelper
    {
        private static string YouTubeDeveloperKey = WebConfigurationManager.AppSettings["YouTubeDeveloperKey"].ToString();
        private static string YouTubeAppName = WebConfigurationManager.AppSettings["YouTubeAppName"].ToString();
        private static string YouTubeClientID = WebConfigurationManager.AppSettings["YouTubeClientID"].ToString();
     
        //Get YouTube video
        public static Video YouTubeVideoEntry(string videoID)
        {
            YouTubeRequestSettings settings = new YouTubeRequestSettings(YouTubeAppName, YouTubeClientID, YouTubeDeveloperKey);
            YouTubeRequest request = new YouTubeRequest(settings);
     
            //Link to the feed we wish to read from
            string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/videos/{0}", videoID);
     
            Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
     
            return videoFeed.Entries.SingleOrDefault();
        }
     
        //Extract the YouTube ID from the web address.
        public static string GetVideoID(string videoUrl)
        {
            Uri tempUri = new Uri(videoUrl);
     
            string sQuery = tempUri.Query;
     
            return System.Web.HttpUtility.ParseQueryString(sQuery).Get("v");
        }
     
        //Get required YouTube video information
        public static YouTubeDetail GetVideoInformation(string url)
        {
            Video v = YouTubeVideoEntry(GetVideoID(url));
     
            //Pass required YouTube information to custom class called YouTubeDetail
            YouTubeDetail vDetail = new YouTubeDetail();
            vDetail.ID = v.VideoId;
            vDetail.Title = v.Title;
            vDetail.Description = v.Description;
            vDetail.ImageUrl = v.Thumbnails[2].Url;
    
            return vDetail;
        }
    
        //Output YouTube property within a document by passing the Document ID
        public static YouTubeDetail GetDocumentYouTubeValue(int docID)
        {
            TreeProvider tree = new TreeProvider();
            TreeNode tn = tree.SelectSingleDocument(docID);
    
            if (tn.GetValue("YouTube") != null && !String.IsNullOrEmpty(tn.GetValue("YouTube").ToString()))
            {
                JavaScriptSerializer jsSerialize = new JavaScriptSerializer();
                var jsResult = jsSerialize.Deserialize<YouTubeDetail>(tn.GetValue("YouTube").ToString());
    
                if (jsResult != null && !String.IsNullOrEmpty(jsResult.ToString()))
                {
                    return jsResult;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
    }
    

    Step 4: Add New Control Into Kentico

    Please refer to the Kentico Development Guide (as referenced in Step 1). The main thing you need to ensure is that the control can only be used as a "Long text" type.

    YouTube Kentico Control Settings

    Step 5: Outputting The YouTube Values To A Page

    Since we have stored all our YouTube fields in a JSON string, we can get those values out by carrying out a deserialization on our document type property.

    if (CMSContext.CurrentDocument.GetStringValue("YouTubeVideo", String.Empty) != String.Empty)
    {
        JavaScriptSerializer jsSerialize = new JavaScriptSerializer();
        YouTubeDetail yt = jsSerialize.Deserialize<YouTubeDetail>(CMSContext.CurrentDocument.GetStringValue("YouTubeVideo", String.Empty));
    
        YouTubeTitle.Text = yt.Title;
        YouTubeDescription.Text = yt.Description;
        YouTubeUrl.Text = yt.Url;
    }
    

    You may think I have slightly over-engineered the process to store YouTube video's. But if you have a website that is trying to push video content along with its META data, I believe this is the way to go.

  • I've been a .NET Developer for around 6 years and it still amazes me how I can overlook something that I never really questioned. For example, when a user control is hidden, I always assumed that all the code it contained would never run since until it was made visible.

    However, after being told by one of my work colleagues that in fact a hidden user control will always run, it will just simply is hidden by the client. After searching the web for a definitive answer, I found a StackOverflow post that fully backed up his theory: http://stackoverflow.com/questions/12143693/hiding-user-controls-using-code-behind-does-internal-code-still-run-ie-am-i.

    As the StackOverflow post suggests, the most performance efficient way to show/hide a user control is by dynamically loading it in when required.

    if (jobs.Count > 0)
    {
         MyPlaceholder.Controls.Add(Page.LoadControl("/Controls/Listings/JobsList.ascx"));
    }
    

    As you can see in my example above, I'm loading my user control to a place holder in the page.

  • Even though my programming weapon of choice is .NET C#, there are times (unfortunate times!) where I need to dabble in a bit if PHP. Being a .NET developer means I do not have the setup to run PHP based sites such as Apache and MySQL.

    In the past I have tried to create an Apache configured server but I could never get it running 100% - possibly because I didn't have the patience or could justify the additional time required for setup when I could be working on a PHP site once in a blue moon...

    Last year, I came across a program called EasyPHP that allowed me to install a local instance of Apache and MySQL altogether in just one installation. It made it really to get up and running without all the setup and configuration hassle.

    Once installed you can create numerous websites and MySQL instances in a version of your own choosing. Wicked! I never been so excited about PHP in my life!

    I have only scratched the surface on the features EasyPHP provides and whenever I did need to use it, there has always been great improvements. Take a look at their site for more information:http://www.easyphp.org.

    So if you're a Windows man who needs to carry out PHP odds and ends, can't recommend EasyPHP enough.

  • A few weeks ago my Dad gave me my first mechanical keyboard. Knowing that I have a major fondness for anything that "lights up", he got me the "Ducky DK9008 Shine 2".

    The manufacturer wasn't kidding when they called their keyboard range "Shine 2". Soon as the keyboard is plugged in, things come to life and you are blinded by the most immensly bright blue light. Images on the internet will not truly justify how bright these LED's are! Thankfully, you have full control over the level of brightness and lighting modes.

    I'd be lying if I said that I've heard of the "Ducky" range of hardware peripherals until this point. Alas, I haven't. If the internet reviews are anything to go by, "Ducky" is well known in the mechanical keyboard market, providing a wide range of high quality and customisable keyboards. Nice!

    What's even better is the fact that I have the flagship model! I dread to think how much this thing costs...

    One thing I noticed after unboxing is the sheer weight. It's definitely one of the bulkiest keyboards I've ever handled. But this piece of hardware exudes quality and workmanship. Well this is something you'd expect from a top of the range keyboard that is on the higher end of the price spectrum.

    There has always been a misconception that mechanical keyboards are known for being stiff and produce a loud clacking noise. This couldn't be further from the truth with DK9008 Shine 2. This particular model uses the Cherry MX Brown switches, which produce a sharp response whether you are the kind of person that is a "heavy hitter" or who has the "lighter touch". Either way, the keys feel light to press with minimum effort and produce a nice satisfying "click".

    I've tried this keyboard for both standard typing and gaming. It's a great all rounder.

    One things for sure. Once you go mechanical you will never want to go back to the traditional membrane switch keyboard.

    For more info on the Ducky DK9008 Shine 2, take a look at their website: http://www.duckychannel.com.tw/en/DK9008_shine2.html

    And yes. This blog post was typed up using the Ducky DK9008 and I forsee all future posts will be typed up in the same manner. :-)