Building Model-View-Presenter applications with MVC#. Part 3: Advanced Customization (Office 2007)

Published: 11/10/2008
By: Oleg Zhukov

Oleg Zhukov talks about building applications with MVC#.

Contents [hide]

Introduction

In the earlier articles (here and here - read them prior to proceeding) we got acquainted with MVC# - a framework for simplifying the usage of the Model-View-Presenter pattern in .NET applications. We have covered all basic requirements and scenarios of using the MVP architectural pattern. However in the real-world conditions application's requirements diverge from those basic ones. Complex applications often demand for an advanced customization and fine-tuning. And, of course, robust frameworks should cope well with such specific requirements.

This article steeps deeper into the MVC# framework capabilities demonstrating its advanced usage scenarios. These scenarios range from extending view and task descriptions to customizing navigation and presentation styles. Each advanced feature is covered in a separate article section and may be read independently.

Using 3rd-party components (Office 2007-like UI)

The major drawback of many existing MVC/MVP frameworks is that they require certain UI components to be used with them. For example the ASP.NET MVC framework comes with its own set of UI elements, CAB (Composite UI Application Block) is either not compatible with many 3rd-party components. With such frameworks a developer finds himself restricted in choosing the UI components he likes. MVC#, on the other hand, does not impose any limitations of such kind. Developers may use MVC# with any 3rd-party library they are committed to. In this article we are using MVC# with 3rd-party UI components for creating an Office 2007-like user interface.

The main form of our application will be constituted from a menu with toolbar strips, an Outlook-like navigation pane on the left and an Outlook-like content panel on the right (see the picture below). To apply the Outlook 2007 style to the menu and toolbar strips we will use an open-source 3rd-party renderer component by ComponentFactory (downloadable from here). The Outlook-like navigation pane we will use is the open-source component by Muhammed Sahin (get it here). And the Outlook-like content pane is the slightly modified Outlook-style panel from http://www.openwinforms.com/.

Figure 1: The Outlook-like example application

The Outlook-like example application

By now we should have only placed the said 3rd-party controls on the form and set their basic properties. Later on we will handle events from these controls and make them interact with the framework.

Outlook-like navigation

When a user selects an item in the navigation pane an appropriate view should be shown in the content pane on the right. In MVC# the way of switching between views is defined by the views manager class. Therefore the desired kind of navigation can be implemented by writing a custom WinformsViewsManager subclass - OutlookLikeViewsManager.

Firstly, this new views manager should gain access to the content panel of the main form:

If a newly created view does not have a parent container then it should be placed inside the content panel:

Whenever a view is activated it should be placed on top over other views:

Finally, in the Main form code we should handle the selection of an item inside the navigation bar:

Extended view and interaction point definitions

The conventional way for describing View-Controller pairs is using [IPoint] attributes inside the task declaration. Since views in our application are divided into three categories (Mail, Notes, Tasks) we might want to supplement the [IPoint] attribute with additional category information. So we will introduce the [IPointEx] attribute and the InteractionPointInfoEx class:

For the new [IPointEx] attributes to be properly processed a custom TaskInfoByAttributesProvider descendant is required:

A similar technique is applied when extending view definitions. Say, we want to associate each view with the corresponding image name. Then we should introduce the [ViewEx] attribute and the ViewInfoEx class:

In order to interpret [ViewEx] attributes we also need to create a DefaultViewInfosProvider descendant:

Before starting the main task the framework should be configured to use the above TaskInfoByAttributesProviderEx and ViewInfosPrividerEx classes:

Finally, view navigation links inside the navigation pane should be added with properly set category and image:

Views in multiple assemblies

For projects with distinctly separated architecture, views may be spread among several DLL assemblies. If this is the case those assemblies should be appended to the MVCConfiguration.ViewsAssemblies collection:

Customized Controller creation

As a rule the controller type is specified in the interaction point description. However, instead of such explicit declaration the default controller type could be used. Each view category (Mail, Notes, Tasks) could be associated with its default controller type. Consider, for instance, the following declaration:

Since the controller type is left unspecified above then the default controller for the Notes category should be used - NotesController class.

Such functionality could be implemented by creating a custom Navigator subclass with overriden GetController method:

In the above code an interaction point with unspecified controller type gets associated to one of the default controller types (depending on the view category - see the switch construct above). And then the controller is created with this type.

To employ the new NavigatorEx class it should referenced with the help of the [Task] attribute:

Another option is applying the custom navigator class globally via the MVCConfiguration.NavigatorType property.

Adding views dynamically

In our example application a user should be able to add views dynamically by clicking the appropriate menu item. Depending on the clicked item views of different categories will be created:

The actual view creation method will reside in the OutlookLikeViewsManager class and will do the following: a) Add a new (properly configured) InteractionPointInfoEx instance to the interaction point descriptions collection for the task and b) Add a new (also configured) ViewInfoEx to the view descriptions collection for the task:

Summary

Throughout this article we have studied a number of advanced MVC# framework features. These features help dealing with complicated application's requirements which often appear in real-world projects. With the discussed customization techniques developers can fully harness MVC# power to creating complex Model-View-Presenter applications.

The source code of this example can be found under "Examples\Advanced (Office2007 UI and more)\" subfolder of the MVC# root folder. The framework itself can be downloaded from www.MVCSharp.org/download.aspx.

Please visit the link at the below url for any additional user comments.

Original Url: http://dotnetslackers.com/articles/net/Building-Model-View-Presenter-Applications-with-MVC--Part-3-Advanced-Customization.aspx