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?
Share
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).