-
Notifications
You must be signed in to change notification settings - Fork 4
Converter Configurator
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.
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.
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);
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);
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.