How to reset all instances in IOC Container

时光毁灭记忆、已成空白 提交于 2019-12-04 09:52:51

With SimpleIoC

I'd add a public static property with a private string backend for a unique Key

something like

private static string _currentKey = System.Guid.NewGuid().ToString();
public static string CurrentKey {
  get {
    return _currentKey;
  }
  private set {
    _currentKey = value;
  }
}

and have the cleanup method to unregister VM's with current key and finally reset the current key(invoke on each app reset stage):

public static void Cleanup() {
  SimpleIoc.Default.Unregister<LoginViewModel>(CurrentKey);
  ...
  CurrentKey = System.Guid.NewGuid().ToString();
}

and when calling GetInstance(...) just pass in the static CurrentKey.

SimpleIoc.Default.GetInstance<LoginViewModel>(ViewModelLocator.CurrentKey);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!