views:

37

answers:

2

I'm looking to see if I can design a HtmlHelper extension method that will generate the Html for different types of widgets I want to produce.

Each different type of widget implements functionality to get and prepare any data it needs to render.

Can anyone suggest any patterns I could refer to for approaches to take? I know there are probably frameworks available that will do this for me, but I thought I'd give it a try anyway. Any points of advice?

Thanks

A: 

I'd look at using PartialViews for most of your widget layout, that's how I'm handing it. What I do is have a PartialView for the widget itself, then have each specific widget implement a PartialView for it's content. My model contains a set of widget models. Each widget model contains information on it's page placement - zone and order -- and what view to render for it's content. The View I'm rendering places the widgets in a grid using the zone and order information and renders the generic widget partial with each individual widget model as it's the model for the partial. The widget partial uses the data in the widget to choose the content partial for that widget. That content partial is strongly-typed to that widget's view model and uses the data from the widget model directly. To make it work, the widget model exposes the view model as a generic object property so that the widget partial can pass the model data to the content partial.

tvanfosson
+1  A: 

To create objects from a family of classes (with all family having a common supertype), you should use the factory method or abstract factory design patterns.

lmsasu