你将如何在 mvvm 中设计与 Viewmodel 的业务逻辑交互
How would you design business logic interaction with Viewmodel in mvvm
视图模型可以通过哪些方式获取业务逻辑。比方说,我有 3 个模型,基于这 3 个模型数据,我将生成(基本上是计算)一些新值(比方说模型 4),我将在视图中显示这个模型 4 数据。
请注意,我无法将计算逻辑放在服务端,因为我无法控制它们。我必须在我这边计算模型 4。我应该在哪里计算这个模型 4 值?
问题
1) 从 ViewModel 调用服务形式是否是一种好的做法,即 GetModel_1_Data、GetModel_2_Data、GetModel_3_Data 如果不是什么好的做法?
2) 根据 mvvm,我不允许在 ViewModel 上有业务逻辑……那我应该怎么做?我们可以通过哪些不同的方式实现..这个?
3) 在 Mvvm 中,我看到 UI 使用的模型基本上是带有 INotifyPropertychanged 的模型.. 称呼它们的正确词是什么.. 我可以称为 UI 模型吗?或 UI 模型对象?其他模型就像普通的 CLR 对象。
当我读到 mvvm 时,很多人说要在模型中保留逻辑..哪个模型..他们是指普通的 clr 类..还是 UI 模型(他们有 INotifyPropertyChanged)
如果不是,我们是否需要一些层来从 POCO 模型生成 UI 模型?这一层叫什么..如果是...
谢谢,
我认为一旦您不再将模型视为单个 class,一切都应该清楚了。 MVVM 是关于表示层设计的。为了专注于表现,它将整个业务逻辑层抽象成一个模型。从这个意义上讲,Model 是一个层,而不是单个 class 或组件。它本身可以有自己的设计模式(服务,DDD,CQRS,...)。
回答您的问题
Note I can not put calculation logic on service side because i dont have control..on them. I have to compute Model 4 on my side.. Where should I compute this Model 4 Values?
这取决于你认为的计算逻辑是什么。如果是业务逻辑,那么应该放到"Model"层。并不总是有明确的界线。有时,它是你做的decision/classification。
Questions 1) Is it good practice to call Service form ViewModel i.e GetModel_1_Data,GetModel_2_Data, GetModel_3_Data if not whats the good practice ?
我不明白为什么不。另一种方法是添加一个额外的应用程序服务(或显示服务)层,以将 ViewModel 的创建与 ViewModel 本身分开。
2) As per mvvm I am not allowed to have business logic on ViewModel ...then where should I do? What are different ways we can achieve..this ?
如上回答,只要你认为计算逻辑是业务逻辑,就应该放入"Model"。除非,那个 Model 4 是纯粹的展示。上面给出了一个替代方案
3) In Mvvm I see models consumed by UI basically Models with INotifyPropertychanged .. what is the right word to call them.. can I call as UI Models? or UI model objects? Other Models just like plain CLR objects.
它们通常被称为ViewModel
when I read about mvvm many say to keep logic in Model..whic model ..does they mean plain clr classes..or UI models(where they have INotifyPropertyChanged) If not do we need to have some layer to produce UI Models from POCO models? What this layer is called ..if so...
模型可以表示业务逻辑对象或业务逻辑层,在上面的上下文中,它表示业务逻辑层
视图模型可以通过哪些方式获取业务逻辑。比方说,我有 3 个模型,基于这 3 个模型数据,我将生成(基本上是计算)一些新值(比方说模型 4),我将在视图中显示这个模型 4 数据。
请注意,我无法将计算逻辑放在服务端,因为我无法控制它们。我必须在我这边计算模型 4。我应该在哪里计算这个模型 4 值?
问题 1) 从 ViewModel 调用服务形式是否是一种好的做法,即 GetModel_1_Data、GetModel_2_Data、GetModel_3_Data 如果不是什么好的做法?
2) 根据 mvvm,我不允许在 ViewModel 上有业务逻辑……那我应该怎么做?我们可以通过哪些不同的方式实现..这个?
3) 在 Mvvm 中,我看到 UI 使用的模型基本上是带有 INotifyPropertychanged 的模型.. 称呼它们的正确词是什么.. 我可以称为 UI 模型吗?或 UI 模型对象?其他模型就像普通的 CLR 对象。
当我读到 mvvm 时,很多人说要在模型中保留逻辑..哪个模型..他们是指普通的 clr 类..还是 UI 模型(他们有 INotifyPropertyChanged) 如果不是,我们是否需要一些层来从 POCO 模型生成 UI 模型?这一层叫什么..如果是...
谢谢,
我认为一旦您不再将模型视为单个 class,一切都应该清楚了。 MVVM 是关于表示层设计的。为了专注于表现,它将整个业务逻辑层抽象成一个模型。从这个意义上讲,Model 是一个层,而不是单个 class 或组件。它本身可以有自己的设计模式(服务,DDD,CQRS,...)。
回答您的问题
Note I can not put calculation logic on service side because i dont have control..on them. I have to compute Model 4 on my side.. Where should I compute this Model 4 Values?
这取决于你认为的计算逻辑是什么。如果是业务逻辑,那么应该放到"Model"层。并不总是有明确的界线。有时,它是你做的decision/classification。
Questions 1) Is it good practice to call Service form ViewModel i.e GetModel_1_Data,GetModel_2_Data, GetModel_3_Data if not whats the good practice ?
我不明白为什么不。另一种方法是添加一个额外的应用程序服务(或显示服务)层,以将 ViewModel 的创建与 ViewModel 本身分开。
2) As per mvvm I am not allowed to have business logic on ViewModel ...then where should I do? What are different ways we can achieve..this ?
如上回答,只要你认为计算逻辑是业务逻辑,就应该放入"Model"。除非,那个 Model 4 是纯粹的展示。上面给出了一个替代方案
3) In Mvvm I see models consumed by UI basically Models with INotifyPropertychanged .. what is the right word to call them.. can I call as UI Models? or UI model objects? Other Models just like plain CLR objects.
它们通常被称为ViewModel
when I read about mvvm many say to keep logic in Model..whic model ..does they mean plain clr classes..or UI models(where they have INotifyPropertyChanged) If not do we need to have some layer to produce UI Models from POCO models? What this layer is called ..if so...
模型可以表示业务逻辑对象或业务逻辑层,在上面的上下文中,它表示业务逻辑层