SimpleIoc - 每次都需要提供新的实例吗? [英] SimpleIoc - can it provide new instance each time required?

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

问题描述

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



我的想法是,如果有一个ViewModel的实例,是一个很小的可能性,这个ViewModel将需要两次,所以我想创建它的每一次需要的新实例。如果我们有ViewModels的工厂,我们将有这样的属性:

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

这一个使用singleton模式,我认为这不是最好的做法病例。为了规避这个问题,我这样做:

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

这有一个缺点,如果我更改MyViewModel的构造函数,我将需要更新此属性。不太大,但仍然有某种依赖。



如何处理这种情况,是否有我缺少的东西?以及为什么决定不返回非共享实例。



另一个问题是,在MVVM Deep dive会话中,Laurent在注册特定的ViewModel后使用GetInstance方法,以便,如他所说,确保在容器中已经有一个这个ViewModel的实例。为什么这是必要的?如果您通过ViewModelLocator提取ViewModel,那么您将在需要时创建它。

解决方案

你可以通过传递一个不同的键来获得一个不同的实例GetInstance方法。但是,实例将被缓存,因此如果你不想将它们保存在缓存中,则需要使用相应的键调用Unregister。



在演示中,我正在创建VM,因为MainVM正在向SecondaryVM发送消息。由于向Messenger的注册是在SecondaryVm的构造函数中完成的,因此需要先创建它,然后才能开始接收消息。 Messenger是伟大的,因为它是非常分离,但它是这些情况之一,你需要做额外的工作,以补偿解耦:SecondaryVM是消息的目标,即使MainVM没有得到任何引用。 p>

希望它有意义。
Cheers,
Laurent


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天全站免登陆