Exemplo n.º 1
0
        public INodeMappingExpression <TDestination> DefaultProperty <TSourceProperty, TDestinationProperty>(
            Expression <Func <TDestination, TDestinationProperty> > destinationProperty,
            Expression <Func <Node, TSourceProperty> > nodeProperty,
            Func <TSourceProperty, TDestinationProperty> mapping
            )
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }
            else if (destinationProperty == null)
            {
                throw new ArgumentNullException("destinationProperty");
            }
            else if (nodeProperty == null)
            {
                throw new ArgumentNullException("nodeProperty");
            }

            var mapper = new DefaultPropertyMapper(
                _nodeMapper,
                destinationProperty.GetPropertyInfo(),
                nodeProperty.GetPropertyInfo(),
                x => (object)mapping((TSourceProperty)x)
                );

            _nodeMapper.InsertPropertyMapper(mapper);

            return(this);
        }
Exemplo n.º 2
0
        public NodeMapper(NodeMappingEngine engine, Type destinationType, IContentType sourceDocumentType)
        {
            if (sourceDocumentType == null)
            {
                throw new ArgumentNullException("sourceDocumentType");
            }
            else if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }
            else if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            SourceDocumentType = sourceDocumentType;
            Engine             = engine;
            DestinationType    = destinationType;
            PropertyMappers    = new List <PropertyMapperBase>();

            // See if base properties have been mapped already.
            var baseNodeMapper = Engine.GetBaseNodeMapperForType(destinationType);

            if (baseNodeMapper != null)
            {
                // Use the property mappings of the closest parent
                // (ideally they will already have the mappings of all their ancestors)
                PropertyMappers.AddRange(baseNodeMapper.PropertyMappers);
            }

            // Map properties
            foreach (var destinationProperty in destinationType.GetProperties())
            {
                if (PropertyMappers.Any(mapper => mapper.DestinationInfo.Name == destinationProperty.Name) || !destinationProperty.CanWrite)
                {
                    // A mapping already exists for this property on a base type or its a ReadOnly property.
                    continue;
                }

                PropertyMapperBase propertyMapper = null;

                var sourcePropertyAlias = GetPropertyAlias(destinationProperty);

                // Check if this property is equivalent to a default Node property.
                var defaultNodeProperty = DefaultPropertyMapper.GetDefaultMappingForName(destinationProperty);

                if (sourcePropertyAlias != null && // check corresponding source property alias was found
                    destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.SystemOrEnum)
                {
                    propertyMapper = new BasicPropertyMapper(
                        x => x,                           // direct mapping
                        destinationProperty.PropertyType, // using the desired type
                        this,
                        destinationProperty,
                        sourcePropertyAlias
                        );
                }
                else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Model)
                {
                    propertyMapper = new SinglePropertyMapper(
                        null,
                        null,
                        this,
                        destinationProperty,
                        sourcePropertyAlias // can be null to map ancestor
                        );
                }
                else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Collection)
                {
                    propertyMapper = new CollectionPropertyMapper(
                        null,
                        null,
                        this,
                        destinationProperty,
                        sourcePropertyAlias // can be null to map descendants
                        );
                }
                else if (defaultNodeProperty != null)
                {
                    propertyMapper = new DefaultPropertyMapper(
                        this,
                        destinationProperty,
                        defaultNodeProperty,
                        null
                        );
                }

                if (propertyMapper != null)
                {
                    PropertyMappers.Add(propertyMapper);
                }
            }
        }