TinyIoC
TinyIoC copied to clipboard
SafeDictionary has side effects
I just finished diagnosing an issue in my Monodroid app where my Activities were null after being registered with TinyIOC.
Turns out I registered them twice and on the second call:
_container.Register<IMyView>(this); //'this' is an Activity
in SafeDictionary....
public TValue this[TKey key] { set { lock (_Padlock) { TValue current; if (_Dictionary.TryGetValue(key, out current)) { var disposable = current as IDisposable;
if (disposable != null)
disposable.Dispose();
}
_Dictionary[key] = value;
}
}
}
I really didn't expect calling register and passing 'this', would cause the object to be disposed. Easy enough to debug through, but counter-intuitive imho.
Would you consider a pull-request to make that configurable? Or to only dispose the object with the same key if it's a DIFFERENT object? I can't imagine any situatoin where a caller calling register and passing an instance would want that instance disposed.