视图定义模型的MVC / MVP [英] Defining view models for MVC / MVP
问题描述
短的问题 - 你怎么定义你的视图模型
Short question - how do you define your view models?
下面是一些选项:
- 通过实际的模型到视图。
- 创建与该模型(如Model.Product)的参考视图模型
- 创建与模型所需要的性能视图模型,并设置这些从模型。
- 也许更多。
都用自己的优点和缺点。
All with their own advantages and disadvantages.
什么是你的经验 - 好的和坏的?而且你用同样的模式GET / POST?
What is your experience - good and bad? And do you use the same model for GET/POST?
谢谢你的投入!
推荐答案
基本上 - 它是所有关于分离的责任。
Basically - it's all about separating responsibilities.
更多你把它们分开 - 更详细的,复杂的,但更容易理解它获得
More you separate them - more verbose, complex but easier to understand it gets.
型号:
public class foo{
string Name{get;set}
Bar Bar {get;set;}
string SomethingThatIsUneccessaryInViews {get;set;}
}
public class bar{
string Name {get;set;}
}
public class fizz{
string Name{get;set;}
}
presenter(我承认 - 仍然没有获得MVP的的想法完全):
public someSpecificViewPresenter{
fizz fizz{get;set;}
foo foo{get;set;}
necessaryThingsForWhatever[] necessaryThingsForWhatever{get;set;}
public void someLogicIfNeeded(){...}
}
魔法object2object映射和放大器;压扁,视图模型modelmetadata配置放在这里......
magic object2object mapping & flattening, viewmodel modelmetadata configuration goes here...
视图模型(NB => POCOS集装箱的道具只有无逻辑应该在这里。):
ViewModel (NB=>POCOS with container props only. No logic should go here.):
public class fooViewModel{
string Name {get;set;}
string BarName {get;set;}
}
public class fizzViewModel{
string Name {get;set;}
}
public class someSpecificView{
fooViewModel foo {get;set;}
fizzViewModel fizz {get;set;}
whateverViewModel whatever {get;set;}
}
和这里有云DAS幸福结局......
and here goes "das happy ending"...
<use viewdata="someSpecificView m" />
<p>
Our foo:<br/>
${Html.DisplayFor(x=>x.foo)}
</p>
<p>
Our fizz:<br/>
${Html.DisplayFor(x=>x.fizz)}
</p>
${Html.UberPaging(m.whatever.Paging)}
是的,我使用GET / POST相同的模型。请参见这了解为什么/ IFS。
And yes, i use same model for GET/POST. See this for more why/ifs.
但最近 - 我在寻找其他的解决方案。 CQRS嗡嗡声吸引了我的眼球。
But lately - I'm looking for other solutions. CQRS buzz catch my eye.
这篇关于视图定义模型的MVC / MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!