Composition with Stored Settings

Jan 25, 2011 at 11:58 PM

Hi,

I'm new to MEF and Prism and have a question on what approach to take. Actually I'm not really sure whether this is a MEF or a Prism problem so I'll pose it to both groups.

I want to make a widget based application that enables users to select widgets that show up on a dashboard screen. Now, let's suppose that the dashboard screen can have a number of different layouts (e.g. single column, two columns, equal split, 60/40 split, etc...). This struck me as a good problem for Prism and MEF. I could have plug-ins that implement, say, IDashboardLayout and use MEF to get all of the layouts that are provided by the current catalogs.

What I want to be able to do is somehow store which layout the user has selected, and also which widgets are where in that layout (assume that I have a working settings storage service that is able to persist whatever settings needed, as this is not a storage problem). My real question is, how do I load the correct layout type with the correct settings at the time of composition?

Thanks

Greg

Feb 3, 2011 at 4:49 PM

Basically you need this:

  1. Regions identified by names
  2. Widget identifiers (probably want these to be unique or contextual) 
  3. Store what widget belongs to that region in some persistent storage

This is a question about region management not about MEF and that's something Prism does quite well.

Region management or compositional UI architectures are definitely non-trivial. You can only allow yourself so much freedom before you end up with something that's not very useful. What I mean is that there's no single or simple answer how to do this, it boils down to a bit of grunt work.

Since MEF is all about composition and decoupling the first thing you need to accept is that you'll need names to identify things. The region management in Prism uses region names to show content somewhere and you need something similar (if you're using Prism I suggest you look into the region management).

The simplest way of this is just to export all regions and then store which region contains what widget somewhere. You can then use metadata or public properties to identify and "join" the widget with the correct region.

 

 

 

Feb 7, 2011 at 1:19 AM

Thanks for your reply. I have two questions about this approach. How would the following be handled?

* How would the user be able to control the order in which their widgets show up? And in which region?

* Would it be possible to have the same widget appearing multiple times (with different settings), e.g. if you had a weather widget you may want to show multiple locations.

What I'm thinking about is a dashboard application where the user can select the layout that they wish to use and then drag on widgets (out of the catalog).

 

Regards

Greg

Feb 8, 2011 at 3:42 PM

Greg,

you can implement your own config something like the following

<views>
 <view name="MyWidget" region="TopRegion" />
 <view name="MyWidget" region="BottomRegion" />
 <view name="MyWidget2" region="TopRegion" />
</views>

then you can implement a WidgetManager service who is responsible for reading the configuration and then creating the specified widgets.

When it comes to ordering your widgets, you might like to introduce an index on each entry in the config, or you might like to instantiate the widgets in the order they appear in the configuration.

With WPF, items are displayed in the ORDER THEY ARE RENDERED. There is the [SortHintAttribute] which is part of the Prism framework which you can use to help with ordering, but since this attribute is only applicable at the class level, it might not be so useful in your scenario.