LINQ - Querying an ArrayList of Objects

by Aaron 09/17/08

Background

I use ArrayList heavily to load datasets into memory to minimize database queries.  This creates huge performance increases, especially for retail oriented sites with lots of product browsing.  I had implemented sorting and searching prior to LINQ but doing this involved somewhat archaic means (IComparer, and foreach loops)

I knew there had to a better way to filter an ArrayList than using a foreach loop so I looked into the LINQ functionality.  I found this MSDN article that explained most of what I wanted.  I also wanted to put the data back into an ArrayList so I had to figure that out too.  This was very easy turns out that there is an ArrayList constructor that excepts a generic list and a LINQ query has a ToList() method. 

Example

In the example below I wanted to create a method that took an ArrayList of Product Objects and returned only the active products as an ArrayList (Product has a bool Active property):

public static ArrayList getActiveProducts(ArrayList ProductList)
{
      var query = from Product p in ProductList
      where p.Active == true
      select p;
      return new ArrayList(query.ToList());
}

This code is much cleaner and efficient than using a foreach loop to copy the matching items into a new ArrayList.  There is a bit of a learning curve with LINQ but it is a great tool because it is so flexible and works with any imaginable data-source.  By the way there are also order by statement in LINQ, so I could have re-ordered my list very easily too w/o having to extend IComparer.

Tags:
Category:

Javascript Check if File Exists On Server

by Aaron 05/15/08

I have been working diligently on implementing some new features into Velowear.com.  One of these features is an in-page image popup.  Originally I had coded it a an asp.net control within an AJAX panel, but found that this approach was not as flexible as just using pure Javascript.  The only problem was that I wanted to perform some server side checking if the image existed before triggering the in-page popup.  Most of what I had read said this was impossible to do via Javascript, but I knew there had to be a way...

Luckily I stumbled across some Javascript code that uses the XMLHttpRequest Object (this is the core of how AJAX works BTW) that performs this tasks with relative ease.  It uses the XMLHttpRequest object to try and download the file (str) passed into the isFile function.  Supposedly this only works when calling the server that originated the request. 

I hope this helps you out as much as it helped me.


Here is the code:
 

function isFile(str){
    var O= AJ();
    if(!O) return false;
    try
    {
        O.open("HEAD", str, false);
        O.send(null);
        return (O.status==200) ? true : false;
    }
    catch(er)
    {
        return false;
    }
}
function AJ()
{
    var obj;
    if (window.XMLHttpRequest)
    {
        obj= new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try
        {
            obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
        }
        catch(er)
        {
            obj=false;
        }
    }
    return obj;
}

This code was buried in the middle of the following thread found here:
http://www.codingforums.com/archive/index.php?t-98182.html

Tags:
Category:

Threading and System.Timers in asp.net

by Aaron 02/01/08

Warning this post is very techy and boring.  Do not read unless your a programmer.

Purpose

I wanted to create a scheduled task (in a separate thread) to run every couple of hours on an ecommerce site, and could not find a good solution.  After much googleing and piecing some things together I figured it out and it is really easy to do this (See code at bottom of post).  There are great articles on creating/using threads in asp.net, so I don't feel a need to cover it here (just make sure to handle all of your errors in a thread or it will fail w/o throwing an error in the main application which can be confusing).

Background

My day job includes building/maintaining the websites for Velowear.com.  Velowear.com uses a overblown order management system called Ecometry to maintain products and everything else to do with running a direct retail business.  The web interface to Ecometry,webcom.dll a com object, is a very archaic and non object oriented approach to getting at this data.  One of the main problems is that whenever our website needed to query data from Ecometry it has to do a round trip over a T1 connection between our web host and our facility, which isn't very efficient.  Plus when you want to merchandise your product a certain way (i.e. display what colors are available on category search) I realized that we needed some kind of product caching system.  Luckily I had pretty much created my own objects that represented our products as they fit our business, now the challenge was creating an updated list of these product objects.

The first step in this process was creating a separate thread that would query all of our products in Ecometry.  This process takes ~ 30 minutes when running on our web server and talking to our App server (at a different location).  The product objects have a timeout feature which allows them to refresh themself after a certain amount of time has passed or if their inventory level is low to make sure the data is always fresh.  The cache is first written out to a binary file on the web server and then loaded into memory, then if the web server restarts it can grab the cache file and keep responding to request w/o having to wait 30 minutes to finish querying data from Ecometry.

This caching has been working well for the past month and a half, but there was still an important component missing, what happens when you add new product, but they are not in the cache?  Well technically you can visit the product page and it will look for the product in the cache and then try to query Ecometry for the data, if the data is found, then it is added to the in memory cache.  The problem is when you want 25 new products to 'go live' this is not a great option. 

What is so cool about ASP.net is that I can schedule this cache refresh process to happen every 4 hours.  This in turn keeps my binary cache file almost in real time with our real product database, which is super cool. 

Below is the example code (in C#) for creating a scheduled event (place this code in your global.asax file):

void Application_Start(object sender, EventArgs e)
{
      //4 hour Timer
      System.Timers.Timer tm = new System.Timers.Timer(4 * 360 * 1000);
      tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
      tm.Start();
}

void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
       //THIS CODE WILL RUN EVERY FOUR HOURS
       //Code here to re-get cache file

}

Tags:
Category:


More Posts
showing: 3 posts, sorting by: most recent