wpf 3层架构中的MVVM WPF应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13840670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:34:46  来源:igfitidea点击:

MVVM WPF application in 3-tier architecture

wpfmvvmn-tier-architecture

提问by Adolfo Perez

I'm currently working on a Enterprise WPF LOB Desktop application using the MVVM design pattern. My current Solution structure in my development machine is the following:

我目前正在使用 MVVM 设计模式开发企业 WPF LOB 桌面应用程序。我的开发机器中当前的解决方案结构如下:

  • Main Project- WPF application containing all the Views(XAML)
  • View Model- Contains all View Models backing up the Views in main project
  • BLL- Business Logic Layer
  • DAL- Data Access Layer - Connects to a MS-SQL server and calls Stored Procedures
  • Model- Contains all business entities
  • 主项目- 包含所有视图(XAML) 的WPF 应用程序
  • 视图模型- 包含备份主项目中的视图的所有视图模型
  • BLL- 业务逻辑层
  • DAL- 数据访问层 - 连接到 MS-SQL 服务器并调用存储过程
  • 模型- 包含所有业务实体

I'm currently not using WCF since everything resides in the same machine at this moment except the database which is in its own server. However, in the future we are planning to split the code-base into 3-tiers.

我目前没有使用 WCF,因为此时除了数据库在自己的服务器中之外,所有东西都驻留在同一台机器上。但是,将来我们计划将代码库分成 3 层。

The problem I have is that one colleague insists that we should split our application as follows in 3 separate servers/machines:

我遇到的问题是一位同事坚持认为我们应该将我们的应用程序拆分为 3 个独立的服务器/机器:

  1. Presentation Tier- The client WPF application (View) in users machine. This may as well be a Web client application
  2. Logic Tier server- The View Model + Model + Business Logic Layer + Data Access Layer
  3. Data Tier server- The database server
  1. 表示层-用户机器中的客户端 WPF 应用程序 ( View)。这也可能是一个 Web 客户端应用程序
  2. 逻辑层服务器——视图模型+模型+业务逻辑层+数据访问层
  3. 数据层服务器- 数据库服务器

I cannot conceive the View Modelliving apart(different server) from the Viewand he claims that should be possible.

我无法想象的视图模型从分开居住(不同的服务器)查看,他声称,应该是可能的。

EDIT:My colleague claims that having the View Model in the Server side will ease any future deployments and will make it more maintainable because changes would go only on the server side. However, I've deployed .NET applications using ClickOnce and it is not really a big deal.

编辑:我的同事声称在服务器端拥有视图模型将简化任何未来的部署并使其更易于维护,因为更改只会在服务器端进行。但是,我已经使用 ClickOnce 部署了 .NET 应用程序,这并不是什么大问题。

From what I have readyou can have a WPF client application installed at users computer which contains the View and ViewModel and then expose the services at lower layers through a communication layer like WCF.

根据我所读到的内容,您可以在用户计算机上安装一个 WPF 客户端应用程序,其中包含 View 和 ViewModel,然后通过 WCF 等通信层在较低层公开服务。

This answer in another posts states the following:"In MVVM the UI Layer is separated into two layers. the ViewModel, which is in charge of the Application Logic, and the View, which is in charge solely on presentations." Based on that, my fundamental question is, can the View and the ViewModel UI layers reside in separate tiers (servers)? If so, is that recommended? and how could that be accomplished?

另一个帖子中的这个答案说明如下:“在 MVVM 中,UI 层分为两层。ViewModel 负责应用程序逻辑,而 View 仅负责演示。” 基于此,我的基本问题是,View 和 ViewModel UI 层可以驻留在不同的层(服务器)中吗?如果是这样,是否推荐?怎样才能做到这一点?

Thanks!

谢谢!

回答by Blachshma

The View Model, call it how ever you want, is at the end an instance of an Objectheld in the memory of your computer. Just like any other instantiated class. Its purpose is to connect the View and the Model, access various BL methods, etc.

View Model,你想怎么称呼它,最后是一个Object保存在你的计算机内存中的实例。就像任何其他实例化类一样。其目的是连接View和Model,访问各种BL方法等。

Even if it is feasible to pass an instanceof the View Model from some server to the client (Let's say you binary serialize it on the server and deserialize it on the other end) - Other then creating a huge overhead (both on the Network and on the CPU), I can't see how it will possibly make things easier, on the contrary I would like to understand howis this going to make things more maintainable.

即使将视图模型的实例从某个服务器传递到客户端是可行的(假设您在服务器上对它进行二进制序列化并在另一端反序列化它) - 否则会产生巨大的开销(在网络和在CPU)上,我看不出它如何将有可能使事情变得更容易,在我想明白相反如何这将让事情变得更容易维护。

Both the View and the View Model should be on the client side. e.g.

视图和视图模型都应该在客户端。例如

Presentation Tier - The client WPF application (View + View Models)
Logic Tier server - Model + Business Logic Layer + Data Access Layer
Data Tier server - The database server

Given thisseparation, he is correct that changes made in the BL or DAL will not need to create a new version of the client (as long as you don't break any interfaces, don't change the model etc.)

鉴于这种分离,他是正确的,在 BL 或 DAL 中所做的更改不需要创建客户端的新版本(只要您不破坏任何接口,不更改模型等)

回答by Faster Solutions

Lets start with the obvious:

让我们从显而易见的开始:

  1. ViewModels don't live on the server. Their role is to make data from multiple services/models available to the view and provide the link between views and data. Nobody does this and nobody will think this is a good idea. Ever.
  2. Your data tier can live wherever you want it to live with minimal intervention.
  1. ViewModel 不在服务器上。它们的作用是使来自多个服务/模型的数据对视图可用,并提供视图和数据之间的链接。没有人会这样做,也没有人会认为这是个好主意。曾经。
  2. 您的数据层可以放置在您希望它存在的任何地方,只需最少的干预。

Where there will be discussion is how much/little of your business logic tier is is moved up to become a WCF layer and how much will remain on the client machine. This is what the discussion should be about.

将讨论的地方是您的业务逻辑层有多少/很少被向上移动成为 WCF 层,以及有多少将保留在客户端计算机上。这是讨论应该讨论的内容。

If your colleague persists on arguing that the viewmodel should be on the server ask him/her to prototype it. It should be highly entertaining.

如果您的同事坚持认为视图模型应该在服务器上,请让他/她对其进行原型设计。它应该非常有趣。