Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the unit of reusability in .NET MVC apps?

In traditional ASP.NET Web Form applications, UserControls are a great way to encapsulate functionality so that it can be reused. However, UserControls don't fit well into the MVC model. They often make heavy use of ViewState and they blur the seperation of concerns that MVC promotes.

My question is, how do you best bundle a piece of functionality so it can be shared across MVC applications?

As an example, consider a from/to date-selector UserControl that:

  • allows a user to select two dates, either using a javascript overlay or by typing in day, month and year into seperate fields
  • can be configured to default to either today and tomorrow's dates or to dates of the developer's choosing
  • validates the dates that comes back from the user to ensure the from date is before the to date
  • exposes From and To properties that can be accessed by code-behind

How would I best build something like this in .NET MVC so that I can easily reuse it?

Note that to fully emulate User Control's functionality the MVC component would have to manage the submitted form data and validation - not just the presentation.

like image 563
ctford Avatar asked Sep 24 '09 21:09

ctford


People also ask

What is used for code reusability in ASP.NET MVC?

Adding an extension method to an existing HtmlHelper class can come in handy for reusability of code.

What is MVC application life cycle?

The life cycle is basically is set of certain stages which occur at a certain time. MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The Starting point for every MVC application begins with routing.

What are components in MVC?

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.


2 Answers

In general I would agree that user controls are nice in terms of encapsulating UI stuff, but I don't think too much has really changed in MVC. If I remember right re-using user controls across classic Asp.net projects was a pain and was never really the best way to truly create reusable components. Most UI toolkits that you bought for classic ASP.net didn't give you user controls, they gave you essentially server controls and javascript controls.

In your example, I would probably create or find a jquery (or ur framework of choice) plugin that did what you wanted on the client side. You could also build a C# wrapper around it similar to what Telerik did with some of the jquery UI controls. I do think that the word code-behind and even viewstate will disappear from your vocabulary the more you get into MVC.

If you look at what open source projects are out there for MVC you will get your answer in terms of what you should be doing.

  • The MVC Contrib app adds a lot of features by creating extension methods and helpers. Their grid control is a typical way to create a reusable component that you could use across projects

  • Telerik, created some extensions that wrap jquery controls and do asset management.

  • Finally I think if you look to the future, MVC has areas, which if I interpret it right will give you the ability to break your project apart into multiple smaller projects.
like image 88
Greg Roberts Avatar answered Oct 11 '22 07:10

Greg Roberts


Besides what is already suggested, ASP.NET MVC v2 will have generic templated input controls, see here. You can read how other people do similar techniques, for example, here:

We have exactly 1 method call for generating a form element, “Html.InputFor”. As part of that “InputFor”, it examines an input specification, that collects the PropertyInfo, any attributes, the type, any modifiers called, and selects an appropriate InputBuilder. Call InputFor(p => p.Id) and Id is a GUID? That creates a hidden input element. Call InputFor(p => p.Customer.Address) and Address is a complex type? That looks for a partial with the same name of the type

like image 20
queen3 Avatar answered Oct 11 '22 09:10

queen3