2

假设我有一个包含我所有模型类的项目,还有一个包含 MVC 4 Web 应用程序的单独项目(服务、存储库层)。

如果我想为我的模型属性使用 MVC 特定的数据注释,我如何在不依赖 MVC 的情况下做到这一点?

任何引用该模型的非 MVC 项目也需要 MVC 引用。我希望这些仅在 MVC 使用模型而不是所有内容时才添加。

最初我在考虑部分类,但我知道这些不能用于交叉组装。

制作克隆类并从模型类之外的原始类继承可能不起作用。它需要代码引用这些子类而不是原始类。

我更喜欢将属性附加到模型的更不显眼的方式。

4

1 回答 1

6

If I want to use MVC specific data annotations for my model properties, how do i do this without having a dependency to MVC?

By using view models. View models are classes that you design and define in your MVC application and which are the classes that get passed to your views. You should never pass your models to the views. Your controller actions could query your service layer for domain models, then map those domain models to view models and pass those view models to the views. On the other hand your [HttpPost] controller actions should take view models wit htheir respective data annotations as arguments, map those view models to their corresponding domain models and pass the domain models to your service layer.

于 2013-07-07T15:31:48.950 回答