Ejemplo n.º 1
0
        private static Type DetermineEntityType(Expression expression)
        {
            var type = expression.Type;

            return(typeof(IEntityCore).IsAssignableFrom(type)
                       ? type
                       : LinqUtils.DetermineEntityTypeFromEntityCollectionType(type));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Discovers the entity navigators.
        /// </summary>
        /// <param name="entitySchemaData">The entity schema data.</param>
        private void DiscoverEntityNavigators(EntityTypeSchemaData entitySchemaData)
        {
            var entityType = entitySchemaData.EntityType;

            if (entitySchemaData.Factory == null)
            {
                throw new InvalidOperationException(string.Format("Factory is null in entitySchemaData for entity '{0}'", entitySchemaData.EntityType.FullName));
            }
            var dummyInstance              = entitySchemaData.Factory.Create();
            var relationsWithMappedFields  = dummyInstance.GetAllRelations().Where(r => !string.IsNullOrEmpty(r.MappedFieldName) && !r.IsHierarchyRelation);
            var relationPerMappedFieldName = relationsWithMappedFields.ToDictionary(r => r.MappedFieldName);

            var properties          = TypeDescriptor.GetProperties(entityType).Cast <PropertyDescriptor>();
            var inheritedProperties = new HashSet <PropertyDescriptor>(DetermineInheritedProperties(entitySchemaData.EntityType, properties));
            var childrenCollection  = entitySchemaData.RelatedExplorerItem.Children;

            foreach (PropertyDescriptor property in properties)
            {
                if (!(typeof(IEntityCore).IsAssignableFrom(property.PropertyType) || typeof(IEntityCollectionCore).IsAssignableFrom(property.PropertyType)))
                {
                    // not an entity navigator,
                    continue;
                }
                string suffix    = string.Empty;
                bool   inherited = false;
                if (inheritedProperties.Contains(property))
                {
                    suffix    = string.Format(" (Inherited from '{0}')", property.ComponentType.Name.Replace("Entity", ""));
                    inherited = true;
                }
                // relationMapped can be null, in the case of a m:n navigator.
                IEntityRelation relationMapped = null;
                relationPerMappedFieldName.TryGetValue(property.Name, out relationMapped);
                if (typeof(IEntityCore).IsAssignableFrom(property.PropertyType))
                {
                    var relatedEntitySchemaData = GetEntitySchemaDataForType(property.PropertyType);
                    // single entity navigator, or property added manually.
                    if (relationMapped == null)
                    {
                        // property added manually, ignore.
                        continue;
                    }
                    var icon = relationMapped.TypeOfRelation == RelationType.ManyToOne ? ExplorerIcon.ManyToOne : ExplorerIcon.OneToOne;
                    if (inherited)
                    {
                        icon = ExplorerIcon.Inherited;
                    }
                    childrenCollection.Add(new ExplorerItem(property.Name + suffix, ExplorerItemKind.ReferenceLink, icon)
                    {
                        DragText        = property.Name,
                        HyperlinkTarget = relatedEntitySchemaData == null ? null : relatedEntitySchemaData.RelatedExplorerItem
                    });
                    continue;
                }
                if (typeof(IEntityCollectionCore).IsAssignableFrom(property.PropertyType))
                {
                    // collection navigator. We have to determine which entity type is returned from the collection.
                    // First, check if there's a TypeContainedAttribute on the property. If so, use the type in the attribute. If not,
                    // try to determine the type using the linq utils method.
                    Type containedType          = null;
                    var  typeContainedAttribute = property.Attributes[typeof(TypeContainedAttribute)] as TypeContainedAttribute;
                    if ((typeContainedAttribute != null) && typeContainedAttribute.TypeContainedInCollection != null)
                    {
                        containedType = typeContainedAttribute.TypeContainedInCollection;
                    }
                    else
                    {
                        containedType = LinqUtils.DetermineEntityTypeFromEntityCollectionType(property.PropertyType);
                    }
                    var relatedEntitySchemaData = GetEntitySchemaDataForType(containedType);
                    var icon = relationMapped == null ? ExplorerIcon.ManyToMany : ExplorerIcon.OneToMany;
                    if (inherited)
                    {
                        icon = ExplorerIcon.Inherited;
                    }
                    childrenCollection.Add(new ExplorerItem(property.Name + suffix, ExplorerItemKind.CollectionLink, icon)
                    {
                        DragText        = property.Name,
                        HyperlinkTarget = relatedEntitySchemaData == null ? null : relatedEntitySchemaData.RelatedExplorerItem
                    });
                }
            }
        }