Hello everybody,
The usability of code reuse and the customization of its behaviour is
one of the most significant goals of a developer when designing and
creating components, controls, classes or even frameworks.
For this, the virtual methods and events are a common technique in the
Object Oriented Programming.
The virtual methods are more preferrable when the behaviour change is
clear, strict and there will be no other adjustments. For example, you want
to unconditionally close the object editing window after a user saves the
changes in addition to the default implementation (which only saves the
changes). This means that this is the only adjustment of the "Save" action
in this application. Generally the virtual methods are used at the end-user
applications development.
The events should be used when there may be introduced a couple of
independed adjustments of the implemented behaviour. For example, two
different pluggable application modules deslare objects which require
additional initialization when creating. In this case both these modules
will extend the "New" action behaviour independently, and for this, using
the events is the most appropriated decision (actually, there will be the
chain of handlers for a single event). This approach can be used at the
end-user applications development as well as at the extentions development
for the existing components or controls.
Of course both these design decisions can be implemented simultaneously.
The above is the common theses and the reused elements should provide
the virtual methods and events. What does the XAF propose for using these
techniques?
XAF is a configurable system which has all the functionality within the
Controllers. This design allows plugging a new functionality (as
controllers) into the application or totally replacing a feature by removing
the appropriate controllers and introducing a couple of new ones. This
concerns code reuse because most of the application functionality remains
untouched, though it is inexpediently to drop a significant block
(controller) when only a bit of customizations is necessary. Controller
should have virtual methods and events for such little adjustments.
For example the "WebDetailViewController" class declares several
actions: "Cancel", "Save", "SaveAndClose" and "Edit". Each action has an
appropriate virtual method called ExecuteXXX: "ExecuteCancel",
"ExecuteSave", "ExecuteSaveAndClose" and "ExecuteEdit". So a developer can
make his own controller and override any of these virtual methods. In this
case all the UI settings of actions (category, caption, image, shortcut,
tool tip, localizations and so on) and the action state management (Active
and Enabled) will be reused. All the WebDetailViewController's actions are
very simple so there is only a single virtual method per each action. If an
action implementation is complex enough then there may be a couple of
virtual methods. This is the best practice for the design of controllers
which will be used by other developers.
All the XAF controllers are collected on the application startup (or
when a ModelEditor loads the application model). You can find all of them in
the Model Editor within the 'Actions' node. If you inherite your own
controller from the "WebDetailViewController" class you will see there both
your controller and the "WebDetailViewController". The main difference
between them is that all the WebDetailViewController's actions will be shown
within the WebDetailViewController's node, while your controller's node will
contain no actions: your controller will contain all the inherited actions
at run-time but it doesn't declare them, so the actions are shown once
within their container's node and are not duplicated into the descendant's
nodes. At run-time XAF doesn't create controllers if there is an inherited
controller, all the base controllers are not working in the application.
Another sample is the "NewObjectViewController" which declares the "New"
action and a couple of events in addition to the virtual methods:
CollectDescendats, CollectRoot, QueryNewObject and ObjectCreated. You can
add a handler to one of these events to customize the list of objects
available for creation, to create object manually, to customize the just
created object and to specify whether it will be shown in a new DetailView
or not. These events allow introducing several handlers for different
objects and primarily they are oriented to the module developers.
In the attached file I have demonstrated the above techniques.
--
Thanks, Dan
R&D, .NET Team Developer Express Inc.
PS. Please don't reply to me directly by email unless it is specifically
requested. If you wish to receive direct assistance from our Support Team,
use Support Center at http://www.devexpress.com/Support/Center
Please take a look at our Knowledge Base at: http://www.devexpress.com/kb.
It contains useful information on our products that may save you time.