Tuesday, August 31, 2010

What is cool about c# generics, why use them?

I thought I'd offer this softball to whomever would like to hit it out of the park. What are generics, what are the advantages of generics, why, where, how should I use them?

  • Allows you to write code/use library methods which are type-safe, i.e. a List<string> is guaranteed to be a list of strings.
  • As a result of generics being used the compiler can perform compile-time checks on code for type safety, i.e. are you trying to put an int into that list of strings? Using an ArrayList would cause that to be a less transparent runtime error.
  • Faster than using objects as it either avoids boxing/unboxing (where .net has to convert value types to reference types or vice-versa) or casting from objects to the required reference type.
  • Allows you to write code which is applicable to many types with the same underlying behaviour, i.e. a Dictionary<string, int> uses the same underlying code as a Dictionary<DateTime, double>; using generics, the framework team only had to write one piece of code to achieve both results with the aforementioned advantages too.

What's your choice for your next ASP.NET project: WebForms or MVC?

Let's say that you will start a new Asp.Net web site/application tomorrow. What your choice between WebForms and MVC, and why?

I'd choose asp.net MVC for the following reasons:
  1. ASP.NET MVC has borrowed so much from Rails that it feels like a direct port in some ways (and that's a good thing in my mind).
  2. AJAX is important, but I hate the Microsoft "Atlas" approach to AJAX (whatever the product name is these days). If you're going to do AJAX, you need to understand the HTML and the JavaScript. Frameworks that hide that from you are hurting you more than they are helping you (IMO).
  3. JQuery has taken over the world it seems in terms of JavaScript frameworks. ASP.net MVC is well-integrated with it. I want to learn it, so there's great alignment here.
  4. The whole "control" model is a neat idea, but it is more complicated than it appears on the surface. For example, look around on SO for questions about how a UserControl can find its highest level containing control and so forth. The control hierarchy abstraction has leaks in it. Grids are great if they do what you want out of the box, but it's very very hard to customize them to do something they weren't made to do. And the best grid controls on the market (the ones that are highly customizable) are large, bloated, overly complicated beasts. Maybe that shows us that we should drop back down to HTML and let loops in our views do that kind of thing for us.
  5. I believe I can build complete, beautiful apps in ASP.net MVC much faster than in ASP.Net (and I've got some years of ASP.Net under my belt). Look at StackOverflow ... built quickly on ASPMVC with JQuery, and it's fast, scalable and a joy to use IMO.
  6. Oh, and it's completely open source! It is ok to read the source code, blog about it, and even modify then redistribute it!
So be wise in choosing it...

Monday, August 30, 2010

Latitude and Longitude Lookup with jQuery, C#, ASP.NET MVC

This asp.net mvc controller action returns a place latitude and longitude based on zipcode and country name,

public ActionResult LookupCoordinates(string Zip, string Country)
    string Lat = "";
    string Lon = "";
    string PostUrl = "http://ws.geonames.org/postalCodeSearch?postalcode=" + Zip + "&maxRows=10&country=" + Country;
    WebResponse webResponse = webRequest.GetResponse();
    if (webResponse == null)
    { }
        StreamReader sr = new StreamReader(webResponse.GetResponseStream());
        string Result = sr.ReadToEnd().Trim();
        if (Result != "")
            // Load the response into an XML doc
            XmlDocument xdoc = new XmlDocument();
            //  Navigate to latitude node
            XmlNodeList name = xdoc.GetElementsByTagName("lat");
            if (name.Count > 0)
                Lat = name[0].InnerText;
            //  Navigate to longitude node
            name = xdoc.GetElementsByTagName("lng");
            if (name.Count > 0)
                 Lon = name[0].InnerText;
    return Json(Lat + "," + Lon);

and the jquery function for implementing this would be,
function LookupCoordinates(zip) {
        { Zip: zip, Country: "US" },
        function(data) {
            var result = eval('(' + data + ')');
            var coordinates = result.split(",");

Change contry to IN for india... 

Thursday, August 26, 2010

Images in asp.net performance booster web config setting...

If your asp.net website deals with images just do this in your web.config file and see the performance and no drawbacks of doing this.

Under system.webServer in web.config set for example

                <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
                <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
                <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />