I have a main application that can load modules (or plugins). Each modules can define one or more links (or items) that will be displayed in a side menu defined in the main application.

Right now, my main application execute a list of ItemProviders which are loaded from the modules. These ItemProviders return a list of items to display in the side menu.

My question now is how am I supposed to order these items since they are not aware their siblings in the menu? Keep in mind that there is not a particular logic in the order, it depends on what the client wants.

My primary solution was to give them an order number (an index). The problem with this approach is that my modules would no longer be completely independent, the "order number" would depend on other modules' number.

What would be the proper way to handle this particular case?

  • What do you mean by it depends on what the client wants? Does the client get to configure the order? Or is it just based on which plugins they load? Do you need to intermingle them? – Bobson Dec 31 '13 at 16:18
  • The client does not have a real time control over the order. The client will ask us to put the "User Management" link below the "System administration" link. – Jean-Philippe Leclerc Dec 31 '13 at 16:28

Use Reference To Application Menu

The main application has a set of menus for the side menu already. For each menu item associate a human readable key-id for each item. You can then use this key-id later in modules to reference where new menu items should be positioned.

When loading modules their menu items can have an optional parameter called after that indicates which menu item from the main application it should appear under.

If the key-id in the application does not exist. You can append the menu item to the bottom of the sidebar.

You can still use an order numeric value to sort menu items, but respect the after parameter first.

Data might look like this grouped by title and order.

 Application Menu
      "File", 0
      "Edit", 1
      "View", 2
      "Tools", 3

A module could have a menu defined like this.

 Module Menu
         "Window", 0
         "Help", 1

You could have other parameters like insert or child for submenus. The key is to use a human readable reference point back to the main application's menu. This makes extending the menu easy.

| improve this answer | |

If the user is aware that these menu items are provided by multiple plug-ins, I would either give each plug-in a sub-menu to fill, or put separators in the menu to separate the entries of the different plug-ins.

Within the set of entries from one plug-in, the plug-in would be responsible for ordering the menu items.

| improve this answer | |
  • But the same problem arise, how do I order the submenus now? – Jean-Philippe Leclerc Dec 31 '13 at 16:29
  • @Jean-PhilippeLeclerc: The two most logical options are then alphabetically or the order in which the plug-ins were installed. – Bart van Ingen Schenau Dec 31 '13 at 16:30

Precondition: You know what modules / plugins are loaded.

Create another module that has the only responsibility to manage / order the menu items.

Because this is the responsibilty of the module, it is allowed to know the menu items provided by the other modules. This module reads the menu items provided by the other modules (say IMenuItem) and orders them according to your or your clients preferences.

Yes, this module is completely aware of the menu items and orders them hard-coded or lets the menu items be sorted e.g. via an xml (which allows to change the order after compile time). Using an xml can avoid the knowledge of the menu items within the code, making the menu module itself more flexible and independent.

This module can be interchanged for applications using different modules, so application, menu module and the other modules are loosely coupled.

The module implements IMenuItemProvider which is then consumed by the main application and returns IMenuItem instances in the correct order.

As stated above, for this solution you have to have knowledge of the loaded modules otherwise there is only the possibility of ordering alphabetically or some other generic sorting.

@cgTag's answer is also a possibility but then the knowledge of ordering is within the modules themselves, which is what you wanted to avoid.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.