Esempio n. 1
0
        public INodeMappingExpression <TDestination> CollectionProperty(
            Expression <Func <TDestination, object> > destinationProperty,
            Func <int, IEnumerable <int> > mapping
            )
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }
            else if (destinationProperty == null)
            {
                throw new ArgumentNullException("destinationProperty");
            }

            var mapper = new CollectionPropertyMapper(
                (context, sourceValue) => mapping(context.Id),
                null,
                _nodeMapper,
                destinationProperty.GetPropertyInfo(),
                null
                );

            _nodeMapper.InsertPropertyMapper(mapper);

            return(this);
        }
Esempio n. 2
0
        public INodeMappingExpression <TDestination> ForProperty <TProperty>(
            Expression <Func <TDestination, TProperty> > destinationProperty,
            string nodeTypeAlias
            )
        {
            if (string.IsNullOrEmpty(nodeTypeAlias))
            {
                throw new ArgumentException("Node type alias cannot be null", "nodeTypeAlias");
            }

            var destinationPropertyInfo = destinationProperty.GetPropertyInfo();
            var propertyType            = destinationPropertyInfo.PropertyType;
            PropertyMapperBase mapper   = null;

            if (propertyType.GetMappedPropertyType() == MappedPropertyType.SystemOrEnum)
            {
                mapper = new BasicPropertyMapper(
                    null,
                    null,
                    _nodeMapper,
                    destinationPropertyInfo,
                    nodeTypeAlias
                    );
            }
            else if (propertyType.GetMappedPropertyType() == MappedPropertyType.Model)
            {
                mapper = new SinglePropertyMapper(
                    null,
                    null,
                    _nodeMapper,
                    destinationPropertyInfo,
                    nodeTypeAlias
                    );
            }
            else if (propertyType.GetMappedPropertyType() == MappedPropertyType.Collection)
            {
                mapper = new CollectionPropertyMapper(
                    null,
                    null,
                    _nodeMapper,
                    destinationPropertyInfo,
                    nodeTypeAlias
                    );
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                          "TProperty",
                          string.Format("The property type {0} on model {1} is not supported.", propertyType.FullName, typeof(TDestination).FullName)
                          );
            }

            _nodeMapper.InsertPropertyMapper(mapper);

            return(this);
        }
Esempio n. 3
0
        public INodeMappingExpression <TDestination> CollectionProperty(
            Expression <Func <TDestination, object> > destinationProperty,
            string propertyAlias
            )
        {
            if (string.IsNullOrEmpty(propertyAlias))
            {
                throw new ArgumentException("Property alias cannot be null", "propertyAlias");
            }

            var mapper = new CollectionPropertyMapper(
                null,
                null,
                _nodeMapper,
                destinationProperty.GetPropertyInfo(),
                propertyAlias
                );

            _nodeMapper.InsertPropertyMapper(mapper);

            return(this);
        }
Esempio n. 4
0
        public INodeMappingExpression <TDestination> CollectionProperty <TSourceProperty>(
            Expression <Func <TDestination, object> > destinationProperty,
            CollectionPropertyMapping <TSourceProperty> mapping,
            string propertyAlias = null
            )
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }

            var mapper = new CollectionPropertyMapper(
                (context, value) => mapping((TSourceProperty)value),
                typeof(TSourceProperty),
                _nodeMapper,
                destinationProperty.GetPropertyInfo(),
                propertyAlias
                );

            _nodeMapper.InsertPropertyMapper(mapper);

            return(this);
        }
Esempio n. 5
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);
                }
            }
        }