public override void ElementAdded(ElementAddedEventArgs e)
        {
            base.ElementAdded(e);

            Association element = (Association)e.ModelElement;

            Store       store   = element.Store;
            Transaction current = store.TransactionManager.CurrentTransaction;

            if (current.IsSerializing || ModelRoot.BatchUpdating)
            {
                return;
            }

            if (e.ModelElement is UnidirectionalAssociation u)
            {
                ConfigureNewAssociation(u);
            }
            else if (e.ModelElement is BidirectionalAssociation b)
            {
                ConfigureNewAssociation(b);
            }

            AssociationChangedRules.SetEndpointRoles(element);
            PresentationHelper.UpdateAssociationDisplay(element);
        }
示例#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);
         }
      }
示例#3
0
      private List<ModelElement> ProcessRootData(ParsingModels.ModelRoot rootData)
      {
         List<ModelElement> result = new List<ModelElement>();
         ModelRoot modelRoot = Store.ModelRoot();

         modelRoot.EntityContainerName = rootData.EntityContainerName;
         modelRoot.Namespace = rootData.Namespace;

         result.AddRange(ProcessClasses(modelRoot, rootData.Classes));
         result.AddRange(ProcessEnumerations(modelRoot, rootData.Enumerations));

         foreach (Association association in modelRoot.Store.GetAll<Association>())
         {
            AssociationChangedRules.SetEndpointRoles(association);
            AssociationChangedRules.FixupForeignKeys(association);
         }

         return result;
      }
示例#4
0
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            base.ElementAdded(e);

            Association          element = (Association)e.ModelElement;
            Store                store   = element.Store;
            Transaction          current = store.TransactionManager.CurrentTransaction;
            PluralizationService pluralizationService = ModelRoot.PluralizationService;

            if (current.IsSerializing || ModelRoot.BatchUpdating)
            {
                return;
            }

            // add unidirectional
            //    source can't be dependent (connection builder handles this)
            // if target is dependent,
            //    source cardinality is 0..1
            //    target cardinality is 0..1
            //    source is principal
            if (element is UnidirectionalAssociation && element.Target.IsDependentType)
            {
                element.TargetMultiplicity = Multiplicity.ZeroOne;
                element.SourceRole         = EndpointRole.Principal;
                element.TargetRole         = EndpointRole.Dependent;
            }

            // add bidirectional
            //    neither can be dependent (connection builder handles this)

            if (string.IsNullOrEmpty(element.TargetPropertyName))
            {
                string rootName = element.TargetMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Target.Name) == true
                                 ? pluralizationService.Pluralize(element.Target.Name)
                                 : element.Target.Name;

                string identifierName = rootName;
                int    index          = 0;

                while (element.Source.HasPropertyNamed(identifierName))
                {
                    identifierName = $"{rootName}_{++index}";
                }

                element.TargetPropertyName = identifierName;
            }

            if (element is BidirectionalAssociation bidirectionalAssociation)
            {
                if (string.IsNullOrEmpty(bidirectionalAssociation.SourcePropertyName))
                {
                    string rootName = element.SourceMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Source.Name) == true
                                    ? pluralizationService.Pluralize(element.Source.Name)
                                    : element.Source.Name;

                    string identifierName = rootName;
                    int    index          = 0;

                    while (element.Target.HasPropertyNamed(identifierName))
                    {
                        identifierName = $"{rootName}_{++index}";
                    }

                    bidirectionalAssociation.SourcePropertyName = identifierName;
                }
            }

            AssociationChangedRules.SetEndpointRoles(element);
            PresentationHelper.UpdateAssociationDisplay(element);
        }
示例#5
0
        private void ProcessAssociation([NotNull] ModelClass source, [NotNull] ModelClass target, [NotNull] PropertyDeclarationSyntax propertyDecl, bool toMany = false)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (propertyDecl == null)
            {
                throw new ArgumentNullException(nameof(propertyDecl));
            }

            using (Transaction tx = Store.TransactionManager.BeginTransaction("processing associations"))
            {
                string propertyName = propertyDecl.Identifier.ToString();

                // since we don't have enough information from the code, we'll create unidirectional associations
                // cardinality 1 on the source end, 0..1 or 0..* on the target, depending on the parameter

                XMLDocumentation xmlDocumentation = new XMLDocumentation(propertyDecl);

                // if the association doesn't yet exist, create it
                if (!Store.ElementDirectory
                    .AllElements
                    .OfType <UnidirectionalAssociation>()
                    .Any(a => a.Source == source &&
                         a.Target == target &&
                         a.TargetPropertyName == propertyName))
                {
                    // if there's a unidirectional going the other direction, we'll whack that one and make a bidirectional
                    // otherwise, proceed as planned
                    UnidirectionalAssociation compliment = Store.ElementDirectory
                                                           .AllElements
                                                           .OfType <UnidirectionalAssociation>()
                                                           .FirstOrDefault(a => a.Source == target &&
                                                                           a.Target == source);

                    if (compliment == null)
                    {
                        UnidirectionalAssociation element =
                            new UnidirectionalAssociation(Store,
                                                          new[]
                        {
                            new RoleAssignment(UnidirectionalAssociation.UnidirectionalSourceDomainRoleId, source),
                            new RoleAssignment(UnidirectionalAssociation.UnidirectionalTargetDomainRoleId, target)
                        },
                                                          new[]
                        {
                            new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, Multiplicity.One),

                            new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, toMany ? Multiplicity.ZeroMany : Multiplicity.ZeroOne),
                            new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, propertyName),
                            new PropertyAssignment(Association.TargetSummaryDomainPropertyId, xmlDocumentation.Summary),
                            new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, xmlDocumentation.Description)
                        });
                        AssociationChangedRules.SetEndpointRoles(element);
                    }
                    else
                    {
                        compliment.Delete();

                        BidirectionalAssociation element =
                            new BidirectionalAssociation(Store,
                                                         new[]
                        {
                            new RoleAssignment(BidirectionalAssociation.BidirectionalSourceDomainRoleId, source),
                            new RoleAssignment(BidirectionalAssociation.BidirectionalTargetDomainRoleId, target)
                        },
                                                         new[]
                        {
                            new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, compliment.TargetMultiplicity),
                            new PropertyAssignment(BidirectionalAssociation.SourcePropertyNameDomainPropertyId, compliment.TargetPropertyName),
                            new PropertyAssignment(BidirectionalAssociation.SourceSummaryDomainPropertyId, compliment.TargetSummary),
                            new PropertyAssignment(BidirectionalAssociation.SourceDescriptionDomainPropertyId, compliment.TargetDescription),

                            new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, toMany ? Multiplicity.ZeroMany : Multiplicity.ZeroOne),
                            new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, propertyName),
                            new PropertyAssignment(Association.TargetSummaryDomainPropertyId, xmlDocumentation.Summary),
                            new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, xmlDocumentation.Description)
                        });
                        AssociationChangedRules.SetEndpointRoles(element);
                    }
                }

                tx.Commit();
            }
        }
示例#6
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;
               }
            }
         }
      }
   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;
            }
         }
      }
   }