Skip to content

Mappings

JVerbruggen edited this page Jan 11, 2021 · 11 revisions

To make mappings extensible, an abstract layer has been created to switch mappings easily within the logic. These mappings are managed by the InstanceManager and can be configured before using them.

Mapping interface

The mapping interface can be implemented to make a mapping between two objects. An input and output type has to be defined when it is used.

MappingSelector interface

Sometimes, variant mapping logic is necessary to map an object when it varies in certain properties. To select the accurate mapping for an object, the MappingSelector is used.

Configuration

MappingSelectors are registered to the InstanceManager. When registered, they can be used by the app's logic.

The forms app registers these MappingSelectors to the InstanceManager in the Dependencies class.

Usage

Mappings can be requested from the InstanceManager by the var mapping = GetMappingSelector<T, U>().Select(T input) method. Then, map an instance by invoking var output = mapping.Map(input).

Complex input and output

In some cases, some more complex input and/or output is necessary for a certain use case. For this, you can use IMappingDepends<T> and IMappingResult<T> interfaces.

Adding new Mappings

There's a tutorial on another wiki on how to do this. You can find it here:

Creating new mappings

Changing already existing Mappings

To change mappings, make sure you've followed the tutorial on creating new mappings first.

Instead of changing already existing mappings, it is expected to create new mappings and selectors, to maintain the functionality of the program. This way, the application can fall back on the other MappingSelector and Mappings.

You can remove the registration of a MappingSelector to the InstanceManager by simply removing it from the dependencies. Then, simply add the new MappingSelector you created.

Design choices

Mappings make use of the strategy pattern. This is a behavioral design pattern that enables selecting an algorithm at runtime. The algorithm may vary independently from the classes that use them.

Diagrams

strategypattern-Class diagram
A class diagram showing the overall mapping structure.

strategypattern-Sequence diagram
The mapping algorithm can differ because the MappingSelector selects the appropriate algorithm for a given object.

Validating mappings

Is there a way for monitoring mapping coverage, so that not one possible mapping is missed?

I think this is very hard to do because mapping is not simply 1 to 1 in all cases. Often multiple variables are used to construct a certain object. So simply testing whether every variable has been edited in the code, is not a solution.

You can write a unit test for every possible combination of variables, where different mappings are expected, which is a viable solution. But as both models evolve, these will have to be updated along with the mappings.

Another way to test the integrity of mapping is to write algorithms for both ends, checking if the behavior is the same. Although this is very time-intensive, it is a good way to validate software integrity.

Clone this wiki locally