Ticket Q572394
Visible to All Users

Pattern advice needed - How to avoid code duplication when separate Web and WinUi Controllers are needed?

created 11 years ago

This is a general situation which I will illustrate with a scenario form my solution
* BO = a Product
* The £ Selling Prices of Products can be changed
* By selecting those to be changed in a List View of all Products
* And using a SingleChoiceAction which has several ChoiceItems e.g. "change by £" "Change by %"
To implement this I need a WInUI and also an ASP View Controller because:
* the Grid multi-select and so on needs to be set
* SelectionChanged event and so on need to be handled
There are 5 elements to the View Controller Code:

  1. Set up Single Choice Actions - event handling etc
  2. Set up the Grid and Grid Events
  3. Interact with the user - users confirms to update, show user results of update and so on
  4. Handle the Choice Action execution (do the updates)
  5. Localisation of captions & messages shown to users
    Of these 5 elements only 2. is Platform specific.
    What is the best design pattern to avoid duplicating the code for the other 4 ?

Answers approved by DevExpress Support

created 11 years ago (modified 11 years ago)

Hello Andrew,
As I can see, you do not need to create two platform-specific controllers.

To implement this I need a WInUI and also an ASP View Controller because:
* the Grid multi-select and so on needs to be set
* SelectionChanged event and so on need to be handled
By default, both GridControls operate in multi-selection mode: the ASPxGridView via the CheckBox column, the GridControl via Shift+Click or Ctrl+Click.
The ListView already contains suitable events for your task - SelectionChanged.
So, you can implement all code in a single platform-agnostic ViewController.
If there is some other limitation which prevents you from using this approach, please describe it. We will be happy to help you.

    Show previous comments (1)
    DevExpress Support Team 11 years ago

      Hello Andrew,
      As you need to customize the Grid control, it's possible to create a base Controller class in the main module of your application, which will contain common code and some abstract methods. Then create two child Controller classes in the Web and Win module with platform-specific code.
      You can do this in the following manner:
      Base Controller class:

      C#
      public abstract class GridControllerCore : ViewController { protected override void OnActivated() { View.SelectionChanged += View_SelectionChanged; } protected abstract void SetupGrid(); // method for platform specific grid customization // common code }

      And its child is in the Win module:

      C#
      public class WinGridController : GridControllerCore { protected void SetupGrid() { GridListEditor gridListEditor = ((ListView)View).Editor as GridListEditor; if(gridListEditor == null) { return; } gridView = gridListEditor.GridView; gridView.OptionsBehavior.EditorShowMode = EditorShowMode.MouseUp; gridView.OptionsSelection.ShowCheckBoxSelectorInGroupRow = DevExpress.Utils.DefaultBoolean.True; gridView.OptionsView.ShowButtonMode = DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowAlways; gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect; gridListEditor.Grid.HandleCreated += Grid_HandleCreated; } }
      AB AB
      Andrew Bingham 2 11 years ago

        Thanks Sergey
        So I guess there is not a single platform agnostic controller then … shucks

        Dennis Garavsky (DevExpress) 11 years ago

          Hello Andrew,
          Controls are different for each platform and it is not possible to completely avoid writing platform-dependent code in this particular case.
          However, it is possible to implement a more reusable solution through Application Model extensions that will enable a developer to set options within a platform-agnostic module. In its turn, there will still be two platform-dependent Controllers that will map platform-agnostic model options to the actual control properties. This way many standard XAF and third-party modules are implemented.

          Other Answers

          created 11 years ago (modified 11 years ago)

          Thanks Dennis
          For some reason it had not occurred to me to inherit View Controllers. I think - as a beginner - I was too worried I would somehow mess up XAF working properly.
          I can see how it will simplify some of my current solution - a refactoring session is called for…

            Comments (1)
            Dennis Garavsky (DevExpress) 11 years ago

              Welcome, Andrew.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.