Archive for the ‘Best Practice’ Category

10 ASP.NET Performance and Scalability Secrets

5 February, 2008

An article by Omar Al Zabir explains 10 easy ways to make ASP.NET and AJAX web sites faster, more scalable and support more traffic at lower cost. Omar Al Zabir is the CTO of Pageflakes one of the best mashups.

Main points he has discussed in this article

  • ASP.NET pipeline optimisation
  • ASP.NET process configuration optimisation
  • Things you must do for ASP.NET before going live
  • Content Delivery Network
  • Caching AJAX calls on browser
  • Making best use of Browser Cache
  • On demand progressive UI loading for fast smooth experience
  • Optimise ASP.NET 2.0 Profile provider
  • How to query ASP.NET 2.0 Membership tables without bringing down the site
  • Prevent Denial of Service (DOS) attack

Read the full article at code project 10 ASP.NET Performance and Scalability Secrets

Option strict and option explicit defined…

23 January, 2007

A friend and coworker asked me today why I use ‘Option Explicit’ and ‘Option Strict’ at the top of all my .net (and for that matter VB6/asp 3.0) classes. I thoguht I’d record the answers here.

‘Option Explicit’ ensures you declare all variables before use. VB.NET doesn’t allow this anymore but in the past it was possible to build an entire application without declaring a single variable!

‘Option Strict’ keeps VB developers coding bad casts as below:

dim x as string = “true”
dim y as boolean = x

or

dim ctl as control
ctl = new textbox

If you’re pure C# you probably can’t believe that this is actually possible. Well it is. More to the point VB developers have bad habits from the ‘days of old’ and code things like this without even batting an eyelid. NOTE: By default VB.NET still allows these bad casts

Option Explicit Statement
By default, the Visual Basic .NET or Visual Basic 2005 compiler enforces explicit variable declaration, which requires that you declare every variable before you use it.

Option Strict Statement
By default, the Visual Basic .NET or Visual Basic 2005 compiler does not enforce strict data typing.

In Visual Basic .NET, you can convert any data type to any other data type implicitly. Data loss can occur when the value of one data type is converted to a data type with less precision or with a smaller capacity. However, you receive a run-time error message if data will be lost in such a conversion. Option Strict notifies you of these types of conversions at compile time so that you can avoid them.

Javascript menus – powered by underordered lists

2 November, 2006

I’ve been working with unordered lists as menus for a while now.

The basic concept is you take most – if not all – of the inherent styling from the <ul> to create a basic one tier menu. The trouble then comes when you want to make the menu open out on hover of a node – this requires javascript. I have been playing with building my own menus using jQuery and the code, frankly, was starting to become a tad messy when I got round to adding effects (‘fx’ in jQuery speak).

I had a quick look round the web and found this beauty from twinhelix. I’ve used it a number of times now and with changes to the stylesheets you can make this menu behave and look exactly as you please. It has built in fade/blind effects and the ability to add images to submenus.

The one key advantage.
Accessibility. If you were to have javscript disabled the menu wouldn’t work but what would happen is the <ul> would just render to the page and the menu would still make perfect sense.

Brilliant.

Using CSS to achieve tabular layout

25 September, 2006

I am told I shouldn’t use tables for anything but tabular data. I’m looking for a way to correctly align form fields using DIVs.

With tables, I would do the following:

<TABLE>
<FORM>
<TR><TD>Field 1:</TD><TD><input type=text></TD></TR>
<TR><TD>Field 2:</TD><TD><input type=text></TD></TR>
</TABLE>
</FORM>

This can be built something like this:

<div>
<label for=”field1″>Field 1:</label>
<input id=”field1″ type=”text”>
</div>
<div>
<label for=”field2″>Field 2:</label>
<input id=”field2″ type=”text”>
</div>

And this is an example CSS I’ve used:

label {
width: 35%;
float: left;
clear: left;
text-align: right;
white-space: nowrap;
min-width: 5em;
}

XML best practice reminders

18 September, 2006

Example Document

<?xml-stylesheet type="text/xsl" href="name.xsl"?>
<!-- Aaron Skonnard's name structure -->
<x:name xmlns:x="http://example.org/name">
<first>Aaron</first>
<!-- middle initial optional -->
<last>Skonnard</last>
</x:name>
<!-- end of name -->

Processing Non-Repeating Elements

One way is to call Read to move past the text node and then ReadEndElement to consume the element’s end tag marker. The pattern for dealing with text-only elements is shown here:

r.ReadStartElement("first");
Console.WriteLine("first: {0}", r.Value);
r.Read(); // moves past text node
r.ReadEndElement(); // first

This pattern can be summarized into the following steps:

  1. Call ReadStartElement to consume the start tag
  2. Retrieve the element’s text content through the Value property
  3. Call Read to move off the text node
  4. Call ReadEndElement to consume the end tag

To simplify using this pattern, the designers introduced another helper method called ReadElementString that encapsulates this behavior. Using ReadElementString makes it possible to simplify the code even further:

XmlTextReader r = new XmlTextReader("name.xml");
r.ReadStartElement("name", "http://example.org/name");
Console.WriteLine("first:{0}", r.ReadElementString("first"));
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name

You’d be hard-pressed to simplify the code more than this.

Repeating Elements

Dealing with repeating elements also presents a problem since you have to check the name of the next element before committing to the ReadStartElement or ReadElementString call. The following code illustrates how to process the name element assuming it may contain zero or more first elements followed by a mandatory last element:

XmlTextReader r = new XmlTextReader(@"name.xml");
r.ReadStartElement("name", "http://example.org/name");
bool more=true;
while (more)
{
r.MoveToContent();
if (r.LocalName.Equals("first"))
Console.WriteLine("first: {0}",
r.ReadElementString("first"));
else
more=false;
}
Console.WriteLine("last: {0}", r.ReadElementString("last"));
r.ReadEndElement(); // name