私が取り組んでいるプロジェクトのコードの安全性とコードの読みやすさ/視覚への影響に関して、内部 Dictionary インスタンス (C#) への参照を返すための 3 つのアプローチを検討しています。
次の 3 つのアプローチに絞り込みましたが、より良い提案をお待ちしています。現在、追加のボイラー プレートを使用せずに安全性を確保するための最良のバランスとして、#3を好みます。
1) 2 番目の ReadOnlyDictionary インスタンスを使用して内部 Dictionary をラップし、ReadOnlyDictionary だけがクラスをエスケープできるようにします。
2) Dictionary インスタンスを IReadOnlyDictionary として返しますが、再キャストすると変更される可能性があるため、オプション #1 または #3 ほど安全ではありません。
3) Dictionary.ToImmutableDictionary() を含むクラスをエスケープするときに ImmutableDictionary として返すため、返されるオブジェクトは内部辞書の不変ビューになりますが、これにより、より高いコストが発生するすべての呼び出しに対して新しいコピーが作成されます。小さな簡単な辞書(私のもの)を使用します。
private readonly Dictionary<string, string> innerDictionary = new Dictionary<string, string>();
// Only required for Example #1
private readonly IReadOnlyDictionary<string, string> readonlyInnerDictionary;
public ExampleClass() {
// Only required for Example #1
readonlyInnerDictionary = new ReadOnlyDictionary<string, string>(innerDictionary);
}
public IReadOnlyDictionary<string, string> GetExampleOne() {
// Requires a second dictionary which is more boiler plate but the object being returned is truly readonly
return readonlyInnerDictionary;
}
public IReadOnlyDictionary<string, string> GetExampleTwo() {
// Requires InnerDictionary be defined as Dictionary (Not IDictionary) but doesn't require the second dictionary be defined
// which is less boiler plate, but the object returned could be re-cast to it's mutable form meaning it's not truly mutation safe.
return innerDictionary;
}
public ImmutableDictionary<string, string> GetExampleThree() {
// Truly immutable object returned, but a new instance is built for every call; fortunately all of my dictionaries are small (containing at most 9 keys)
return innerDictionary.ToImmutableDictionary();
}