How best to query and cache database result set in a NameValueListBase object?

How best to query and cache database result set in a NameValueListBase object?

Old forum URL: forums.lhotka.net/forums/t/9045.aspx


j055 posted on Monday, June 07, 2010


Hi there

I would like to cache the database result on the client after the DataPortal_Fetch has fired for the first time. Is this a reliable approach?

1. In the factory method check a static field for some data
2. If it's null call DataPortal.Fetch() first and copy to the field
3. Query the cached data and return.

It means that I need to populate the list on the client-side rather than on the server in the DataPortal_Fetch method, e.g.

public static GroupsList GetList(// some optional params
    )
{
    if (_data == null)
        DataPortal.Fetch<GroupsList>();

    // query or remove items from _data
    var list = new GroupsList();
    list.RaiseListChangedEvents = false;
    list.IsReadOnly = false;
   
    foreach (var item in _data)
        list.Add(new NameValuePair(item.GroupID, item.Name));

    list.IsReadOnly = true;
    list.RaiseListChangedEvents = true;
   
    return list;
}

This does look a bit clunky. Is there a more appropriate pattern I should follow?

Thanks
Andrew

rxelizondo replied on Monday, June 07, 2010

If there any particular reason why not just do:

 

public static GroupsList GetList()
{
    if (_data == null)
        _data = GroupsList .Get();

    return _data;

}

That static property could reside on your "GroupsList" object.

Of course, if you are going to have optional parameters then you are going to have to have some logic to see if you already have retrieve a list with those specific parameter values, if yes then return that list if not then you have to execute the fetch again.

 

 

j055 replied on Monday, June 07, 2010

Hi Rene

Yes I do need to query the data before it's added to the NameValue list. Do you think my approach above is acceptable? Do I need to do any other checks on the _data field for example?

Thanks
Andrew

rxelizondo replied on Monday, June 07, 2010

Hi Andrew,

I guess I am just not sure why are you refilling the object again with the cached data. I am sure I am missing something as usual but here is a quick example of somethign that I may consider doing (this sample code was not compiled or tested):

---------------------------

private static Dictionary<int, GroupsList> cachedLists = new Dictionary<int, GroupsList>();
public static GroupsList GetList(int param)
{
    GroupsList _data;
    cachedLists.TryGetValue(param, out _data);

    if (_data == null)
    {
        _data = DataPortal.Fetch<GroupsList>();
        cachedLists[param] = _data;
    }

    return _data;
}

j055 replied on Tuesday, June 08, 2010

Hi again

Your suggestion is good but it does mean a new database hit for each int value. Perhaps I'm trying to over engineer the solution. In practice there may only need to be 4/5 items in the cache array.

I'm just trying to find an elegant solution within CSLA BOs.

Cheers
Andrew

RockfordLhotka replied on Tuesday, June 08, 2010

I usually use the technique Rene demonstrated.

But if you want to get all the possible lists in one call - pre-populate the whole cache - you might consider creating another ReadOnlyBase object that knows how to load the entire list of NVL objects. You can use that specialized object in your list's factory method to load the cache - and then simply use Rene's code to return items out of the cache on demand.

Copyright (c) Marimer LLC