Skip to content

Conditional Code Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Usage

<map:Conditional>
  <map:Case>
    <map:Case.Condition>
      ... MAPPING-CONDITION ...
    </map:Case.Condition>
    <map:Case.Action>
      ... MAPPING-ACTION ...
    </map:Case.Action>
  </map:Case>
  ...
  <map:Case>
    <map:Case.Condition>
      ... MAPPING-CONDITION ...
    </map:Case.Condition>
    <map:Case.Action>
      ... MAPPING-ACTION ...
    </map:Case.Action>
  </map:Case>
  <map:Default>
    ... MAPPING-ACTION ...
  </map:Default>
</map:Conditional>

Description

Applies mapping actions depending on conditions.

See [Writing case-based conditional mappings]( Writing case-based conditional mappings).

Properties

Property ​Usage ​Description
CaseConditions Content property A sequence of conditions

The Case element

A Case element is used to specify one possible conversion for an API.

Property ​Usage ​Description
Condition Required A code mapping condition instance
Action Required A code mapping mapping action to be executed if the condition succeeds

The Default element

The Default element contains the code mapping action to be executed when no other Case was successful. This element is required.

Example

The Conditional mapping action is useful when converting elements which vary given the origin expression.

For example the MessageBox.Show method has several overloads. We can define a separate conversion for each case, for example the following mapping will apply a different conversion if the original call expression one or three arguments.

Say that we are creating a mapping for MessageBox.Show. This method has several overloads. In this case we want to create a mapping that does something for the case when the method is called with one argument, and do something else when the method is called with three arguments.

Here's the mapping:

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">
  <MapUnit.Elements>
    <map:CodeMapPackage Type="System.Windows.MessageBox">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Call" MemberName="Show">
          <map:Conditional>
            <!-- Case for one argument -->
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:ArgumentCount>1</map:ArgumentCount>
                  <map:WithArgument Position="0">
                    <map:AssignName>$message</map:AssignName>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  await (new Windows.UI.Popups.MessageDialog($message)).ShowAsync()
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
            <!-- Case for three arguments with OK -->
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:ArgumentCount>3</map:ArgumentCount>
                  <map:WithArgument Position="0">
                    <map:AssignName>$message</map:AssignName>
                  </map:WithArgument>
                  <map:WithArgument Position="1">
                    <map:AssignName>$title</map:AssignName>
                  </map:WithArgument>
                  <map:WithArgument Position="2">
                    <map:Equals>MessageBoxButton.OK</map:Equals>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  await (new Windows.UI.Popups.MessageDialog($message, $title)).ShowAsync()
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
            <map:Default>
              <map:MarkAsNotMapped />
            </map:Default>
          </map:Conditional>
        </map:CodeMap>
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

Notes

  • The cases are evaluated from top to bottom
  • When the first case succeeds the conditional evaluation finishes
  • The Default element is required, and its inner action will be executed when no other Case succeed.

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally