3

In our program we store a large volume of data in a cache. The cache is a Dictionary. As large volume of data is in-memory, we plan to use a database instead of dictionary. So all data that was once in the dictionary will be moved to a table in database. Now the problem is, the dictionary cache is been used in many places in the project. Please suggest me a design pattern to effectively replace the dictionary cache with database with minimal code changes.

The cache will be like Dictionary(string, MyBusinessObject). The dictionary key will be 'Id'. MyBusinessObject will have the data in it's properties. Eg- myBusinessObject.CustomerName, myBusinessObject.CustomerAge. Throughout my project, various modules will search for the Id (key) in the dictionary.

Ram
  • 44
  • 1
  • 13
  • Can you provide an example of how you currently would interact with the dictionary? – StingyJack Apr 24 '13 at 16:51
  • Does any of [this](https://www.google.com/search?q=Database+Backed+Dictionary+C%23) help? [Looking for a simple standalone persistent dictionary implementation in C#](http://stackoverflow.com/questions/100235/looking-for-a-simple-standalone-persistent-dictionary-implementation-in-c-sharp) seems interesting. – Miserable Variable Apr 24 '13 at 16:53
  • If you are using standard Dictionary class then the clients will need code change. If it is custom class it can be modified to get data from the table. – Miserable Variable Apr 24 '13 at 16:53
  • @StingyJack The cache will be like Dictionary. The dictionary key will be 'Id'. MyBusinessObject will have the data in it's properties. Eg- myBusinessObject.CustomerName, myBusinessObject.CustomerAge. Throughout my project, various modules will search for the Id (key) in the dictionary. The dictionary, if id is present will return the corresponding business object. Please ask me if I need to provide more information. – Ram Apr 24 '13 at 16:59
  • Please edit the question with those details – StingyJack Apr 24 '13 at 17:00
  • A cache-enabling pattern implementing IDictionary should be sufficient. The real problem you'll have is if any of your code tries to enumerate the dictionary. – RBarryYoung Apr 24 '13 at 17:15

2 Answers2

9

Dictionary implements the IDictionary<TKey, TValue> You should create a facade over the database that also implements the IDictionary<TKey, TValue> Then making sure you no longer use the concrete dictionary. Lastly you will need to create a factory for the new abstraction which will allow you to replace it with a mock for testing.

rerun
  • 25,014
  • 6
  • 48
  • 78
  • +1: This is ideal approach. I think one can just derive from Dictionary to do the same with potentially less impact on other code. – Alexei Levenkov Apr 24 '13 at 17:03
3

Have you considered PersistentDictionary? It's open-source Dictionary that's built on top of the ESENT database engine. You get to use Dictionary that can store up to 16TB.

Ed Power
  • 8,310
  • 3
  • 36
  • 42
  • Thank you for PersistentDictionary. Looks promising. I never know that before. Our database is Sqlite. – Ram Apr 24 '13 at 17:21