在单独的程序集中使用 MVC 2.0 和模型
我是 MVC 的新手,尽管有很多(我的意思是很多)非常有用的信息,但要清楚地了解如何使用 MVC 2.0 实现我的确切要求是非常困难的。
我想设置一个解决方案如下:
- 使用 MVC 2.0 项目提供 Web UI。
- 使用 Linq to SQL 类项目进行数据持久化。
- 我有两个单独的代码模块,需要访问上述 Linq to SQL 模型 - 因此我无法将我的 Linq to SQL 模型直接包含在 MVC 项目本身中。
- 另外,我的 Linq to SQL 项目前面有一个业务逻辑层。
我的问题是:
- 如何设置 MVC 应用程序的模型部分以通过 BLL 指向我的 Linq to SQL 项目?
- 如何执行 Web 应用程序验证?我可以使用 MVC 2.0 模型验证吗?如果不是,还有什么替代方案?
- 最后(稍微放在一边)-什么是 ViewModel,它与 Model 有何不同?
这么多问题。但这是一项令人兴奋的新技术,除了数据访问问题之外,我必须很快掌握其他所有内容,我认为 MVC 2.0 非常棒。
感谢您提供的任何指示。
I'm new to MVC and even though there is a lot (and I do mean a lot) of information out there that is very useful - it's proofing very difficult to get a clear understanding on how to achieve my exact requirements with MVC 2.0.
I would like to set up a solution as follows:
- Provide a web UI using an MVC 2.0 project.
- Use Linq to SQL classes project for data persistence.
- I have a two separate code modules that will need to access the above Linq to SQL model - so I won't be able to include my Linq to SQL model directly in the MVC project itself.
- Also I have a Business Logic layer in front of my Linq to SQL project.
My questions are:
- How do I set up the Model part of my MVC application to point to my Linq to SQL project via my BLL?
- How do I perform web app validation? Can I use MVC 2.0 Model Validation? If not what are the alternatives?
- Finally (and slightly aside) - What is the ViewModel and how does this differ from the Model?
So many questions. But this is an exciting new technology and data access issues aside, everything else I've got to grips with very quickly and I think MVC 2.0 is fantastic.
Thanks for any pointers you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常您会为此使用存储库模式。您的控制器有对您的存储库的引用 - 存储库从数据库返回您的域对象。 MVC 应用程序不知道 LINQ to SQL 的存在。
将视图模型放入您的 MVC 项目中。这些视图模型可能与您的领域模型密切相关,但它们关注的是表示模型。将数据注释放在这些视图模型上进行验证 - MVC 框架将自动确保在这些用数据注释修饰的视图模型上进行验证。它是可插拔的,因此您可以使用替代方案 - 但对于 MVC 2,它的内置效果相当好,其中包括客户端验证。
我在上面部分回答了这个问题。您的域模型的形状可能不是您显示视图所需的形状 - 视图模型非常适合弥补这一差距。此外,即使形状确实完全匹配 - 视图模型仍然是一个好主意,这样您就可以在其中放置 UI 验证代码并在其中放置其他表示元数据(因为您不希望在域模型上放置与表示逻辑相关的任何内容) 。
这是 查看模型模式的链接。
希望这有帮助。
Typically you'd use a repository pattern for this. Your controller has a reference to your repository - the repository returns your domain objects from your database. The MVC app has no knowledge LINQ to SQL exists.
Put view models in your MVC project. These view models may closely align with your domain models but their concern is to be the presentation model. Put your data annotations for validation on these view models - the MVC framework will automatically ensure validation occurs on these view models decorated with data annotations. It's pluggable so you could use alternatives - but with MVC 2, it's baked in fairly well and this includes client side validation.
I partially answered this one above. the shape of your domain models may not be the shape you need to display your views - view models are great to bridge this gap. Additionally, even if the shape does match exactly - view models are still a good idea so that you can put UI validation code there and other presentation meta-data on them (since you do not want anything related to presentation logic on your domain model).
Here's link for view model patterns.
Hope this helps.
您可以添加对 BLL 程序集公开的对象的引用,并将它们用作模型。
当您想要向生成的类添加验证时,请使用 好友类。
ViewModel 是模型数据的自定义聚合。每个视图都只有一个,因为 ViewModel 的目的是以一种方便、简洁的方式准确地显示特定视图所需的数据。
一个示例可能是包含订单和订单详细信息的视图。 ViewModel 可以保存对每种类型的存储库和业务对象的内部引用。 ViewModel 的属性将这些对象的数据合并在一起。
ViewModel 在您的情况下也很有用,因为您希望模型位于单独的程序集中。您可以将 DataAnnotations 应用于 ViewModel 属性以进行验证。您可以将“原始”业务对象模型设置为 ViewModel 的内部属性,并公开公共方法来检索和保留数据。
You can add a reference to the objects exposed from your BLL assembly and use them as your Models.
When you want to add validation to classes that are generated use buddy classes.
A ViewModel is a custom-shaped aggregate of Model data. There is exactly one per View, as the ViewModel's purpose is to surface exactly the data needed by a particular View in a convenient and concise way.
An example might be a View that contains both Order and OrderDetail information. A ViewModel can hold internal references to the repositories and business objects for each type. Properties of the ViewModel merge together the data from these objects.
ViewModels will be useful in your case also because you want your Models to be in a separate assembly. You can apply the DataAnnotations to ViewModel properties for validation. You would make the "raw" business object models internal properties of your ViewModels, and expose public methods to retrieve and persist data.