Esempio n. 1
0
      private static void RemoveDuplicateBidirectionalAssociations(List<ParsingModels.ModelClass> classDataList)
      { 
         // Bidirectional associations get duplicates, and it's better to clean them here than to rely on each parser version
         List<ModelBidirectionalAssociation> allBidirectionalAssociations = classDataList.SelectMany(cls => cls.BidirectionalAssociations).ToList();

         for (int index = 0; index < allBidirectionalAssociations.Count; index++)
         {
            ModelBidirectionalAssociation keeper = allBidirectionalAssociations[index];

            ModelBidirectionalAssociation duplicate =
               allBidirectionalAssociations.Skip(index)
                                           .FirstOrDefault(a => a.SourcePropertyTypeName == keeper.TargetPropertyTypeName
                                                             && a.SourcePropertyName == keeper.TargetPropertyName
                                                             && a.TargetPropertyTypeName == keeper.SourcePropertyTypeName
                                                             && a.TargetPropertyName == keeper.SourcePropertyName);

            if (duplicate != null)
            {
               // discard the one on the target
               ParsingModels.ModelClass duplicateOwner = classDataList.Single(c => c.FullName == duplicate.TargetClassFullName);
               duplicateOwner.BidirectionalAssociations.Remove(duplicate);
               allBidirectionalAssociations.Remove(duplicate);
            }
         }
      }
Esempio n. 2
0
        private List <ModelBidirectionalAssociation> GetBidirectionalAssociations(EntityType entityType)
        {
            List <ModelBidirectionalAssociation> result = new List <ModelBidirectionalAssociation>();

            foreach (NavigationProperty navigationProperty in entityType.DeclaredNavigationProperties.Where(np => Inverse(np) != null))
            {
                ModelBidirectionalAssociation association = new ModelBidirectionalAssociation();

                association.SourceClassName      = navigationProperty.DeclaringType.Name;
                association.SourceClassNamespace = navigationProperty.DeclaringType.NamespaceName;
                association.TargetClassName      = navigationProperty.ToEndMember.GetEntityType().Name;
                association.TargetClassNamespace = navigationProperty.ToEndMember.GetEntityType().NamespaceName;

                NavigationProperty inverse = Inverse(navigationProperty);

                // the property in the source class (referencing the target class)
                association.TargetPropertyTypeName = navigationProperty.ToEndMember.GetEntityType().Name;
                association.TargetPropertyName     = navigationProperty.Name;
                association.TargetMultiplicity     = ConvertMultiplicity(navigationProperty.ToEndMember.RelationshipMultiplicity);
                association.TargetSummary          = navigationProperty.ToEndMember.Documentation?.Summary;
                association.TargetDescription      = navigationProperty.ToEndMember.Documentation?.LongDescription;

                // the property in the target class (referencing the source class)
                association.SourcePropertyTypeName = navigationProperty.FromEndMember.GetEntityType().Name;
                association.SourcePropertyName     = inverse?.Name;
                association.SourceMultiplicity     = ConvertMultiplicity(navigationProperty.FromEndMember.RelationshipMultiplicity);
                association.SourceSummary          = navigationProperty.FromEndMember.Documentation?.Summary;
                association.SourceDescription      = navigationProperty.FromEndMember.Documentation?.LongDescription;

                result.Add(association);
            }

            return(result);
        }
Esempio n. 3
0
        private List <ModelBidirectionalAssociation> GetBidirectionalAssociations(IEntityType entityType)
        {
            List <ModelBidirectionalAssociation> result = new List <ModelBidirectionalAssociation>();

            foreach (INavigation navigationProperty in entityType.GetDeclaredNavigations().Where(n => n.FindInverse() != null))
            {
                ModelBidirectionalAssociation association = new ModelBidirectionalAssociation();

                Type sourceType = navigationProperty.GetSourceType().ClrType.Unwrap();
                association.SourceClassName      = sourceType.Name;
                association.SourceClassNamespace = sourceType.Namespace;

                Type targetType = navigationProperty.GetTargetType().ClrType.Unwrap();
                association.TargetClassName      = targetType.Name;
                association.TargetClassNamespace = targetType.Namespace;

                INavigation inverse = navigationProperty.FindInverse();

                // the property in the source class (referencing the target class)
                association.TargetPropertyTypeName = navigationProperty.PropertyInfo.PropertyType.Unwrap().Name;
                association.TargetPropertyName     = navigationProperty.Name;
                association.TargetMultiplicity     = ConvertMultiplicity(navigationProperty.GetTargetMultiplicity());

                //association.TargetSummary = navigationProperty.ToEndMember.Documentation?.Summary;
                //association.TargetDescription = navigationProperty.ToEndMember.Documentation?.LongDescription;

                // the property in the target class (referencing the source class)
                association.SourcePropertyTypeName = inverse.PropertyInfo.PropertyType.Unwrap().Name;
                association.SourcePropertyName     = inverse.Name;
                association.SourceMultiplicity     = ConvertMultiplicity(navigationProperty.GetSourceMultiplicity());

                //association.SourceSummary = navigationProperty.FromEndMember.Documentation?.Summary;
                //association.SourceDescription = navigationProperty.FromEndMember.Documentation?.LongDescription;

                if (navigationProperty.ForeignKey != null)
                {
                    List <string> fkPropertyDeclarations = navigationProperty.ForeignKey.Properties
                                                           .Where(p => !p.IsShadowProperty)
                                                           .Select(p => p.Name)
                                                           .ToList();

                    association.ForeignKey = fkPropertyDeclarations.Any()
                                           ? string.Join(",", fkPropertyDeclarations)
                                           : null;
                }

                result.Add(association);
            }

            return(result);
        }
Esempio n. 4
0
        private List <ModelBidirectionalAssociation> GetBidirectionalAssociations(EntityType entityType)
        {
            List <ModelBidirectionalAssociation> result = new List <ModelBidirectionalAssociation>();

            foreach (NavigationProperty navigationProperty in entityType.DeclaredNavigationProperties.Where(np => Inverse(np) != null))
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                ModelBidirectionalAssociation association = new ModelBidirectionalAssociation();

                association.SourceClassName      = navigationProperty.DeclaringType.Name;
                association.SourceClassNamespace = navigationProperty.DeclaringType.NamespaceName;
                association.TargetClassName      = navigationProperty.ToEndMember.GetEntityType().Name;
                association.TargetClassNamespace = navigationProperty.ToEndMember.GetEntityType().NamespaceName;

                NavigationProperty inverse = Inverse(navigationProperty);

                // the property in the source class (referencing the target class)
                association.TargetPropertyTypeName = navigationProperty.ToEndMember.GetEntityType().Name;
                association.TargetPropertyName     = navigationProperty.Name;
                association.TargetMultiplicity     = ConvertMultiplicity(navigationProperty.ToEndMember.RelationshipMultiplicity);
                association.TargetSummary          = navigationProperty.ToEndMember.Documentation?.Summary;
                association.TargetDescription      = navigationProperty.ToEndMember.Documentation?.LongDescription;

                // the property in the target class (referencing the source class)
                association.SourcePropertyTypeName = navigationProperty.FromEndMember.GetEntityType().Name;
                association.SourcePropertyName     = inverse?.Name;
                association.SourceMultiplicity     = ConvertMultiplicity(navigationProperty.FromEndMember.RelationshipMultiplicity);
                association.SourceSummary          = navigationProperty.FromEndMember.Documentation?.Summary;
                association.SourceDescription      = navigationProperty.FromEndMember.Documentation?.LongDescription;
                // ReSharper restore UseObjectOrCollectionInitializer

                log.Info($"Found bidirectional association {association.SourceClassName}.{association.TargetPropertyName} <-> {association.TargetClassName}.{association.SourcePropertyTypeName}");
                log.Info("\n   " + JsonConvert.SerializeObject(association));

                result.Add(association);
            }

            return(result);
        }
Esempio n. 5
0
        private List <ModelBidirectionalAssociation> GetBidirectionalAssociations(IEntityType entityType)
        {
            List <ModelBidirectionalAssociation> result = new List <ModelBidirectionalAssociation>();

            foreach (INavigation navigationProperty in entityType.GetDeclaredNavigations().Where(n => n.FindInverse() != null))
            {
                ModelBidirectionalAssociation association = new ModelBidirectionalAssociation();

                Type sourceType = navigationProperty.GetSourceType().ClrType.Unwrap();
                association.SourceClassName      = sourceType.Name;
                association.SourceClassNamespace = sourceType.Namespace;

                Type targetType = navigationProperty.GetTargetType().ClrType.Unwrap();
                association.TargetClassName      = targetType.Name;
                association.TargetClassNamespace = targetType.Namespace;

                INavigation inverse = navigationProperty.FindInverse();

                // the property in the source class (referencing the target class)
                association.TargetPropertyTypeName = navigationProperty.PropertyInfo.PropertyType.Unwrap().Name;
                association.TargetPropertyName     = navigationProperty.Name;
                association.TargetMultiplicity     = ConvertMultiplicity(navigationProperty.GetTargetMultiplicity());

                //association.TargetSummary = navigationProperty.ToEndMember.Documentation?.Summary;
                //association.TargetDescription = navigationProperty.ToEndMember.Documentation?.LongDescription;

                // the property in the target class (referencing the source class)
                association.SourcePropertyTypeName = inverse.PropertyInfo.PropertyType.Unwrap().Name;
                association.SourcePropertyName     = inverse.Name;
                association.SourceMultiplicity     = ConvertMultiplicity(navigationProperty.GetSourceMultiplicity());

                //association.SourceSummary = navigationProperty.FromEndMember.Documentation?.Summary;
                //association.SourceDescription = navigationProperty.FromEndMember.Documentation?.LongDescription;

                result.Add(association);
            }

            return(result);
        }
Esempio n. 6
0
        private List <ModelBidirectionalAssociation> GetBidirectionalAssociations(EntityType entityType)
        {
            List <ModelBidirectionalAssociation> result = new List <ModelBidirectionalAssociation>();

            if (entityType == null)
            {
                return(result);
            }

            foreach (NavigationProperty navigationProperty in entityType.DeclaredNavigationProperties.Where(np => Inverse(np) != null))
            {
                StructuralType sourceType = navigationProperty.DeclaringType;

                if (sourceType.Name != entityType.Name)
                {
                    continue;
                }

                ModelBidirectionalAssociation association = new ModelBidirectionalAssociation();
                EntityType targetType = navigationProperty.ToEndMember.GetEntityType();

                AssociationType       associationType = navigationProperty.RelationshipType as AssociationType;
                ReferentialConstraint constraint      = associationType?.Constraint;
                AssociationEndMember  principalEnd    = constraint?.FromRole as AssociationEndMember;
                EntityType            principalType   = principalEnd?.GetEntityType();

                AssociationEndMember dependentEnd  = constraint?.ToRole as AssociationEndMember;
                EntityType           dependentType = dependentEnd?.GetEntityType();

                if (principalType?.Name == sourceType.Name)
                {
                    association.SourceRole = AssociationRole.Principal;
                    association.TargetRole = AssociationRole.Dependent;
                }
                else if (principalType?.Name == targetType.Name)
                {
                    association.TargetRole = AssociationRole.Principal;
                    association.SourceRole = AssociationRole.Dependent;
                }
                else if (principalType == null && dependentType == null)
                {
                    association.SourceRole = AssociationRole.NotApplicable;
                    association.TargetRole = AssociationRole.NotApplicable;
                }

                association.SourceClassName      = sourceType.Name;
                association.SourceClassNamespace = sourceType.NamespaceName;
                association.TargetClassName      = targetType.Name;
                association.TargetClassNamespace = targetType.NamespaceName;

                NavigationProperty inverse = Inverse(navigationProperty);

                // the property in the source class (referencing the target class)
                association.TargetPropertyTypeName = targetType.Name;
                association.TargetPropertyName     = navigationProperty.Name;
                association.TargetMultiplicity     = ConvertMultiplicity(navigationProperty.ToEndMember.RelationshipMultiplicity);
                association.TargetSummary          = navigationProperty.ToEndMember.Documentation?.Summary;
                association.TargetDescription      = navigationProperty.ToEndMember.Documentation?.LongDescription;

                // the property in the target class (referencing the source class)
                association.SourcePropertyTypeName = navigationProperty.FromEndMember.GetEntityType().Name;
                association.SourcePropertyName     = inverse?.Name;
                association.SourceMultiplicity     = ConvertMultiplicity(navigationProperty.FromEndMember.RelationshipMultiplicity);
                association.SourceSummary          = navigationProperty.FromEndMember.Documentation?.Summary;
                association.SourceDescription      = navigationProperty.FromEndMember.Documentation?.LongDescription;

                // look for declared foreign keys
                List <EdmProperty> dependentProperties = navigationProperty.GetDependentProperties().ToList();

                if (dependentProperties.Any())
                {
                    association.ForeignKey = string.Join(",", dependentProperties.Select(p => p.Name));
                }

                log.Debug($"Found bidirectional association {association.SourceClassName}.{association.TargetPropertyName} <-> {association.TargetClassName}.{association.SourcePropertyTypeName}");
                log.Debug("\n   " + JsonConvert.SerializeObject(association));
                result.Add(association);
            }

            return(result);
        }