Translates from one ModelType to another using a set of property translation expressions.
Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="ModelTranslator"/> to support translation from the specified source <see cref="ModelType"/>
        /// to the specified destination <see cref="ModelType"/> with a specified set of property translations.
        /// </summary>
        /// <param name="translation"></param>
        public ModelTranslator(ModelType sourceType, ModelType destinationType, ModelTranslator parent = null, IEnumerable <string> mappings = null, Func <ModelProperty, TypeConverter> getValueConverter = null, Func <ModelInstance, ModelInstance> createDestinationInstance = null)
        {
            this.SourceType                = sourceType;
            this.DestinationType           = destinationType;
            this.Parent                    = parent;
            this.createDestinationInstance = createDestinationInstance ?? ((source) => DestinationType.Create(source.Id) ?? DestinationType.Create());
            this.getValueConverter         = getValueConverter ?? (property => property is ModelValueProperty ? ((ModelValueProperty)property).Converter : null);
            this.Properties                = new List <PropertyTranslator>();

            // Create a compiled translation for each property mapping
            if (mappings != null)
            {
                foreach (var mapping in mappings)
                {
                    var index = mapping.IndexOf("=");
                    if (index < 0)
                    {
                        throw new ArgumentException("Invalid mapping expression: must be in the form of 'Destination Path = Source Expression'.");
                    }

                    var sourceExpression = mapping.Substring(index + 1).Trim();
                    var destinationPath  = mapping.Substring(0, index).Trim();
                    AddPropertyTranslation(sourceExpression, destinationPath);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="ModelTranslator"/> to support translation from the specified source <see cref="ModelType"/> 
        /// to the specified destination <see cref="ModelType"/> with a specified set of property translations.
        /// </summary>
        /// <param name="translation"></param>
        public ModelTranslator(ModelType sourceType, ModelType destinationType, ModelTranslator parent = null,IEnumerable<string> mappings = null, Func<ModelProperty, TypeConverter> getValueConverter = null, Func<ModelInstance, ModelInstance> createDestinationInstance = null)
        {
            this.SourceType = sourceType;
            this.DestinationType = destinationType;
            this.Parent = parent;
            this.createDestinationInstance = createDestinationInstance ?? ((source) => DestinationType.Create(source.Id) ?? DestinationType.Create());
            this.getValueConverter = getValueConverter ?? (property => property is ModelValueProperty ? ((ModelValueProperty)property).Converter : null);
            this.Properties = new List<PropertyTranslator>();

            // Create a compiled translation for each property mapping
            if (mappings != null)
            {
                foreach (var mapping in mappings)
                {
                    var index = mapping.IndexOf("=");
                    if (index < 0)
                        throw new ArgumentException("Invalid mapping expression: must be in the form of 'Destination Path = Source Expression'.");

                    var sourceExpression = mapping.Substring(index + 1).Trim();
                    var destinationPath = mapping.Substring(0, index).Trim();
                    AddPropertyTranslation(sourceExpression, destinationPath);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a value <see cref="PropertyTranslator"/> to the current <see cref="ModelTranslator"/>.
        /// </summary>
        /// <param name="sourceExpression">The source expression</param>
        /// <param name="destinationPath">The destination path</param>
        /// <param name="valueConverter">The optional value converter to use</param>
        public void AddPropertyTranslation(string sourceExpression, string destinationPath, ModelTranslator referenceConverter)
        {
            // Create translator
            var translator = CreatePropertyTranslator(sourceExpression, destinationPath);
            translator.ReferenceConverter = referenceConverter;

            // Add the new value property translation
            Properties.Add(translator);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a value <see cref="PropertyTranslator"/> to the current <see cref="ModelTranslator"/>.
        /// </summary>
        /// <param name="sourceExpression">The source expression</param>
        /// <param name="destinationPath">The destination path</param>
        /// <param name="valueConverter">The optional value converter to use</param>
        public void AddPropertyTranslation(string sourceExpression, string destinationPath, ModelTranslator referenceConverter)
        {
            // Create translator
            var translator = CreatePropertyTranslator(sourceExpression, destinationPath);

            translator.ReferenceConverter = referenceConverter;

            // Add the new value property translation
            Properties.Add(translator);
        }