예제 #1
0
        private void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList <string> mappedProperties)
        {
            var discriminatorSet = false;
            var isDiscriminated  = expressions.IsDiscriminated(classType);

            foreach (var inheritedClass in mappingTypes.Where(q =>
                                                              q.Type.BaseType == classType &&
                                                              !expressions.IsConcreteBaseType(q.Type.BaseType)))
            {
                if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
                {
                    var discriminatorColumn = expressions.DiscriminatorColumn(classType);
                    var discriminator       = new DiscriminatorMapping
                    {
                        ContainingEntityType = classType,
                        Type = new TypeReference(typeof(string))
                    };
                    discriminator.AddDefaultColumn(new ColumnMapping {
                        Name = discriminatorColumn
                    });

                    ((ClassMapping)mapping).Discriminator = discriminator;
                    discriminatorSet = true;
                }

                ISubclassMapping subclassMapping;
                var subclassStrategy = expressions.SubclassStrategy(classType);

                if (subclassStrategy == SubclassStrategy.JoinedSubclass)
                {
                    // TODO: This id name should be removed. Ideally it needs to be set by a
                    // default and be overridable by a convention (preferably the ForeignKey convention
                    // that already exists)
                    var subclass = new JoinedSubclassMapping
                    {
                        Type = inheritedClass.Type
                    };

                    subclass.Key = new KeyMapping();
                    subclass.Key.AddDefaultColumn(new ColumnMapping {
                        Name = mapping.Type.Name + "_id"
                    });

                    subclassMapping = subclass;
                }
                else
                {
                    subclassMapping = new SubclassMapping();
                }

                MapSubclass(mappedProperties, subclassMapping, inheritedClass);

                mapping.AddSubclass(subclassMapping);

                MergeMap(inheritedClass.Type, (ClassMappingBase)subclassMapping, mappedProperties);
            }
        }
예제 #2
0
        private void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList <Member> mappedMembers)
        {
            var discriminatorSet = HasDiscriminator(mapping);
            var isDiscriminated  = cfg.IsDiscriminated(classType) || discriminatorSet;
            var mappingTypesWithLogicalParents = GetMappingTypesWithLogicalParents();

            foreach (var inheritedClass in mappingTypesWithLogicalParents
                     .Where(x => x.Value != null && x.Value.Type == classType)
                     .Select(x => x.Key))
            {
                if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
                {
                    var discriminatorColumn = cfg.GetDiscriminatorColumn(classType);
                    var discriminator       = new DiscriminatorMapping
                    {
                        ContainingEntityType = classType,
                        Type = new TypeReference(typeof(string))
                    };
                    discriminator.AddDefaultColumn(new ColumnMapping {
                        Name = discriminatorColumn
                    });

                    ((ClassMapping)mapping).Discriminator = discriminator;
                    discriminatorSet = true;
                }

                SubclassMapping subclassMapping;

                if (!isDiscriminated)
                {
                    subclassMapping = new SubclassMapping(SubclassType.JoinedSubclass)
                    {
                        Type = inheritedClass.Type
                    };
                    subclassMapping.Key = new KeyMapping();
                    subclassMapping.Key.AddDefaultColumn(new ColumnMapping {
                        Name = mapping.Type.Name + "_id"
                    });
                }
                else
                {
                    subclassMapping = new SubclassMapping(SubclassType.Subclass)
                    {
                        Type = inheritedClass.Type
                    }
                };

                // track separate set of properties for each sub-tree within inheritance hierarchy
                var subclassMembers = new List <Member>(mappedMembers);
                MapSubclass(subclassMembers, subclassMapping, inheritedClass);

                mapping.AddSubclass(subclassMapping);

                MergeMap(inheritedClass.Type, subclassMapping, subclassMembers);
            }
        }

        bool HasDiscriminator(ClassMappingBase mapping)
        {
            if (mapping is ClassMapping && ((ClassMapping)mapping).Discriminator != null)
            {
                return(true);
            }

            return(false);
        }

        Dictionary <AutoMapType, AutoMapType> GetMappingTypesWithLogicalParents()
        {
            var excludedTypes = mappingTypes
                                .Where(x => cfg.IsConcreteBaseType(x.Type.BaseType))
                                .ToArray();
            var availableTypes = mappingTypes.Except(excludedTypes);
            var mappingTypesWithLogicalParents = new Dictionary <AutoMapType, AutoMapType>();

            foreach (var type in availableTypes)
            {
                mappingTypesWithLogicalParents.Add(type, GetLogicalParent(type.Type, availableTypes));
            }
            return(mappingTypesWithLogicalParents);
        }

        AutoMapType GetLogicalParent(Type type, IEnumerable <AutoMapType> availableTypes)
        {
            if (type.BaseType == typeof(object) || type.BaseType == null)
            {
                return(null);
            }

            var baseType = availableTypes.FirstOrDefault(x => x.Type == type.BaseType);

            if (baseType != null)
            {
                return(baseType);
            }

            return(GetLogicalParent(type.BaseType, availableTypes));
        }