Question - Design Name/Value List Objects

Question - Design Name/Value List Objects

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


foxhard posted on Wednesday, January 06, 2010

Hi.

In my work some people are using this way to coding nameValueList classes...I don't know if this is correct.


------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------
    [Serializable()]
    public class LoanSimpleList : Csla.NameValueListBase<int, string>
    {

        #region Factory Methods
        private LoanSimpleList()
        { /* require use of factory method */ }

        private static LoanSimpleList _list;

        public static LoanSimpleList GetLoanSimpleList()
        {
            //if (_list == null)
                _list = DataPortal.Fetch<
LoanSimpleList>(new Criteria(typeof(LoanSimpleList)));
            return _list;
        }

        public static LoanSimpleList GetLoanSimpleListByClientId(
int clientId)
        {
            //if (_list == null)
                _list = DataPortal.Fetch<
LoanSimpleList>(new SingleCriteria<LoanSimpleList,int>(clientId));
            return _list;
        }
                              
        /*public static void InvalidateCache()
        {
            _list = null;
        }*/

...
}
------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------

Then this class is used like this:

private LoanSimpleList _loanList;

public void Method1()
    _loanlist = LoanSimpleList.
GetLoanSimpleList():
}

public void Method2(int clientId)
{
    _loanlist = LoanSimpleList.
GetLoanSimpleListByClientId(clientId):
}


Before you can see the the line //if (_list == null), this is comment, because the data must be changed in each call.

My question is if that is OK?
...maybe the correct way will be uncomment that lines and use the InvalidateCache Method...like this:


private LoanSimpleList _loanList;

public void Method1()
         LoanSimpleList.
InvalidateCache();
         _loanlist = LoanSimpleList.
GetLoanSimpleList():
}

public void Method2(int clientId)
{
    LoanSimpleList.
InvalidateCache();
    _loanlist = LoanSimpleList.
GetLoanSimpleListByClientId(clientId):
}


Then....What is correct?....How you explanain the correct use of InvalidateCache Method?...

Regards,

rsbaker0 replied on Thursday, January 07, 2010

This is definitely not going to do what you want unless you use the second variation, where you explicitly discard the saved list on each fetch using the InvalidateCache method, and then you have lost the benefit of keeping a static list around anyway. (If you don't discard the list each time, then you'll get the wrong list if you retrieve for a different client id).

An improvement on this that would take advantage of the caching would be to also keep the last clientId that goes with the current value of _list. That way, repeated fetches for the same client id (or the global list) will return the previously fetched list as long as it matches.

I have the impression that the most common use of NameValue lists is for an essentially static set of values that is lazy-loaded just once for the duration of the application. The InvalidateCache method allows for forcing the list to be refetched (for example, if the original source of the underlying values is changed, then you would want to refetch the list on the next call)

Copyright (c) Marimer LLC