/// <summary> /// Creates a map to a strong type from an Umbraco document type. /// </summary> /// <typeparam name="TDestination">The type to map to.</typeparam> /// <param name="documentTypeAlias">The document type alias to map from.</param> /// <returns>Further mapping configuration</returns> /// <exception cref="DocumentTypeNotFoundException"> /// If the <paramref name="documentTypeAlias"/> could not be found /// </exception> public INodeMappingExpression <TDestination> CreateMap <TDestination>(string documentTypeAlias) where TDestination : class, new() { var destinationType = typeof(TDestination); // Remove current mapping if any if (NodeMappers.ContainsKey(destinationType)) { NodeMappers.Remove(destinationType); } // Get document type var docType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(documentTypeAlias); if (docType == null) { throw new DocumentTypeNotFoundException(documentTypeAlias); } var nodeMapper = new NodeMapper(this, destinationType, docType); NodeMappers[destinationType] = nodeMapper; if (_cacheProvider != null) { _cacheProvider.Clear(); } return(new NodeMappingExpression <TDestination>(this, nodeMapper)); }
/// <summary> /// Maps a basic property value. /// </summary> /// <param name="mapping"> /// The mapping for the property value. Cannot be null. /// </param> /// <param name="sourcePropertyType"> /// The type of the first parameter being supplied to <paramref name="mapping"/>. /// Cannot be <c>null</c>. /// </param> /// <param name="sourcePropertyAlias"> /// The alias of the node property to map from. Required. /// </param> /// <param name="nodeMapper"></param> /// <param name="destinationProperty"></param> public BasicPropertyMapper( Func<object, object> mapping, Type sourcePropertyType, NodeMapper nodeMapper, PropertyInfo destinationProperty, string sourcePropertyAlias ) :base(nodeMapper, destinationProperty) { if (sourcePropertyType == null && mapping != null) { throw new ArgumentNullException("sourcePropertyType", "Source property type must be specified when using a mapping"); } if (sourcePropertyAlias == null) { sourcePropertyAlias = NodeMapper.GetPropertyAlias(destinationProperty); if (sourcePropertyAlias == null) { throw new PropertyAliasNotFoundException(sourcePropertyType, destinationProperty, sourcePropertyAlias); } } SourcePropertyAlias = sourcePropertyAlias; RequiresInclude = false; AllowCaching = true; _mapping = mapping; _sourcePropertyType = sourcePropertyType; }
/// <summary> /// Maps a collection relationship. /// </summary> /// <param name="mapping"> /// Mapping to a collection of node IDs. Takes the context and source property value /// as parameters. If <c>null</c>, the mapping will be deduced from /// the other parameters. /// </param> /// <param name="sourcePropertyType"> /// The type of object being supplied to <paramref name="mapping"/>. /// Will be set to <c>IEnumerable{int}</c> if <paramref name="mapping"/> is specified. /// </param> /// <param name="sourcePropertyAlias"> /// The alias of the node property to map from. If null, descendants of /// the node which are compatible with <paramref name="destinationProperty"/> /// will be mapped instead. /// </param> /// <param name="nodeMapper"></param> /// <param name="destinationProperty"></param> public CollectionPropertyMapper( Func<NodeMappingContext, object, IEnumerable<int>> mapping, Type sourcePropertyType, NodeMapper nodeMapper, PropertyInfo destinationProperty, string sourcePropertyAlias ) : base(nodeMapper, destinationProperty) { if (sourcePropertyType == null && mapping != null) { sourcePropertyType = typeof(IEnumerable<int>); } if (sourcePropertyAlias == null && mapping != null && !typeof(IEnumerable<int>).IsAssignableFrom(sourcePropertyType)) { throw new ArgumentException("If specifying a mapping for a collection with no property alias, the source property type must implement IEnumerable<int>."); } _elementType = destinationProperty.PropertyType.GetGenericArguments().FirstOrDefault(); if (_elementType == null || !typeof(IEnumerable).IsAssignableFrom(destinationProperty.PropertyType)) { throw new CollectionTypeNotSupportedException(destinationProperty.PropertyType); } Type rawCollectionType = null; if (_elementType == typeof(int)) { // Collection of IDs RequiresInclude = false; rawCollectionType = typeof(IEnumerable<int>); } else { // Collection of models RequiresInclude = true; rawCollectionType = typeof(IEnumerable<>).MakeGenericType(_elementType); } // See if the collection can be assigned, or must be instantiated _canAssignDirectly = CheckCollectionCanBeAssigned(DestinationInfo.PropertyType, rawCollectionType); SourcePropertyAlias = sourcePropertyAlias; AllowCaching = true; _mapping = mapping; _sourcePropertyType = sourcePropertyType; }
public NodeMappingExpression(NodeMappingEngine engine, NodeMapper mapping) { if (mapping == null) { throw new ArgumentNullException("mapping"); } else if (mapping == null) { throw new ArgumentNullException("engine"); } _nodeMapper = mapping; _engine = engine; }