如何重置IOC容器中的所有实例 [英] How to reset all instances in IOC Container
问题描述
我已经使用 MVVM Light框架制作了C#WPF应用程序.我的应用程序使用ViewModelLocator
类在运行时定位视图模型. ViewModelLocator
使用SimpleIoc
类,该类也随 MVVM Light框架一起提供. /p>
这是我的情况: 用户登录后可以使用我的应用程序.注销后,我想处置/重置/重新创建所有viewmodel实例,以为下一个用户提供一个干净的环境.
我试图在ViewModelLocator
类中实现Cleanup()
方法,但是它不起作用.
不起作用意味着(第二个)用户可以从之前登录的用户那里看到数据.
第一次尝试:
public static void Cleanup()
{
SimpleIoc.Default.Reset();
}
第二次尝试:
public static void Cleanup()
{
SimpleIoc.Default.Unregister<LoginViewModel>();
SimpleIoc.Default.Unregister<TaskViewModel>();
SimpleIoc.Default.Register<LoginViewModel>();
SimpleIoc.Default.Register<TaskViewModel>();
}
第三次尝试(不是我想要的,但这是一种解决方法):
public static void Cleanup()
{
// I implemented the ICleanup interface in my viewmodels
// The cleanup method clears all my variables eg: myCollection.clear();
SimpleIoc.Default.GetInstance<LoginViewModel>().Cleanup();
SimpleIoc.Default.GetInstance<TaskViewModel>().Cleanup();
}
如何重置ViewModelLocator
类中的所有实例?如有必要,我愿意使用更高级的Ioc容器.
使用SimpleIoC
我将为私有Key
类似
private static string _currentKey = System.Guid.NewGuid().ToString();
public static string CurrentKey {
get {
return _currentKey;
}
private set {
_currentKey = value;
}
}
并具有清理方法,以使用当前密钥注销虚拟机并最终重置当前密钥(在每个应用重置阶段调用):
public static void Cleanup() {
SimpleIoc.Default.Unregister<LoginViewModel>(CurrentKey);
...
CurrentKey = System.Guid.NewGuid().ToString();
}
,并且在调用GetInstance(...)
时,只需传入静态CurrentKey
.
SimpleIoc.Default.GetInstance<LoginViewModel>(ViewModelLocator.CurrentKey);
I have made an C# WPF Application using the MVVM Light framework. My Application uses the ViewModelLocator
class to locate the viewmodels during runtime. The ViewModelLocator
makes usage of the SimpleIoc
class which also comes with the MVVM Light framework.
Here is my scenario: The user logs in an can use my application. On logout, i want to dispose/reset/recreate all viewmodel instances to provide a clean environment to the next user.
I tried to implement the Cleanup()
method in the ViewModelLocator
class but it is not working.
Not working means that the (second) user sees the data from the user who was logged in before.
first try:
public static void Cleanup()
{
SimpleIoc.Default.Reset();
}
second try:
public static void Cleanup()
{
SimpleIoc.Default.Unregister<LoginViewModel>();
SimpleIoc.Default.Unregister<TaskViewModel>();
SimpleIoc.Default.Register<LoginViewModel>();
SimpleIoc.Default.Register<TaskViewModel>();
}
third try (not what i want but it is a workaround):
public static void Cleanup()
{
// I implemented the ICleanup interface in my viewmodels
// The cleanup method clears all my variables eg: myCollection.clear();
SimpleIoc.Default.GetInstance<LoginViewModel>().Cleanup();
SimpleIoc.Default.GetInstance<TaskViewModel>().Cleanup();
}
How do i reset all instances in my ViewModelLocator
class? I'm willing to use a more advanced Ioc Container if necessary.
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);
这篇关于如何重置IOC容器中的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!