Vincenzo
Vincenzo

Reputation: 168

MVC - View that needs multiple model

I have a problem at the conceptual level, I read a lot but I'm not out yet. I would like to model a system that plans to place orders and allows you to choose the products according to the selected location. In the analysis phase it is right that is the user interface to request data to the classes product, location and orders? In the design phase, can view access to multiple models and display the data or is more correct if the orderView access only to the order model and this takes all the data from the database (using different DAO for location, order and product)? Thanks in advance, Vincenzo

Upvotes: 0

Views: 2843

Answers (4)

radarbob
radarbob

Reputation: 5101

There are no absolutes

In the design phase "is it right..." is the wrong thing to say. Design is an exercise in trade offs. At the end of my treatise I hope you conclude that we're splitting hairs on a sound design concept that you should be adhering to.

Separating UI from Data from Managing their interaction is a good design goal

Whether you use MVP, MVC, MVVM is largely an academic exercise in this forum because we don't have the overall design details.

For arguments sake, let's just call the basic idea MVC. MVP is a variation of that and MVVM (ViewModel) is a specialization of our MVP.

MVVM / ViewModel is .NET Specific

Model-View-ViewModel pattern was created by microsoft architects to take advantage of the capabilities of WCF, XAML, and Silverlight (vis-a-vis ASP.NET and Windows Forms). In the final analysis it's a variation of our basic concept, a variation that leverages .NET technologies to be sure.

Exposing Model to View
.. or not is a judgement call on how much coupling you want between your model view controller components.

MVC gives your view a reference to your model. This is handy for data binding. If you use .NET Binding architecture then this pattern is what you may end up using.

MVP - only the Presenter (a Controller w/ more power, if you will) sees the data model. You can still use .NET Binding architecture, for example, but the presenter is the middle man hooking it all up. Why? Because you made that informed decision based on your overall design.

MVP means the Presenter intimately knows about the View and Model so it can wire them together and handle UI inputs. It therefore follows that there is a Presenter for each View as they are customized pairs, (most likely) not interchangeable.

Development Tools can Influence Design Architecture

For example if you are developing in .NET WCF you absolutely have no choice but to use MVVM; that's the way that framework works.

Likewise, Mac OS X development IDE forces you to use MVC. So does Ruby On Rails and .NET MVC web development

If you are using some kind of Object Relational Mapper (ORM) - and .NET LINQ is one - then your design may not have a Data Access Layer per se.

Upvotes: 1

JJJCoder
JJJCoder

Reputation: 16916

If you need an example, this is done in the music store http://mvcmusicstore.codeplex.com/releases/view/64379#DownloadId=228002.

The details are around p95 of the associated PDF.

Upvotes: 0

Mike Mooney
Mike Mooney

Reputation: 11989

An approach to solve this is to use a ViewModel (http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx), basically a composite model that groups several other models together so that they can be serviced to a view.

So if you have a Product model and a Location model and an Order model, all logically distinct models that you want to combine together on a single view (e.g. Monthly Sales Summary), you could create a SalesSummaryViewModel that contains products, orders, and locations to be returned to that view.

Upvotes: 2

Emmanuel N
Emmanuel N

Reputation: 7449

You should consider Model View ViewModel, have ViewModel objects created for serving your view needs, composing your business object.

Upvotes: 1

Related Questions