views:

103

answers:

2

What feature(s) of ASP.Net MVC can replace the way events can be used in Webforms to support loosely coupled components?

For example, take a simple pager control in Webforms:

  1. A page number is clicked
  2. Pager fires off a "PageChange" event with the new page number
  3. This subscribing page/control received the event and handles initiating a call to fetch and bind new data.

What tools are available in ASP.Net MVC to similarly support

  • Loose coupling
  • Component re-usability
  • Separation of logic for a single page/view (such a very complex "portal" type page).
+1  A: 

ASP.NET MVC, and the Model-View-Controller in general, support loose coupling and separation of concerns by keeping the data and code that supports an application separate from the visual "Presentation-layer" markup that is seen by users.

Designed properly, Controllers and Views in MVC can be reused so that the Edit View for an entity can be "embedded" into a related View with no modification.

For example: an Orders View might include an OrdersDetail partial view. That partial view could be replaced with the OrderDetail Edit View that is also available elsewhere within the application.

Separating the Model from the View makes unit testing more effective and less cumbersome by splitting the code from the context of the presentation layer. You don't want to have to reference System.Web to unit test code that fetches data from a database.

Dave Swersky
Thanks for your reply Dave. So, I get the idea of a Partial View. I still don't understand *how* a loosely coupled components can be made. With the example of a pager, how can it be used to page Orders in one instance, and Users in another, without lots of repeated c# code? I am sure there must be a way (especially with a framework that prides itself on "Don't Repeat Yourself" coding - I just can't see it yet.
James
Look at http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/ for a quick example of how it might be done. Instead of your model being List<Order> you might return a PagedList<Order> and the logic for how many pages can be contained the in the PagedList implementation. Then you can make a generic partial that deals with PagedList.
Jab
Thanks Jab - this is some interesting code to help with paging in MVC. However, it is not really the paging itself I am trying to work out, it is how loosely coupled components can be developed for use within an MVC framework. Even with this code sample, I am non the wiser.
James
I think you may be trying to map the concept of a "component" from the webforms world to MVC. MVC doesn't really implement components the same way as webforms. The closest thing would be HTML Helpers.
Dave Swersky
OK - so say with our pager example. We have Partial View containing a pager used on both a User List view and a Order View. How can we write code (using HTML Helpers or any other tool available in MVC) to result in The correct list getting bound when a page link in the Partial View is clicked?
James
@James, Here is a example of a html helper that I wrote that uses the PagedList: http://codejunkies.se/post/2009/03/19/Page-selector-html-helper.aspx.
Mattias Jakobsson
As far as how you know to bind the correct list this is where things are totally different. Each link would go to its own distinct controller so there is no chance of them getting mixed up.
jwsample
A: 

MVC does away with events because events for the most part are just an unnecessary layer between what the client is trying to tell the server to do and the server actually doing it.

In the paging example for webforms the client clicks the button, the browser sends the event/viewstate, and the engine fires the ButtonClicked event. You examine the event, determine the client is intending to page, and you execute the paging logic.

In the MVC paradigm the user clicks a button that makes a request directly to the code that executes the paging logic. Since you know what action the button is supposed to invoke when you put it there why go through all machinations of the event firing? In your controller you certainly could fire an event when you get the command but I honestly can't imagine the use case for doing so.

Both methods accomplish the same thing but MVC just removes a layer of complexity.

jwsample