ORM까지는 아니지만 RDB 구조를 간단하게 저장하여 처리하고자
Multi Key Dictionary를 사용하여 보았습니다.
출처는 stackoverflow 입니다.
출처 : http://stackoverflow.com/questions/1171812/multi-key-dictionary-in-c
Dictionary는 key, value의 조합으로 되어 있는데,
value에 Dictionary를 넣으면 key를 2개를 사용할 수 있게 됩니다.
- Key 1개 : <K, V>
- Key 2개 : <K, <K, V>>
Key를 3개 이상 쓰고 싶다면 이를 반복하면 됩니다.
- Key 3개 : <K, <K, <K, V>>>
- Key 4개 : <K, <K, <K, <K, V>>>>
이러한 아이디어로 작성된 코드는 아래와 같습니다.
public class MultiKeyDictionary<K1, K2, V> : Dictionary<K1, Dictionary<K2, V>>
{
public V this[K1 key1, K2 key2]
{
get
{
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
throw new ArgumentOutOfRangeException();
return base[key1][key2];
}
set
{
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
this[key1][key2] = value;
}
}
public void Add(K1 key1, K2 key2, V value)
{
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
this[key1][key2] = value;
}
public bool ContainsKey(K1 key1, K2 key2)
{
return base.ContainsKey(key1) && this[key1].ContainsKey(key2);
}
public new IEnumerable<V> Values
{
get
{
return from baseDict in base.Values
from baseKey in baseDict.Keys
select baseDict[baseKey];
}
}
}
public class MultiKeyDictionary<K1, K2, K3, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, V>>
{
public V this[K1 key1, K2 key2, K3 key3]
{
get
{
return ContainsKey(key1) ? this[key1][key2, key3] : default(V);
}
set
{
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
}
}
public bool ContainsKey(K1 key1, K2 key2, K3 key3)
{
return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
}
public void Add(K1 key1, K2 key2, K3 key3, V value)
{
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
}
}



