ViewModel有自己的逻辑吗?

时间:2022-04-11 02:03:36

When assigning ViewModel fields, should the domain objects be passed directly to the ViewModel objects which will then determine how to present the data, or should another class like a service be assigning data from the Model to the ViewModel?

在分配ViewModel字段时,是否应将域对象直接传递给ViewModel对象,然后确定如何呈现数据,或者另一个类如服务是否应该将模型中的数据分配给ViewModel?

also:

EDIT:is there any sense in dividing a viewmodel into receiver and presenter? (instead of binding only certain fields on update?)

编辑:将视图模型划分为接收器和演示者是否有任何意义? (而不是仅在更新时绑定某些字段?)

3 个解决方案

#1


6  

Usually the Controller Action takes the business objects and puts whatever is needed by the viewmodel.

通常,Controller Action接受业务对象并放置viewmodel所需的任何内容。

If you have a business object that contains the fields Name, Address, Id and the View should only display the Name, then the ViewModel only has a field "Name", and the controller action populates it. The ViewModel should know nothing about your Business Classes, it should only know about the stuff that it needs to display.

如果您的业务对象包含字段Name,Address,Id和View应仅显示Name,则ViewModel只有一个字段“Name”,控制器操作会填充它。 ViewModel应该对您的业务类一无所知,它应该只知道它需要显示的东西。

The main/only logic is then "Display Logic", that is stuff like "if TotalAmount is negative, display it with CSS Class negativeNumber".

主/唯一的逻辑是“显示逻辑”,就像“如果TotalAmount为负,用CSS Class negativeNumber显示它”。

#2


4  

For the sake of SoC if that logic relates to View they safe to be in ViewModel or even in View itself but if they relate to Business or Program put them on Model and Controller respectively.

为了SoC,如果该逻辑与View相关,那么它们可以安全地存在于ViewModel中,甚至可以安装在View本身中,但如果它们与Business或Program相关,则分别将它们放在Model和Controller上。

#3


3  

In my experience I've used services to map the Model to the ViewModel. I don't put logic in my ViewModels.

根据我的经验,我使用服务将Model映射到ViewModel。我没有在我的ViewModels中放置逻辑。

As an aside, it's probably worth your while to check out AutoMapper to assist you with the mapping. Definitely helps cut down on writing repetitive mapping logic.

顺便说一句,看看AutoMapper以帮助您进行映射可能是值得的。绝对有助于减少编写重复映射逻辑。

#1


6  

Usually the Controller Action takes the business objects and puts whatever is needed by the viewmodel.

通常,Controller Action接受业务对象并放置viewmodel所需的任何内容。

If you have a business object that contains the fields Name, Address, Id and the View should only display the Name, then the ViewModel only has a field "Name", and the controller action populates it. The ViewModel should know nothing about your Business Classes, it should only know about the stuff that it needs to display.

如果您的业务对象包含字段Name,Address,Id和View应仅显示Name,则ViewModel只有一个字段“Name”,控制器操作会填充它。 ViewModel应该对您的业务类一无所知,它应该只知道它需要显示的东西。

The main/only logic is then "Display Logic", that is stuff like "if TotalAmount is negative, display it with CSS Class negativeNumber".

主/唯一的逻辑是“显示逻辑”,就像“如果TotalAmount为负,用CSS Class negativeNumber显示它”。

#2


4  

For the sake of SoC if that logic relates to View they safe to be in ViewModel or even in View itself but if they relate to Business or Program put them on Model and Controller respectively.

为了SoC,如果该逻辑与View相关,那么它们可以安全地存在于ViewModel中,甚至可以安装在View本身中,但如果它们与Business或Program相关,则分别将它们放在Model和Controller上。

#3


3  

In my experience I've used services to map the Model to the ViewModel. I don't put logic in my ViewModels.

根据我的经验,我使用服务将Model映射到ViewModel。我没有在我的ViewModels中放置逻辑。

As an aside, it's probably worth your while to check out AutoMapper to assist you with the mapping. Definitely helps cut down on writing repetitive mapping logic.

顺便说一句,看看AutoMapper以帮助您进行映射可能是值得的。绝对有助于减少编写重复映射逻辑。