SimpleIoc - 它可以在每次需要时提供新实例吗? [英] SimpleIoc - can it provide new instance each time required?

查看:24
本文介绍了SimpleIoc - 它可以在每次需要时提供新实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,SimpleIoc 使用 GetInstance 方法来检索已注册的类的实例.如果实例不存在,它将创建它.然而,这个实例被缓存并总是被检索,这模仿了单例模式.

我的想法是,如果这个 ViewModel 被需要两次的可能性很小,就没有必要在内存中保留一个 ViewModel 的实例,所以我想每次需要时都创建它的新实例.如果我们有一个 ViewModels 工厂,我们将拥有这样的属性:

public MyViewModel MyViewModel{get { return SimpleIoc.Default.GetInstance();}}

这个使用单例模式,我认为这不是所有情况下的最佳实践.为了规避这个问题,我这样做:

public MyViewModel MyViewModel{get { return new MyViewModel(SimpleIoc.Default.GetInstance());}}

这个有一个缺点,如果我改变了 MyViewModel 的构造函数,我也需要更新这个属性.没什么大不了的,但仍然存在某种依赖性.

您如何处理这种情况,我是否遗漏了什么?以及为什么决定不返回非共享实例.

另一个问题是,在 MVVM 深度潜水会话中,Laurent 在注册特定 ViewModel 后立即使用 GetInstance 方法,以确保容器中已经存在此 ViewModel 的实例.为什么这是必要的?如果您通过 ViewModelLocator 获取 ViewModel,那么您将在需要时创建它.那么我为什么要预先创建它们?

解决方案

通过将不同的键传递给 GetInstance 方法,您可以每次获得不同的实例.但是实例会被缓存,因此如果您不想将它们保留在缓存中,则需要使用相应的键调用 Unregister.

在演示中,我预先创建了 VM,因为 MainVM 正在向 SecondaryVM 发送消息.由于对 Messenger 的注册是在 SecondaryVm 的构造函数中完成的,因此需要在它开始接收消息之前创建它.Messenger 很棒,因为它非常解耦,但这是其中一种情况,您需要做额外的工作来补偿解耦:SecondaryVM 是消息的目标,即使 MainVM 没有得到任何引用.

希望这是有道理的.干杯,洛朗

So far as I understand, SimpleIoc uses GetInstance method to retrieve an instance of a class that is registered. If the instance doesnt exist, it will create it. However, this instance is cached and always retrieved, which mimics the singleton pattern.

My thinking is that there is no need to keep an instance of ViewModel in a memory if there is a small possibility that this ViewModel will be needed twice, so I would like to create new instance of it each time that is needed. If we have are having a factory for ViewModels, we will have a property like this:

public MyViewMOdel MyViewModel
{
    get { return SimpleIoc.Default.GetInstance<MyViewModel>(); }
}

this one uses singleton pattern, which I think is not best practice in all cases. In order to circumvent this problem, I do this:

public MyViewModel MyViewModel
{
    get { return new MyViewModel(SimpleIoc.Default.GetInstance<ISomeInterface>()); }
}

This one has a disadvantage that if I ever change a constructor for MyViewModel, I will need to update this property also. Not big deal, but still there is some sort of dependency.

How do you handle this scenario, and is there something I am missing? and why it was decided not to have non-shared instance returned.

And another question is, in MVVM Deep dive session Laurent uses GetInstance method right after he registers a particular ViewModel, in order to, as he says, ensure that there is already an instance of this ViewModel in container. Why exactly is this necessary? If you are fetching a ViewModel through ViewModelLocator, then you will create it whenever required. So why would I want to have them created upfront?

解决方案

You can get a different instance each time by passing a different key to the GetInstance method. However the instances will be cached, so if you do not want to keep them in the cache, you will need to call Unregister with the corresponding key.

In the demo, I was creating the VM upfront because the MainVM was sending messages to the SecondaryVM. Since the registration to the Messenger is done in the SecondaryVm's constructor, it needs to be created before it can start receiving messages. The Messenger is great because it is very decoupled, but it is one of these cases where you need to do extra work to compensate the decoupling: The SecondaryVM is the target of messages even though the MainVM does not get any reference to it.

Hope it makes sense. Cheers, Laurent

这篇关于SimpleIoc - 它可以在每次需要时提供新实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆