Efficient way to cache frequently used lists in entity framework 4.0

In a .Net c# application we are designing we have several tables with a small number of lookup values.  For example we have a table Payment which has FKs to table Status (Complete, Pending, Refunded) and table Type (Cash, CreditCard, BankTransfer).  

We are using Entitiframework 4.0 .  The table Status and Type have very few entries that rarely change so I don't want to keep querying the DB over and over again for their values.  What is the best way to cache the values so the entities can be smarter?
canuckconsultingAsked:
Who is Participating?
 
ericpeckhamCommented:
The way I do this is to create my own list class in the same namespace as your entities, as in the following example, where the type I want to cache the list for is named ItemType.  I also like to throw in a couple of indexers.  The list itself is lazy-loaded, meaning it isn't fetched from the database until it's needed.

 
namespace MyDB
{
  public class ItemTypeList : List<ItemType>
  {
    private ItemTypeList()
    {
    }

    private ItemTypeList(List<ItemType> list)
      : base(list)
    {
    }

    private static List<ItemType> _ItemTypes;
    public static ItemTypeList ItemTypes
    {
      get
      {
        if (_ItemTypes == null)
        {
        	// No constructor argument - uses named connection string from app.config:
          using (MyEntityDB db = new MyEntityDB())
          {
            _ItemTypes = db.ItemTypes.ToList();
          }
        }
        return new ItemTypeList(_ItemTypes);
      }
    }

    public new ItemType this[int itemTypeID]
    {
      get
      {
        return Find(i => i.ItemTypeID == itemTypeID);
      }
    }

    public ItemType this[string typeName]
    {
      get
      {
        return Find(i => string.Compare(i.TypeName, typeName, true) == 0);
      }
    }
  }
}

Open in new window

0
 
cubaman_24Commented:
Hello:
Take a look at System.Web.Cache class.
http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

Here are some examples of caching at different levels
http://authors.aspalliance.com/aspxtreme/webapps/aspcachingfeatures.aspx

Best regards
0
 
canuckconsultingAuthor Commented:
Thank you and sorry for delay
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.