0

I have situation where in the Repository class I have a dictionary:

Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> ()

with methods for manipulating data (Get,Save,Add etc).

Now there is a need to search by value from the inner map, not only by key (in this example SourceColumnName).

My question is what would be best approach here? I want to add another dictionary but with reversed data:

Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> map2 = new Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> ()

But the problem is memory usage, is that a waste of memory? But if I implement lookup by value, I'll lose the main advantage of dictionary.

The data stored in the dictionary is around 20 * 5k data in the inner map. Any suggestion how to design / refactor this class would be appreciated.

EDIT1: Micro optimisation post didn't resolved my problem. I work on project where optimisation is very important.

NSKBpro
  • 219
  • 1
  • 7
  • Possible duplicate of [Is micro-optimisation important when coding?](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Feb 06 '19 at 10:50

1 Answers1

2

The type you're looking for is generally called a 'bidirectional dictionary'; I found a C# implementation in this Stack Overflow answer, but you're on the right track; you'll need two dictionaries, so memory usage will be higher. Whether this is worth the increase in performance you can only find out by testing and measuring.

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
  • Firstly thanks for answer. This means that 2 dictionary is good option and i will accept this answer. – NSKBpro Feb 06 '19 at 11:26