Skip to content

Path Migration

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

Path migration is one of additional features of Mutators connected with document validation and document conversion. It is highy recommended to make yourself familiar with these topics before proceeding with path migration.

Suppose you have received a document from a user which is represented by the class A and you have converted it to server internal representation (class B) with the help of Mutators:

public class AtoBConverterCollection : ConverterCollection<A, B>
{
    ...

    protected override void Configure(MutatorsContext context, ConverterConfigurator<A, B> configurator)
    {
        configurator.Target(b => b.Property.Of.B).Set(a => a.Property.Of.A);
        configurator.Target(b => b.Other.Property.Of.B).Set(a => a.Other.Property.Of.A);
        
        ...
    }
    
    ...
}

Then the server validates an internal representation of the document with the help of DataConfiguratorCollectionBase:

public class BDataConfiguratorCollection : DataConfiguratorCollectionBase<B>
{
    ...
    
    protected override void Configure(MutatorsContext context, MutatorsConfigurator<B> configurator)
    {
        configurator.Target(b => b.Property.Of.B)
                    .RequiredIf(b => b.Other.Property.Of.B == null);

        ...
    }

    ...
}

Suppose that Property.Of.B and Other.Property.Of.B happened to be null, so we want to show the user a message that Property.Of.B is required. The problem is that the user knows nothing about the internal representation and Property.Of.B. But we know that Property.Of.B received it's value from Property.Of.A of the document received from the user. Thus Mutators can automatically translate paths in terms of destination document to paths in terms of source document.

All we need to do is call the method MigratePaths of the converter collection instance and pass MutatorsTreeBase<B> to it. Thus we get a MutatorsTreeBase<B> instance from which we can get a validation function that produces property paths in terms of the A document:

var migratedPathsTree = converterCollection.MigratePaths(bDataConfiguratorCollection.GetMutatorsTree(dataConfigurationContext), converterContext);
var validationFunc = migratedPathsTree.GetValidator();