MVC数据库设计问题 - 每个模型表1 [英] Problem With Database Design For MVC - 1 Table Per Model

查看:143
本文介绍了MVC数据库设计问题 - 每个模型表1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最开始使用MVC框架的最大障碍与1模型对1 DB表的概念有关。对我来说,除了一个简单的应用程序之外的任何东西,它都过于简单化和不切实际。然而,MVC正在使用,包括这个令人敬畏的StackOverflow站点。通常,我遇到的所有代码示例和教程都非常简单,在这种情况下,1到1的关系正常工作。但是,我正在寻找的是一个坚实的现实世界的一个MVC模型的例子,它解决了表的连接。在Stackoverflow的情况下,我会想象一个DB设计,其中包含问题,标签,用户等等。在我的数据库设计中,我可能有一个Question_Tag链接表来查找与给定问题相关联的所有标签。 MVC如何处理这个问题?

解决方案

我不认为有什么关于 MVC设计模式,声明每个域类应该有一个数据库表。实际上,关于MVC的设计模式,甚至说你的模型应该被保存在一个关系数据库中,所以没有什么关系。



这只是一个策略一些流行的框架(Ruby on Rails - 也许是ASP.NET MVC?)已经采取了,为什么似乎是方便的。但它不是MVC的要求。 Spring MVC(对于Java世界)没有固有的概念,如何将模型组件映射到数据库,实际上美丽的是它不在乎 - 您只需要提供要使用的模型数据,以及您获得的位置它从MVC框架不在乎。



所以换句话说,你不需要假设MVC模式意味着你必须使用一个数据库每个模型组件的表。你甚至不需要使用数据库,你的模型也可以来自一个平面文件或Web服务(和MVC的好处是,如果你正确设计你的应用程序,你可以交换不同的数据层实现和在您的应用程序中,View或Controller 将不会知道)。


My biggest hurdle with getting started with an MVC framework has to do with with the 1 model to 1 DB table concept. To me it's overly simplistic and unrealistic for anything beyond a simple app. Yet, MVC is in use everywhere, including this awesome StackOverflow site. As is typical, all of the code examples and tutorials i come across are very simple and the 1 to 1 relationship works fine in those cases. But, what i'm looking for is a solid real world example of an MVC model that address table joins. In the case of stackoverflow, i would imagine a DB design with tables like Questions, Tags, Users, etc. In my DB design I may have a Question_Tag link table to to look up all tags associated with a given question. How does MVC deal with this?

解决方案

I don't believe that there is anything about the MVC design pattern that states that there should be one database table per domain class. In fact, there is nothing about the MVC design pattern that even says that your model should be or has to be persisted in a relational database.

This is just the strategy that some popular frameworks (Ruby on Rails - maybe ASP.NET MVC too?) have taken on, for what appears to be convenience sake. But it is not a requirement of MVC. Spring MVC (for the Java world) has no inherent concept of how to map the model components to a database, in fact it's beauty is that it doesn't care - you just need to supply the model data to use, and where you get it from the MVC framework doesn't care about.

So in other words, you don't need to assume that the MVC pattern means that you have to use one database table per model component. Heck you don't even need to use a database, your model could come just as well from a flat file or web services (and what's great about MVC is that if you design your app properly, you could swap different data layer implementations in and out of your application and the View or Controller won't even know).

这篇关于MVC数据库设计问题 - 每个模型表1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆