什么进入模型/视图模型? [英] What goes into the Model/Viewmodel?

查看:168
本文介绍了什么进入模型/视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么去哪里

这是我的C#项目的简短描述:

This is a short description of my C# project:

我有一个机械结构(只有一个在整个程序),约20至30个参数(deimensions,材料参数等),可作为一套完整的,来自一个输入屏幕或从XML文件(反序列化)来描述。这些参数,随后在计算处理,以产生输出,即进入一个JPEG文件,并为HTML文件

I have a mechanical construction (only one in the whole program), described by some 20 to 30 parameters (deimensions, material parameters etc.), that may, as a complete set, come from an input screen or from an XML file (Deserialized). These parameters must then be processed in a calculation to generate output, that goes to a JPEG file and also to an HTML file.

视图是显而易见的:它的IO屏幕

The View is clear: it's the IO screen.

视图需要一个视图模型,其中属性。也许

The View needs an ViewModel where the Properties are. Maybe:

我的模型是在手的结构,是由它的参数来描述。
但是这些参数,都是从IO屏幕云集的相同,查看或XML。

My Model is the construction at hand, that is described by its parameters. Those parameters however, are the same ones that are gathered from the IO screen , the View or from XML.

有些输出(JPEG文件)也变为风景。这可能是通知它更改的属性。

Some output (the JPEG file) also goes the View. It might be a Property that Notifies it's changed.

现在我的问题是,我需要一个模式可言,因为视图模型具有的所有属性了。

Now my question is, do I need a Model at all, because the ViewModel has all the properties already.

或者,我需要一个视图模型在所有的,因为我的模型拥有所有要查看的属性。我可以定义在视图模型的模型(就像是在MVVM经常做),并利用该模型作为视图一个DataContext。但是,最后一个选项将使视图意识到型号:不是在MVVM精神

Or, do I need a ViewModel at all, because my Model has all the Properties to be Viewed. I could define a Model in the ViewModel (like it is always done in MVVM) and use the Model as a DataContext for the View. But that last option would make the View aware of the Model: not in the MVVM spirit.

推荐答案

我已经写了这多次了,但我会做一次更多...

I've written this multiple times already, but I'll do it once more...

MVVM背后的主要理由是,分层,避免紧密耦合尽可能。

The main reasoning behind MVVM is to separate layers and to avoid tight coupling as much as possible.

这表示,查看是,因为你已经猜到了正确的UI。事情的用户看到。如果它是一个Windows,一个页面,一个自定义的控制,网页,甚至控制台(当我们在更广的范围内谈论MVVM)没关系。

That said, View is, as you've correctly guessed, the UI. Things your user sees. It doesn't matter if it's a Windows, a Page, a custom control, webpage or even a console (when we talk about MVVM in a broader context).

视图模型是模型和视图之间的调停人。这需要,结合和操纵从模型视图的目的的方法和属性。它不关心如何,在何时何地被这些使用。它也可以触发模型边行动,为照顾更新数据库的实例调用服务。

ViewModel is a mediator between your model and the view. It takes, combines and manipulates your methods and properties from the model for the purposes of the View. It doesn't care how, when and where are these used. It also can trigger actions on the model side, for instance call services that take care of updating your database.

模式是一切,是不依赖于特定平台。这是你的业务逻辑类,它的数据库实体等它基本上剥夺任何联系的UI实现的应用程序。这就是人们得到错误的,认为模型只是数据库实体。 !那是完全错误的。

Model is EVERYTHING that isn't tied to the specific platform. It's classes of your business logic, it's database entities etc. It's basically your application stripped of any ties to the UI implementation. This is what people get wrong and think that model are only database entities. That's simply wrong!

要回答你问的问题:现在的问题是,难道我需要模型在所有的,因为视图模型具有的所有属性已。

是的,你应该,否则,你最终会直接耦合的观点该模型而这违反MVVM原则。你的观点不应该知道的直接模式任何东西。至于查看关心的属性和方法,每个人都可以从不同的项目中来。它不会改变任何事情,视图仍将功能相同。

Yes, you should, otherwise you'll end up coupling the view directly to the model and that's violating MVVM principle. Your View shouldn't know ANYTHING about the model directly. As far as View cares, every one of the properties and methods can be coming from a different project. It won't change a thing and the view will still function the same.

您也许看不到它,但在未来它会让你的生活更更容易。如果做得正确,更具有可读性等的代码变得易于维护。

You maybe don't see it yet, but in the future it will make your life much more easier. The code becomes easily maintainable if done correctly, much more readable etc.

这篇关于什么进入模型/视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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