Skip to content

Converter Configurator

Anton Chaplygin edited this page Sep 19, 2019 · 2 revisions

ConverterConfigurator is a class that used to configure conversion rules inside the Configure method of the ConverterCollection class. You can see many examples of it's usage in the functional tests.

Target method

The Target method takes a lambda expression (path to a property of the destination document) and returns a new instance of ConverterConfigurator that stores path to a destination property (property we want to set into). On calling the Target method no conversion rule will be recorded, it will only happen after applying the Set method.

Set method

The Set method is used to specify a path to a property of the source document, which value we want to set into a target property. It should be applied after the Target method which specifies where we want to set a value.

configurator.Target(dest => dest.Path.To.DestProperty)
            .Set(source => source.Source.Property.Path);

Goto method

The Goto method helps you get rid of boilerplate code. It allows you to travel to a subnode of the source and the target document, so all further Target and Set methods will be taking paths from this subnode.

Instead of

configurator.Target(x => x.Path.To.Dest.DestProperty).Set(y => y.Path.To.Source.SourceProperty);

configurator.Target(x => x.Path.To.Dest.Other.DestProperty).Set(y => y.Path.To.Source.Other.SourceProperty);

We can write

var newConfigurator = configurator.Goto(x => x.Path.To.Dest, y => y.Path.To.Source);

newConfigurator.Target(x => x.DestProperty).Set(y => y.SourceProperty);
newConfigurator.Target(x => x.Other.DestProperty).Set(y => y.Other.SourceProperty);

If method

In case you need conversion to happen only when some condition is satisfied, you can use the If method of a configurator. It takes as a parameter lambda expression from TSource to bool?. All covnersions recorded using a configurator instance with the If method applied will happen only if the specified condition is satisfied.

configurator.If(source => source.SomeString.Length == 10)
            .Target(dest => dest.DestValue)
            .Set(source => source.SourceValue);

The If method can be applied multiple times, it will mean that for all further conversions to happen all conditions should be satisfied.