MVVM-Light:将WCF调用放在ViewModel中的位置 [英] MVVM-Light: Where to put WCF call in ViewModel

查看:99
本文介绍了MVVM-Light:将WCF调用放在ViewModel中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个WP7应用程序,该应用程序通过WCF服务获取其所有数据.我想在其中实现MVVM-Light,但在完成的教程中,我看到在ViewModelLocator中,它想在应用启动时为所有ViewModel创建一个静态实例. 我的问题是,在我的VM的构造函数中,我进行WCF调用,结果当然会返回到回调中.在回调中,我将结果分配给我的视图"看到的可观察集合.当不使用MVVM-Light时,这种方法可以正常工作,但是如果实现了它,我将无法在启动时进行这些WCF调用,因为它们传递的参数是未知的/不可用的,直到用户使用该应用程序(选择项目等)为止. 我尝试将对WCF的调用移到我的可观察集合的getter上,但它一直在无休止的循环中调用WCF. 在我所看到的所有MVVM-light样本中,我还没有看到任何人调用WCF服务. 关于将WCF调用放在模型中的位置有何建议?

I'm building a WP7 app that gets all its data through WCF services. I want to implement MVVM-Light in it but in tutorials I've done, I see that in the ViewModelLocator it wants to create a static instance of all my ViewModels when the app starts. My problem is that in my constructors for my VM's is where I make my WCF calls with the results of course coming back in the callback. It is in the callback where I assign the results to my observable collection that my Views see. This works fine when not using MVVM-Light but if I implement it I can't have these WCF calls at startup as they pass parameters not known/available until the user uses the app (selecting items, etc.) I tried moving my calls to the WCF to the getter of my observable collection but it keeps calling the WCF in an endless loop. In all the samples of MVVM-light that I've seen I haven't seen anyone calling WCF services. Any suggestions as to where to put my WCF calls in the model?

推荐答案

就像MVVM中一样,没有正确的方式... ...关于feedom ... :-)

As ever in MVVM there is no right way ... as it is about feedom ... :-)

如果需要,可以将代码放入视图模型.但是,如果要使用它,则还必须在视图模型中也生成设计时数据.

You can put the code into your view model if you want. However, you then will have to also generate the design time data in the view model too - if you want to use it.

此方法的最大缺点是,您在视图模型和服务代码之间引入了耦合(因为您将不得不在视图模型中实例化服务).解耦组件的一般方法是创建一个描述服务的接口,并将实现该接口的对象实例注入其构造函数中的视图模型.这样一来,您就可以创建设计时间和运行时实现,并且视图模型不在乎它使用的是哪个模型-即对象是分离的.

The big disadvantage with this approach is that you introduce a coupling between your view model and your service code (as you will have to instantiate the service in your view model). The general approach to de-couple the components is to create an interface describing your service and injecting an instance of an object implementing this interface into your view model in it's constructor. This enables you to create a design time and a run time implementation and your view model does not care which one it uses - i.e. the objects are decoupled.

修改

在我的帖子中插入并非暗示您使用的是注入容器/框架,这仅意味着您使用接口来抽象您的服务行为,然后将实现程序或该接口传递给您的视图模型的构造函数.现在,您可以在创建视图模型时传递接口的不同实现,例如在视图定位器中.

Injecting in my post does not imply hat you use an injection container/framework, it only means that you use an interface to abstract your service behavior, and then pass an implementor or of this interface into the constructor of your view model. You now can pass different implementatios of the interface when you create your view model, e.g. in a view locator.

建议的模式称为控制反转",注入是一种将在其他位置创建的对象传递到您创建的类中的技术.通过这个步骤,我们班级现在可以不受实现者的任何详细知识的影响,而且他们可以互换.

The pattern suggested is called "inversion of control" and injecting is a technique to pass objects created elsewhere into the class you creating. Through this your our class is now shielded from any detail knowledge of the implementators and they become interchangeable.

控制容器的倒置-如ninject,Unity等-只能帮助您自动解决依赖关系,但是,使用in的控制模式版本并不需要.

Inversion of control containers - like ninject, unity, etc. - just help you automatically resolving dependencies, however, they are not required for using the in version of control pattern.

这篇关于MVVM-Light:将WCF调用放在ViewModel中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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