Skip to content

Converter Collection

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

ConverterCollection is an abstract generic class designed to contain conversion ruleset in the implementation of it's abstract Configure method. Generic parameters of the ConverterCollection<TSource, TDest> represent a source document of the conversion and a destination document. Example:

public class ExampleConverterCollection : ConverterCollection<Source, Dest>
{
    public UserConverterCollection(IPathFormatterCollection pathFormatterCollection, 
                                   IStringConverter stringConverter)
        : base(pathFormatterCollection, stringConverter)
    {
    }

    protected override void Configure(MutatorsContext context, 
                                      ConverterConfigurator<Source, Dest> configurator)
    {
        configurator.Target(dest => dest.Login)
                    .Set(source => source.Email);

        configurator.Target(dest => dest.UserId)
                    .Set(source => Guid.Parse(source.UserId));

        configurator.Target(dest => dest.Properties.Each().Key)
                    .Set(source => source.Properties.Current().Item1);
        configurator.Target(dest => dest.Properties.Each().Value)
                    .Set(source => source.Properties.Current().Item2);
    }
}

The ConverterCollection class exposes the GetConverter method which takes MutatorsContext parameter and returns a conversion function of type Func<TSource, TDest>. A ConverterCollection instance caches created conversion functions for each MutatorsContext instance.

You can see more examples of converter collections and supported conversion rules in functional tests.