Example #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);
            }
         }
      }
Example #2
0
      private void ProcessUnidirectionalAssociations(ParsingModels.ModelClass modelClass)
      {
         List<ModelUnidirectionalAssociation> unidirectionalAssociations = modelClass.UnidirectionalAssociations;

         foreach (ModelUnidirectionalAssociation data in unidirectionalAssociations)
         {
            if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6 
             && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany 
             && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany)
            {
               data.ForeignKey = null;
            }

            UnidirectionalAssociation existing = Store.GetAll<UnidirectionalAssociation>()
                                                      .FirstOrDefault(x => x.Target.Name == data.TargetClassName
                                                                        && x.Source.Name == data.SourceClassName
                                                                        && x.Source.Name == modelClass.Name // just to be sure
                                                                        && x.TargetPropertyName == data.TargetPropertyName);

            if (existing != null)
            {
               if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey))
               {
                  existing.FKPropertyName = data.ForeignKey;
                  existing.Source.ModelRoot.ExposeForeignKeys = true;
               }

               continue;
            }

            ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.SourceClassFullName);
            ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.TargetClassFullName);

            if (source == null || target == null || source.FullName != modelClass.FullName)
               continue;

            // ReSharper disable once UnusedVariable
            UnidirectionalAssociation element = new UnidirectionalAssociation(Store,
                                                    new[]
                                                    {
                                                       new RoleAssignment(UnidirectionalAssociation.UnidirectionalSourceDomainRoleId, source),
                                                       new RoleAssignment(UnidirectionalAssociation.UnidirectionalTargetDomainRoleId, target)
                                                    },
                                                    new[]
                                                    {
                                                       new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)),
                                                       new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)),
                                                       new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName),
                                                       new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary),
                                                       new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription),
                                                       new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey),
                                                       new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)), 
                                                       new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)), 
                                                    });
            AssociationChangedRules.SetEndpointRoles(element);
         }
      }
Example #3
0
      private void ProcessBidirectionalAssociations(ParsingModels.ModelClass modelClass)
      {
         List<ModelBidirectionalAssociation> bidirectionalAssociations = modelClass.BidirectionalAssociations;

         foreach (ModelBidirectionalAssociation data in bidirectionalAssociations)
         {
            if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6
             && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany
             && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany)
               data.ForeignKey = null;

            BidirectionalAssociation existing = Store.GetAll<BidirectionalAssociation>()
                                                     .FirstOrDefault(x => x.Target.Name == data.TargetClassName
                                                                       && x.Source.Name == data.SourceClassName
                                                                       && x.Source.Name == modelClass.Name // just to be sure
                                                                       && x.TargetPropertyName == data.TargetPropertyName
                                                                       && x.SourcePropertyName == data.SourcePropertyName)
                                             ?? Store.GetAll<BidirectionalAssociation>()
                                                     .FirstOrDefault(x => x.Source.Name == data.TargetClassName
                                                                       && x.Target.Name == data.SourceClassName
                                                                       && x.Target.Name == modelClass.Name // just to be sure
                                                                       && x.SourcePropertyName == data.TargetPropertyName
                                                                       && x.TargetPropertyName == data.SourcePropertyName);

            if (existing != null)
            {
               if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey))
               {
                  existing.FKPropertyName = string.Join(",", data.ForeignKey.Split(',').ToList().Select(p => p.Split('/').Last().Split(' ').Last()));
                  existing.Source.ModelRoot.ExposeForeignKeys = true;
               }

               continue;
            }

            ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.Name == data.SourceClassName);
            ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.Name == data.TargetClassName);

            if (source == null || target == null || source.FullName != modelClass.FullName)
               continue;

            BidirectionalAssociation elementLink = (BidirectionalAssociation)BidirectionalAssociationBuilder.Connect(source, target);
            elementLink.SourceMultiplicity = ConvertMultiplicity(data.SourceMultiplicity);
            elementLink.TargetMultiplicity = ConvertMultiplicity(data.TargetMultiplicity);
            elementLink.TargetPropertyName = data.TargetPropertyName;
            elementLink.TargetSummary = data.TargetSummary;
            elementLink.TargetDescription = data.TargetDescription;
            elementLink.FKPropertyName = data.ForeignKey;
            elementLink.SourceRole = ConvertRole(data.SourceRole);
            elementLink.TargetRole = ConvertRole(data.TargetRole);
            elementLink.SourcePropertyName = data.SourcePropertyName;
            elementLink.SourceSummary = data.SourceSummary;
            elementLink.SourceDescription = data.SourceDescription;

            // ReSharper disable once UnusedVariable
            //BidirectionalAssociation element = new BidirectionalAssociation(Store,
            //                                       new[]
            //                                       {
            //                                          new RoleAssignment(BidirectionalAssociation.BidirectionalSourceDomainRoleId, source),
            //                                          new RoleAssignment(BidirectionalAssociation.BidirectionalTargetDomainRoleId, target)
            //                                       },
            //                                       new[]
            //                                       {
            //                                          new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)),
            //                                          new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)),
            //                                          new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName),
            //                                          new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary),
            //                                          new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription),
            //                                          new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey),
            //                                          new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)),
            //                                          new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)),
            //                                          new PropertyAssignment(BidirectionalAssociation.SourcePropertyNameDomainPropertyId, data.SourcePropertyName),
            //                                          new PropertyAssignment(BidirectionalAssociation.SourceSummaryDomainPropertyId, data.SourceSummary),
            //                                          new PropertyAssignment(BidirectionalAssociation.SourceDescriptionDomainPropertyId, data.SourceDescription),
            //                                       });
            AssociationChangedRules.SetEndpointRoles(elementLink);
            AssociationChangedRules.FixupForeignKeys(elementLink);

            // we could have a situation where there are no roles assigned (if 0/1-0/1 or 1-1). If we have exposed foreign keys, though, we can figure those out.
            if ((elementLink.SourceMultiplicity != Multiplicity.ZeroMany || elementLink.TargetMultiplicity != Multiplicity.ZeroMany)
             && (elementLink.SourceRole == EndpointRole.NotSet || elementLink.TargetRole == EndpointRole.NotSet)
             && !string.IsNullOrEmpty(elementLink.FKPropertyName))
            {
               // which, if any, end has the foreign key properties in it?
               string firstFKPropertyName = elementLink.FKPropertyName.Split(',').First();

               if (elementLink.Source.AllPropertyNames.Contains(firstFKPropertyName))
               {
                  elementLink.SourceRole = EndpointRole.Dependent;
                  elementLink.TargetRole = EndpointRole.Principal;
               }
               else if (elementLink.Target.AllPropertyNames.Contains(firstFKPropertyName))
               {
                  elementLink.TargetRole = EndpointRole.Dependent;
                  elementLink.SourceRole = EndpointRole.Principal;
               }
            }
         }
      }
Example #4
0
      private void ProcessBidirectionalAssociations(ParsingModels.ModelClass modelClass)
      {
         List<ModelBidirectionalAssociation> bidirectionalAssociations = modelClass.BidirectionalAssociations;

         foreach (ModelBidirectionalAssociation data in bidirectionalAssociations)
         {
            BidirectionalAssociation existing = Store.Get<BidirectionalAssociation>()
                                                     .FirstOrDefault(x => x.Target.Name == data.TargetClassName
                                                                       && x.Source.Name == data.SourceClassName
                                                                       && x.Source.Name == modelClass.Name // just to be sure
                                                                       && x.TargetPropertyName == data.TargetPropertyName
                                                                       && x.SourcePropertyName == data.SourcePropertyName)
                                             ?? Store.Get<BidirectionalAssociation>()
                                                     .FirstOrDefault(x => x.Source.Name == data.TargetClassName
                                                                       && x.Target.Name == data.SourceClassName
                                                                       && x.Target.Name == modelClass.Name // just to be sure
                                                                       && x.SourcePropertyName == data.TargetPropertyName
                                                                       && x.TargetPropertyName == data.SourcePropertyName);

            if (existing != null)
            {
               if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey))
               {
                  existing.FKPropertyName = data.ForeignKey;
                  existing.Source.ModelRoot.ExposeForeignKeys = true;
               }

               continue;
            }

            ModelClass source = Store.Get<ModelClass>().FirstOrDefault(c => c.Name == data.SourceClassName);
            ModelClass target = Store.Get<ModelClass>().FirstOrDefault(c => c.Name == data.TargetClassName);

            if (source == null || target == null || source.FullName != modelClass.FullName)
               continue;

            // ReSharper disable once UnusedVariable
            BidirectionalAssociation element = new BidirectionalAssociation(Store,
                                                   new[]
                                                   {
                                                      new RoleAssignment(BidirectionalAssociation.BidirectionalSourceDomainRoleId, source),
                                                      new RoleAssignment(BidirectionalAssociation.BidirectionalTargetDomainRoleId, target)
                                                   },
                                                   new[]
                                                   {
                                                      new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)),
                                                      new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)),
                                                      new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName),
                                                      new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary),
                                                      new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription),
                                                      new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey),
                                                      new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)), 
                                                      new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)), 
                                                      new PropertyAssignment(BidirectionalAssociation.SourcePropertyNameDomainPropertyId, data.SourcePropertyName),
                                                      new PropertyAssignment(BidirectionalAssociation.SourceSummaryDomainPropertyId, data.SourceSummary),
                                                      new PropertyAssignment(BidirectionalAssociation.SourceDescriptionDomainPropertyId, data.SourceDescription),
                                                   });

            AssociationChangeRules.SetEndpointRoles(element);
            element.EnsureForeignKeyAttributes();
         }
      }
   private void ProcessUnidirectionalAssociations(ParsingModels.ModelClass modelClass)
   {
      List<ModelUnidirectionalAssociation> unidirectionalAssociations = modelClass.UnidirectionalAssociations;

      foreach (ModelUnidirectionalAssociation data in unidirectionalAssociations)
      {
         if (Store.ModelRoot().EntityFrameworkVersion == EFVersion.EF6
          && data.SourceMultiplicity != ParsingModels.Multiplicity.ZeroMany
          && data.TargetMultiplicity != ParsingModels.Multiplicity.ZeroMany)
         {
            data.ForeignKey = null;
         }

         UnidirectionalAssociation existing = Store.GetAll<UnidirectionalAssociation>()
                                                      .FirstOrDefault(x => x.Target.FullName == data.TargetClassFullName
                                                                        && x.Source.FullName == data.SourceClassFullName
                                                                        && x.Source.FullName == modelClass.FullName // just to be sure
                                                                        && x.TargetPropertyName == data.TargetPropertyName);

         if (existing != null)
         {
            if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey))
            {
               existing.FKPropertyName = data.ForeignKey;
               existing.Source.ModelRoot.ExposeForeignKeys = true;
            }

            continue;
         }

         ModelClass source = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.SourceClassFullName);
         ModelClass target = Store.GetAll<ModelClass>().FirstOrDefault(c => c.FullName == data.TargetClassFullName);

         if (source == null || target == null || source.FullName != modelClass.FullName)
            continue;

         UnidirectionalAssociation elementLink = (UnidirectionalAssociation)UnidirectionalAssociationBuilder.Connect(source, target);
         elementLink.SourceMultiplicity = ConvertMultiplicity(data.SourceMultiplicity);
         elementLink.TargetMultiplicity = ConvertMultiplicity(data.TargetMultiplicity);
         elementLink.TargetPropertyName = data.TargetPropertyName;
         elementLink.TargetSummary = data.TargetSummary;
         elementLink.TargetDescription = data.TargetDescription;
         elementLink.FKPropertyName = data.ForeignKey;
         elementLink.SourceRole = ConvertRole(data.SourceRole);
         elementLink.TargetRole = ConvertRole(data.TargetRole);

         AssociationChangedRules.SetEndpointRoles(elementLink);
         AssociationChangedRules.FixupForeignKeys(elementLink);

         // we could have a situation where there are no roles assigned (if 0/1-0/1 or 1-1). If we have exposed foreign keys, though, we can figure those out.
         if ((elementLink.SourceMultiplicity != Multiplicity.ZeroMany || elementLink.TargetMultiplicity != Multiplicity.ZeroMany)
          && (elementLink.SourceRole == EndpointRole.NotSet || elementLink.TargetRole == EndpointRole.NotSet)
          && !string.IsNullOrEmpty(elementLink.FKPropertyName))
         {
            // which, if any, end has the foreign key properties in it?
            string firstFKPropertyName = elementLink.FKPropertyName.Split(',').First();

            if (elementLink.Source.AllPropertyNames.Contains(firstFKPropertyName))
            {
               elementLink.SourceRole = EndpointRole.Dependent;
               elementLink.TargetRole = EndpointRole.Principal;
            }
            else if (elementLink.Target.AllPropertyNames.Contains(firstFKPropertyName))
            {
               elementLink.TargetRole = EndpointRole.Dependent;
               elementLink.SourceRole = EndpointRole.Principal;
            }
         }
      }
   }