I'm new to MVC.
我是MVC的新手。
I have read this short bit detailing three ways of dealing with the view model in MVC:
我已经阅读了这篇简短的内容,详细介绍了在MVC中处理视图模型的三种方法:
http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx
http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx
The gist of it seems to me that:
在我看来,它的主旨是:
Method 1, pull an object out the database and use it as your view model. Quick and simple, but if you want data from multiple tables you're totally screwed (I can't think of a way around it without Method 2).
方法1,将对象拉出数据库并将其用作视图模型。快速而简单,但是如果你想要来自多个表的数据,你就完全搞砸了(如果没有方法2,我想不出办法)。
Method 2, create a class that has references to multiple objects, and use this as your view model. This way you can access everything you need. The article says that when views become complicated it breaks down due to impedance mismatch between domain/view model objects... I don't understand what this means. Googling impedance mismatch returned a lot of stuff, the gist of it being that you're representing database stuff using objects and stuff doesn't map across cleanly, but you'd presumably have this problem even with Method 1. Not sure what I am missing. Also seems to me that creating a class for each View to get the data you want isn't ideal from a maintenance point of view, not sure if you have a choice.
方法2,创建一个引用多个对象的类,并将其用作视图模型。这样您就可以访问所需的一切。文章说,当视图变得复杂时,由于域/视图模型对象之间的阻抗不匹配而导致故障......我不明白这意味着什么。谷歌搜索阻抗不匹配返回了很多东西,它的主旨是你使用对象和东西代表数据库的东西并没有干净地映射,但你可能会遇到这个问题,即使使用方法1.不确定我是什么失踪。在我看来,从维护的角度来看,为每个View创建一个类以获取所需的数据并不理想,不确定您是否有选择权。
Method 3, I am still getting my head around it, but I don't quite understand why their checkbox example wouldn't work in method 2, if you added a bool addAdditional to your class that wasn't connected to the domain model. Method 3 seems to say rather than return the domain stuff directly, just pull out the properties you specifically need, which I think is nicer but is gonna be harder to maintain since you'll need some big constructors that do this.x = domain.x
, this.y = domain.y
etc.
方法3,我仍然在考虑它,但我不太明白为什么他们的复选框示例在方法2中不起作用,如果你向你的类添加了一个未连接到域模型的bool addAdditional。方法3似乎说而不是直接返回域名的东西,只是提取你特别需要的属性,我认为这样做更好但是更难维护,因为你需要一些做这个.x =域的大构造函数。 x,this.y = domain.y等
I don't understand the builder, specifically why the interface is used, but will keep working on it.
我不了解构建器,特别是为什么使用接口,但会继续使用它。
Edit: I just realised this isn't really a question, my question is, is my thinking correct?
编辑:我刚才意识到这不是一个真正的问题,我的问题是,我的想法是否正确?
2 个解决方案
#1
1
The problem I've run into with #2 is that I have to do one of these two things:
我遇到的第2个问题是我必须做以下两件事之一:
-
Include every single field on every single object on the form -- those that aren't going to be displayed need to be included but hidden.
在表单上的每个对象上包含每个单独的字段 - 那些不会显示的字段需要包含但隐藏。
-
Only include the specific fields I need but use AutoMapper or something similar to map these fields back onto the actual objects.
仅包含我需要的特定字段,但使用AutoMapper或类似的东西将这些字段映射回实际对象。
So with #2 I see a mismatch between what I want to do and what I'm required to do. If we move on to #3, this mismatch is removed (from what I understand, briefly glanced at it). It also fixes the problem that a determined hacker could submit values like id fields or similar when using method #2 that unless I was very careful could be written to my data store. In other words, it is possible to update anything on any of the objects unless one is very careful.
因此,对于#2,我发现我想要做的事与我需要做的事情之间存在不匹配。如果我们继续前进到#3,那么这个不匹配就会被删除(从我的理解,简要地看一眼)。它还修复了一个问题,即确定的黑客在使用方法#2时可以提交id字段或类似的值,除非我非常小心可以写入我的数据存储。换句话说,除非非常小心,否则可以更新任何对象上的任何内容。
With method #3 you can use AutoMapper or similar to do the dirty work of mapping the custom object to the data store objects without worrying about the security issues/impedance exposed by method #2 (see comment for more details on security issues with #2).
使用方法#3,您可以使用AutoMapper或类似方法执行将自定义对象映射到数据存储对象的脏工作,而不必担心方法#2暴露的安全问题/阻抗(有关#2安全问题的更多详细信息,请参阅注释) )。
#2
0
You're probably right about impedance mismatch being present in both Methods 1 and 2 - it shows up anywhere you're going between objects in code and DB objects with relational mapping. Jeff Atwood writes about it, and cites this article, which is a fantastic discussion of everything dealing with Object-Relational mapping, or "The Vietnam of Computer Science". What you'll pretty much end up doing is weighing the pros and cons of all these approaches and choose the one that sounds like it fits your needs the best, then later realizing you chose the wrong one. Or perhaps you're luckier than I am and can get it right the first go 'round. Either way, it's a hairy problem.
你可能对方法1和方法2中存在阻抗不匹配是正确的 - 它出现在你在代码中的对象和具有关系映射的数据库对象之间的任何地方。杰夫阿特伍德写了这篇文章,并引用了这篇文章,这篇文章讨论了与对象关系映射或“计算机科学的越南”有关的一切。你最终要做的就是权衡所有这些方法的优点和缺点,并选择一个听起来最符合你需求的方法,然后再意识到你选错了方法。或者也许你比我更幸运,并且可以在第一轮比赛中做到正确。无论哪种方式,这都是一个毛茸茸的问题。
#1
1
The problem I've run into with #2 is that I have to do one of these two things:
我遇到的第2个问题是我必须做以下两件事之一:
-
Include every single field on every single object on the form -- those that aren't going to be displayed need to be included but hidden.
在表单上的每个对象上包含每个单独的字段 - 那些不会显示的字段需要包含但隐藏。
-
Only include the specific fields I need but use AutoMapper or something similar to map these fields back onto the actual objects.
仅包含我需要的特定字段,但使用AutoMapper或类似的东西将这些字段映射回实际对象。
So with #2 I see a mismatch between what I want to do and what I'm required to do. If we move on to #3, this mismatch is removed (from what I understand, briefly glanced at it). It also fixes the problem that a determined hacker could submit values like id fields or similar when using method #2 that unless I was very careful could be written to my data store. In other words, it is possible to update anything on any of the objects unless one is very careful.
因此,对于#2,我发现我想要做的事与我需要做的事情之间存在不匹配。如果我们继续前进到#3,那么这个不匹配就会被删除(从我的理解,简要地看一眼)。它还修复了一个问题,即确定的黑客在使用方法#2时可以提交id字段或类似的值,除非我非常小心可以写入我的数据存储。换句话说,除非非常小心,否则可以更新任何对象上的任何内容。
With method #3 you can use AutoMapper or similar to do the dirty work of mapping the custom object to the data store objects without worrying about the security issues/impedance exposed by method #2 (see comment for more details on security issues with #2).
使用方法#3,您可以使用AutoMapper或类似方法执行将自定义对象映射到数据存储对象的脏工作,而不必担心方法#2暴露的安全问题/阻抗(有关#2安全问题的更多详细信息,请参阅注释) )。
#2
0
You're probably right about impedance mismatch being present in both Methods 1 and 2 - it shows up anywhere you're going between objects in code and DB objects with relational mapping. Jeff Atwood writes about it, and cites this article, which is a fantastic discussion of everything dealing with Object-Relational mapping, or "The Vietnam of Computer Science". What you'll pretty much end up doing is weighing the pros and cons of all these approaches and choose the one that sounds like it fits your needs the best, then later realizing you chose the wrong one. Or perhaps you're luckier than I am and can get it right the first go 'round. Either way, it's a hairy problem.
你可能对方法1和方法2中存在阻抗不匹配是正确的 - 它出现在你在代码中的对象和具有关系映射的数据库对象之间的任何地方。杰夫阿特伍德写了这篇文章,并引用了这篇文章,这篇文章讨论了与对象关系映射或“计算机科学的越南”有关的一切。你最终要做的就是权衡所有这些方法的优点和缺点,并选择一个听起来最符合你需求的方法,然后再意识到你选错了方法。或者也许你比我更幸运,并且可以在第一轮比赛中做到正确。无论哪种方式,这都是一个毛茸茸的问题。