具有关系数据库和对象包装器的 MVC 2 应用程序 - 如何将关系数据放入视图中?
我最近用以下内容重构了我的应用程序: Linq 到 SQL 数据库连接 包装 linq to sql 类的对象 用于在对象和 Linq to Sql 实体之间来回映射的映射器 服务层调用存储库,将对象交给 UI。
在我只是在 UI 中使用 Linq to SQL 对象之前(我知道)。但当展示关系时就很容易了。例如:
我有一个名为 SchoolProfile 的能力,和一个名为 School 的表。用户有一个学校档案(包含 GPA、排名等),它链接到学校。学校添加功能很容易 - 因为它没有外键。
当为用户创建一个表单来列出所有 SchoolProfiles 时 - 他们不想看到 SchoolId。在我看来,之前它只是 schoolprofile.School.Name。现在,ShoolProfile 是我的 ViewData 中的一个“平面”对象,没有属性。我想我可以创建其他类来获取相关数据(学校名称等),但这感觉很混乱。有什么建议吗?
I've recently refactored my application with the following:
Linq to SQL db connection
Objects to wrap the linq to sql classes
Mappers to map back and forth between objects and Linq to Sql entitys
Service layer to call the repository, handing up objects to the UI.
Before I was just using Linq to SQL objects in my UI (I know). But when displaying relations it was so easy. For instance:
I have a able called SchoolProfile, and a table called School. A user has a SchoolProfile (with GPA, Rank, etc..) , which links to a School. The School adding functionality was easy - because it has no foreign keys.
When creating a form for a user to list all SchoolProfiles - they dont want to see a SchoolId. Before in my view it would simply be schoolprofile.School.Name. Now a ShoolProfile is a "flat" object in my ViewData with no properties. I guess I could create other classes to get the related data (name of the school, etc..) but that feels messy. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是查看 ViewModels 和 AutoMapper。基本上,您将为您的视图创建一个特定的 ViewModel,并且您可以将 SchoolName 作为该 ViewModel 的属性包含在内。然后,您可以使用 AutoMapper 轻松从域模型(从 Linq 到 Sql)映射到 ViewModel。
基本上,您希望 View 从 ViewModel 获取所需的所有信息。因此,根据需要显示的所有信息来设计 ViewModel。所以它需要做的就是获取 ViewModel 中的内容并将其吐出。
My suggestion is to look at ViewModels and AutoMapper. Basically you will create a specific ViewModel for your View and you can include SchoolName as a property of that ViewModel. Then you can use AutoMapper to map from your Domain Model (form Linq to Sql) to your ViewModel easily.
Basically, you want your View to get all the information it needs from the ViewModel. So design your ViewModel based on all the info you need to display. So all it needs to do is take what is in the ViewModel and spit it out.