Fremus.co.za

Demistifying Life and Web Development

Archive for November, 2010

Product Navigation Design

As I mentioned in my previous blogpost I am busy working on a website at the moment and a part of the website display dynamic product-related content. I believe that the product navigation is not perfect yet and I think it might be a good idea to take a look at some examples and draw a few conclusions from it. The first example I looked at this morning was Hi Fi Corporation (I was looking at some LCD tv’s) and immediately I started looking at the way the product navigation was done, and here are some of my thoughts:

Products are divided into categories and each category is displayed in two areas, the horizontal menu at the top and a vertical-collapsible menu to the right. Each category links to its own individual page, e.g. prods/audio for Audio products. When you click through to a category page the category bar remains to the left, with another “product filter” bar that appears below it.

On the category page for audio, here, there is a product comparison box and a paging component. Notice that the pages aren’t individual pages but javascripty/ajaxy items. Each product however has its own dedicated permalink. Isn’t it equally important to link to pages? Or is that not that important due to the nature of the data?

If you go to another website, Take 2 (my favourite online retailer for DVD’s and books), you will notice that if you click through to electronics (which is a category much like the Hi Fi Corporation example) you get to a page with sub categories to the left, which when clicked on (e.g. Mp3 Players) produces a paged result page. The difference from the Hi Fi Corporation example is that the pages are not ajaxy and you could probably access an individual page with a link – whereas the Hi Fi Corporation example you couldn’t. I’m not saying its a bad thing, just stating the difference. How would you design a product pager? Make it URL accessible? If you take a look at this Amazon.com page you will see that the paging is ajaxy. The Amazon.com page I just linked to is also bound by category, software architecture, as was the Take 2 and Hi Fi Corporation example. Would it be wrong to surmise that categories are permalinks and the products/items on them are page-able and that the individual pages are not permalinks? I can understand why a page within a category would not make sense as a permalink – simply because the data might change and move around. If 20 new books on software architecture were added today that would affect the pages. You could also argue that a page could give you the top 10 new items which might make sense in a MVC architecture where you could apply action filters.

What would the ideal product navigation design be? Well the ideal product navigation design would take the functional aspects into consideration and produce an effective solution.

  • Share/Bookmark
posted by fr3dr1k in Web Design and have No Comments

A Quick Look at some website design / functionality

I have been working on a new website recently and its far from perfect – in fact I see it being refactored several times before I think it will reach an optimum phase or place. That being said business people don’t buy-in or the see the value in refactoring, so its something only the developer is aware or should be aware of. I digress.

Parts of the design I have been working on seems to have been using scrollable areas a lot and personally I see that as a fatal design flaw for two major reasons:

  • You are forcing design onto the natural flow of information on a web page. Surely the design should work with the flow and nature of a website and not against it. Content is the most important asset on your website and should ideally have a natural flow to it. Personally if you are using too many scrollable areas, you should re-look your design.
  • Not many websites use scrollable areas. Its ok to be different, not indifferent.

In case you are wondering what I mean by scrollable areas, it refers to applying the height property to an area and setting the overflow: auto. The effect is that you have a scrollable area. I cannot find many sites that use this technique actually.

  • Share/Bookmark
posted by fr3dr1k in Web Design and have No Comments

WCF, ASP.NET Webforms, ASP.NET MVC and jQuery

I haven’t written a technical article in a while but I thought I would write one that discusses AJAX calls with jQuery. Before I get into it I would like to reiterate some of my personal development objectives of late:

  1. Don’t do ANY UI manipulation in any of my server side API’s. The sole purpose of any API within the applications I build is to deal with business layer logic and the data related to it. Ideally speaking I would want my API to return structured data which is easily transformed into a format for the client application using the API (e.g. JSON). I have had experiences within another development team in the recent past where the API returned HTML with the data and to say that this is a mess is understating the obvious – if you think about it quickly then you can understand the impact of one style or layout change will have on this approach. It also blurs your application in terms of its concerns and makes testing a problem. So ideally I dont want any UI logic in any of my API’s.
  2. The second objective I have is that all my UI for web applications must be handled with client-side scripts. Client side includes both static HTML, CSS and JavaScript, and in this instance I specifically refer to JavaScript and the handling of my API’s data using JSON, for which I use jQuery. The REST (:D) of this article is dedicated to the AJAX options available in an ASP.NET application.

Dealing with AJAX in an ASP.NET Application

Developing ASP.NET applications gives you two “frameworks” or “architectures” to work with:

  1. ASP.NET Webforms
  2. ASP.NET MVC

Notice that each architecture or framework uses the ASP.NET runtime which in turn runs on the .NET framework, and part of the .NET framework is the Common Language Runtime (CLR). People often think ASP.NET automatically refers to webforms. The difference between the architectures lies both in how it was designed and who it was designed for. Webforms was designed for Windows Forms developers working within the Visual Studio drag-and-drop environment and aimed to provide an abstraction of Windows Forms for the web. With webforms you could simply drag and drop controls onto a design surface and assign properties to the controls. The problem with webforms was that it required state management and to deal with the state management viewstate was created. Basically state management refers to a controls ability to retain its “value state” across postbacks. So if I created a simple label and set its text to “hello world” viewstate is used to retain that text value throughout the life of the rendered page. Webforms also introduced the concept of the Page Life Cycle, which exposed certain events at certain points of the page’s rendering process. The problem with viewstate is that the viewstate is inserted into your HTML pages which causes the page to bloat – so there is no clean markup. Viewstate totally goes against the grain of how the web works – being stateless.

ASP.NET MVC addresses some of the issues associated with webforms by using a mature and well known architectural pattern – Model-View-Controller, as used in Ruby-On-Rails. ASP.NET MVC does not use the page lifecycle for a start and it embraces the web’s statelessness instead of fighting it. The result is that you have no ViewState and this means that there is no HTML bloat and you also have complete control over your HTML.

There are a couple of ways to consume AJAX requests in ASP.NET but they fall into two categories:

  1. SOAP
  2. REST

In my previous development role I worked with guys that used SOAP to do AJAX requests and although this approach was good enough it was specifically aimed at ASMX web services which meant adding web references to your project. Needless to say adding these reference is a pain in the ass, because if a new method was added to a service you had to first update your reference before you could see the method. REST, however, addresses this issue by allowing you to make calls to URL’s, and not by adding any references. It also means that if a new method is created its available immediately. The remainder of this article is about consuming REST-like services in an ASP.NET application.

With my latest development effort I started out by using Page Methods in an ASP.NET Webform application. Why ASP.NET webforms you may ask? Well I wanted to focus on the business value and I just started out with Webforms without concerning myself too much with the technical side of things. Page methods implement a REST-like architecture and simply requires that you add a WebMethod attribute in your code-behind’s CS file like this:

    [WebMethod]
    public static Dictionary> GetLastSixMonthReport()

You could then access the web method through jQuery like this (notice the Default.aspx/GetImportsLastSixMonths):

    $.ajax({
        type: "POST",
        url: "Default.aspx/GetImportsLastSixMonths",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var dict = msg.d;
            var html = '';
            $("#importsData").empty();
            html += dict;
            $("#importsData").append(html);
        }
    });

The main issue I have with this approach was that if I wanted to create a new web project I had to re-create the web methods – which is not ideal. So if I wanted to move this project over to a new MVC app I would have to recreate the methods. Based on this I decided to create a separate WCF project that could host all my methods and deliver them in a REST-JSON way. To get WCF to deliver JSON in a REST fashion you need to do two important things:

  • Configure it
  • Add the appropriate Interface attributes

Configuring a WCF service for JSON requires that you add an endpoint address to the service itself like this:


You also need to add an endPointBehavior like this:

				
					
				
			

In your service behaviours you need to set httpGetEnabled to true as well. I added this in my service class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

In service contract I added the WebInvoke attribute to each method, like this:

    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    ProductCollectionView[] GetProductCollectionViews();

Once I set this up I could access the JSON-enabled endpoint: http://localhost/LRB2CServiceLayer_v1/LRB2c.svc/ajaxEndpoint/GetProductCollectionViews and I would see the JSON. Notice that the JSON comes with a MethodNameResult as opposed to the “d” from the page methods. Consuming it uses a similar approach to the jQuery for page methods, except that you use a GET (which is a little dodgy):

    $.ajax({
        type: "GET",
        url: "http://localhost/LRB2CServiceLayer_v1/LRB2c.svc/ajaxEndpoint/GetProductCollectionViews",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var dict = msg["GetProductCollectionViewsResult"];
            var html = '';
            $("#importsData").empty();
            html += dict;
            $("#importsData").append(html);
        }
    });

I decided to convert the WCF service to a class library (DLL) instead because I had all kinds of issues with XSS because I ran the WCF service as a separate web application. The idea was that a WCF service was easily shared between web apps. I basically combined the class library with a MVC web app instead. A class library can just as easily be shared among multiple projects or web sites. The JSON that MVC returns is different to WCF and Webforms – it just returns the object, no indexer/key such as “d”. Getting MVC to return JSON is pretty straight forward:

        [HttpPost]
        public JsonResult GetGetProductCollectionViews()
        {
        }

Of the three methods/technologies I described I like the ASP.NET MVC approach because:

  • Its RESTful
  • The JSON result only contains the object(s)
  • Share/Bookmark
posted by fr3dr1k in AJAX,ASP.NET,ASP.NET MVC,Web Development,Web Technologies and have No Comments
Get Adobe Flash playerPlugin by wpburn.com wordpress themes