MVVM——模型的正确实现
MVVM – Correct implementation of the Model
上下文:
C#/WPF Windows-Store 应用程序使用 SQLite 在本地存储数据,并通过 Web 访问下载数据。
应用程序:
数据必须从网上下载并映射到一个 class 中,该 class 存储在本地 SQLite 数据库中。例如,在客户 class 中:
[Table("Customer")]
class Customer{
[AutoIncrement, PrimaryKey, Unique]
public int Id {get;set;}
public string Name {get; set;}
…
}
class 由不同的线程操作,因此 Customer class 中的 INotifypropertyChanged 不是一个好主意。此外,GUI 应显示多个客户,因此 ViewModel 包含一个客户列表。此外,客户必须通过多个属性进行扩展才能由 GUI 正确显示。
问题:
什么是模型?客户?
我应该如何在不弄乱客户 class 的情况下使用其他属性扩展客户?
除了将客户存储在数据库中或从 Web 下载数据并将其映射到客户 class 的业务逻辑之外,我应该在哪里?在模型中?
如果您的答案是客户就是模型并且它不应该包含任何逻辑,那么为什么 MVVM 模式说模型包含业务逻辑?
客户肯定是这种情况下的模型。模型应该是 POCO 并且仅由 GUI 使用的每个 属性 应该在视图模型中,例如 CustomerViewModel。
业务逻辑应该在一个模型中。可以使用 repository pattern.
解决数据库中的存储问题
模型通常有逻辑,但仅限于业务逻辑。根据 MVVM,您不应在模型中放置任何 UI 逻辑,而应在视图模型中放置。
假设我们有您的模型
class Customer {
public int Id { get; set; }
public string Name { get; set; }
}
并且需要以大写形式显示客户姓名。不是向客户 class 添加额外的 属性,而是在视图模型
中添加一个
class CustomerViewModel implements INotifyPropertyChanged {
...
public string UpperCaseName { ... }
...
}
我认为 Wikipedia page 总结得很好:
Model
Model refers either to a domain model, which represents the real state content (an object-oriented approach), or to the data access
layer that represents that content (a data-centric approach).
View
As in the MVC and MVP patterns, the view is the user interface (UI).
View model
The view model is an abstraction of the view that exposes public properties and commands. Instead of the controller of the MVC pattern,
or the presenter of the MVP pattern, MVVM has a binder. In the view
model, this binder mediates communication between the view and the
data binder. The view model has been described
as a state of the data in the model.
Binder
Declarative data- and command-binding are implicit in the MVVM pattern. In the Microsoft solution stack, the binder is a markup
language called XAML. The binder frees the developer from being
obliged to write boiler-plate logic to synchronise the view model and
view. When implemented outside of the Microsoft stack the presence of
a declarative databinding technology is a key enabler of the pattern.
What is the Model?
持有/是数据框架的实体或任何class。
How should I extend the Customer with additional properties without messing up the Customer class?
通过 Partial
class 修饰符。允许您将专用逻辑放入 extended operations/properties。另外,您可以让客户遵守 INotifyProperty
更改。
Where should I but the business logic for storing Customers in the DB or downloading and mapping the data from the web to the Customer class? In the Model?
将所有业务逻辑放在 ViewModel
中以获取和存储信息。这是魔法发生的地方,是 View
和 Models
之间的管道,供 View
显示。
why does the MVVM pattern say the Model contains the business logic?
业务逻辑采用构成数据的实体形式。
刚接触 MVVM 的人总是想知道他们是否正确地遵循了 MVVM。 MVVM(恕我直言)只是一个三层数据系统(记住范例),它将数据与逻辑分离以获取数据并最终显示数据;三层。
如果将 MVVM 视为老派三层数据系统,其中 VM contains/acquires 数据(称为模型)并处理业务逻辑以处理数据并简单地允许视图显示所述数据;就这么简单。
上下文:
C#/WPF Windows-Store 应用程序使用 SQLite 在本地存储数据,并通过 Web 访问下载数据。
应用程序:
数据必须从网上下载并映射到一个 class 中,该 class 存储在本地 SQLite 数据库中。例如,在客户 class 中:
[Table("Customer")]
class Customer{
[AutoIncrement, PrimaryKey, Unique]
public int Id {get;set;}
public string Name {get; set;}
…
}
class 由不同的线程操作,因此 Customer class 中的 INotifypropertyChanged 不是一个好主意。此外,GUI 应显示多个客户,因此 ViewModel 包含一个客户列表。此外,客户必须通过多个属性进行扩展才能由 GUI 正确显示。
问题:
什么是模型?客户?
我应该如何在不弄乱客户 class 的情况下使用其他属性扩展客户?
除了将客户存储在数据库中或从 Web 下载数据并将其映射到客户 class 的业务逻辑之外,我应该在哪里?在模型中?
如果您的答案是客户就是模型并且它不应该包含任何逻辑,那么为什么 MVVM 模式说模型包含业务逻辑?
客户肯定是这种情况下的模型。模型应该是 POCO 并且仅由 GUI 使用的每个 属性 应该在视图模型中,例如 CustomerViewModel。
业务逻辑应该在一个模型中。可以使用 repository pattern.
解决数据库中的存储问题模型通常有逻辑,但仅限于业务逻辑。根据 MVVM,您不应在模型中放置任何 UI 逻辑,而应在视图模型中放置。
假设我们有您的模型
class Customer {
public int Id { get; set; }
public string Name { get; set; }
}
并且需要以大写形式显示客户姓名。不是向客户 class 添加额外的 属性,而是在视图模型
中添加一个class CustomerViewModel implements INotifyPropertyChanged {
...
public string UpperCaseName { ... }
...
}
我认为 Wikipedia page 总结得很好:
Model
Model refers either to a domain model, which represents the real state content (an object-oriented approach), or to the data access layer that represents that content (a data-centric approach).
View
As in the MVC and MVP patterns, the view is the user interface (UI).
View model
The view model is an abstraction of the view that exposes public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. In the view model, this binder mediates communication between the view and the data binder. The view model has been described as a state of the data in the model.
Binder
Declarative data- and command-binding are implicit in the MVVM pattern. In the Microsoft solution stack, the binder is a markup language called XAML. The binder frees the developer from being obliged to write boiler-plate logic to synchronise the view model and view. When implemented outside of the Microsoft stack the presence of a declarative databinding technology is a key enabler of the pattern.
What is the Model?
持有/是数据框架的实体或任何class。
How should I extend the Customer with additional properties without messing up the Customer class?
通过 Partial
class 修饰符。允许您将专用逻辑放入 extended operations/properties。另外,您可以让客户遵守 INotifyProperty
更改。
Where should I but the business logic for storing Customers in the DB or downloading and mapping the data from the web to the Customer class? In the Model?
将所有业务逻辑放在 ViewModel
中以获取和存储信息。这是魔法发生的地方,是 View
和 Models
之间的管道,供 View
显示。
why does the MVVM pattern say the Model contains the business logic?
业务逻辑采用构成数据的实体形式。
刚接触 MVVM 的人总是想知道他们是否正确地遵循了 MVVM。 MVVM(恕我直言)只是一个三层数据系统(记住范例),它将数据与逻辑分离以获取数据并最终显示数据;三层。
如果将 MVVM 视为老派三层数据系统,其中 VM contains/acquires 数据(称为模型)并处理业务逻辑以处理数据并简单地允许视图显示所述数据;就这么简单。