private static string[] GetForeignKeys(AssociationEndModel associationEnd)
 {
     return(associationEnd.Element.GetStereotypeProperty("Foreign Key", "Column Name", associationEnd.OtherEnd().Name().ToPascalCase() + "Id")
            .Split(',')
            .Select(x => x.Trim())
            .ToArray());
 }
        private static string GetForeignKeyLambda(AssociationEndModel associationEnd)
        {
            var foreignKeys = GetForeignKeys(associationEnd);

            if (foreignKeys.Length == 1)
            {
                return($"x => x.{foreignKeys.Single()}");
            }
            return($"x => new {{ {string.Join(", ", foreignKeys.Select(x => "x." + x))}}}");
        }
Esempio n. 3
0
        public static string Name(this AssociationEndModel associationEnd)
        {
            if (string.IsNullOrEmpty(associationEnd.Name))
            {
                var className = associationEnd.Class.Name;
                return(associationEnd.IsCollection ? className.ToPluralName() : className);
            }

            return(associationEnd.Name);
        }
        public override string BeforeField(AssociationEndModel thatEnd)
        {
            if (!thatEnd.IsNavigable)
            {
                throw new InvalidOperationException("Cannot call this method if associationEnd is not navigable.");
            }
            var annotations = new List <string>();
            var sourceEnd   = thatEnd.OtherEnd();

            if (!sourceEnd.IsCollection && !thatEnd.IsCollection) // one-to-one
            {
                annotations.Add($"@{_template.ImportType("javax.persistence.OneToOne")}(optional = {thatEnd.IsNullable.ToString().ToLower()}{(sourceEnd.IsNullable ? "" : ", orphanRemoval = true")})");
                if (thatEnd.IsTargetEnd())
                {
                    annotations.Add($"@{_template.ImportType("javax.persistence.JoinColumn")}(name=\"{thatEnd.Name.ToSnakeCase()}_id\", nullable = {thatEnd.IsNullable.ToString().ToLower()})");
                }
            }
            else if (!sourceEnd.IsCollection && thatEnd.IsCollection) // one-to-many
            {
                var settings = new List <string>();
                if (sourceEnd.IsNavigable)
                {
                    settings.Add($"mappedBy=\"{sourceEnd.Name.ToCamelCase()}\"");
                }

                if (!sourceEnd.IsNullable)
                {
                    settings.Add("orphanRemoval = true");
                }
                annotations.Add($"@{_template.ImportType("javax.persistence.OneToMany")}({string.Join(", ", settings)})");
            }
            else if (sourceEnd.IsCollection && !thatEnd.IsCollection) // many-to-one
            {
                annotations.Add($"@{_template.ImportType("javax.persistence.ManyToOne")}(optional = {thatEnd.IsNullable.ToString().ToLower()})");
                //if (thatEnd.IsTargetEnd())
                //{
                annotations.Add($"@{_template.ImportType("javax.persistence.JoinColumn")}(name=\"{thatEnd.Name.ToSnakeCase()}_id\", nullable = {thatEnd.IsNullable.ToString().ToLower()})");
                //}
            }
            else if (sourceEnd.IsCollection && thatEnd.IsCollection) // many-to-many
            {
                annotations.Add($"@{_template.ImportType("javax.persistence.ManyToMany")}");
                if (thatEnd.IsTargetEnd())
                {
                    annotations.Add($@"@{_template.ImportType("javax.persistence.JoinTable")}(
            name = ""{sourceEnd.Element.Name.ToSnakeCase()}_{thatEnd.Element.Name.ToPluralName().ToSnakeCase()}"",
            joinColumns = {{ @{_template.ImportType("javax.persistence.JoinColumn")}(name = ""{sourceEnd.Element.Name.ToSnakeCase()}_id"") }},
            inverseJoinColumns = {{ @{_template.ImportType("javax.persistence.JoinColumn")}(name = ""{thatEnd.Element.Name.ToSnakeCase()}_id"") }}
    )");
                }
            }

            return(string.Join(@"
    ", annotations));
        }
 public static string MultiplicityString(this AssociationEndModel associationEnd)
 {
     if (associationEnd.IsCollection)
     {
         return(associationEnd.IsNullable ? "0..*" : "1..*");
     }
     else
     {
         return(associationEnd.IsNullable ? "0..1" : "1");
     }
 }
 public static string IdentifierName(this AssociationEndModel associationEnd)
 {
     if (string.IsNullOrEmpty(associationEnd.Name))
     {
         return(associationEnd.Class.IdentifierType());
     }
     else
     {
         return(associationEnd.Name.ToPascalCase() + "Id");
     }
 }
Esempio n. 7
0
 public string DeterminePrinciple(AssociationEndModel associationEnd)
 {
     if (associationEnd.Association.AssociationType == AssociationType.Composition)
     {
         return("Principal");
     }
     if (associationEnd.Association.AssociationType == AssociationType.Aggregation)
     {
         return("Dependent");
     }
     return("");
 }
        public override string AssociationAfter(AssociationEndModel associationEnd)
        {
            if (!associationEnd.IsNavigable)
            {
                return(base.AssociationAfter(associationEnd));
            }

            var name = Template.Types.InContext(DomainEntityStateTemplate.InterfaceContext).Get(associationEnd).Name;

            return($@"
        {Template.NormalizeNamespace(name)} I{associationEnd.OtherEnd().Class.Name}.{associationEnd.Name().ToPascalCase()} => {associationEnd.Name().ToPascalCase()};
");
        }
Esempio n. 9
0
        public override string AssociationBefore(AssociationEndModel associationEnd)
        {
            if (!associationEnd.IsNavigable && associationEnd.Multiplicity == Multiplicity.One && associationEnd.OtherEnd().Multiplicity == Multiplicity.Many)
            {
                return($@"       public virtual { Template.GetTypeName(associationEnd) } { associationEnd.Name() } {{ get; set; }}
");
            }
            if (!associationEnd.IsNavigable && associationEnd.Multiplicity == Multiplicity.Many && associationEnd.OtherEnd().Multiplicity == Multiplicity.Many)
            {
                return($@"       public virtual { Template.GetTypeName(associationEnd) } { associationEnd.Name() } {{ get; set; }}
");
            }
            return(base.AssociationBefore(associationEnd));
        }
Esempio n. 10
0
        public static string Name(this AssociationEndModel associationEnd)
        {
            //if (string.IsNullOrEmpty(associationEnd.Name))
            //{
            //    var className = associationEnd.Class.Name;
            //    if (associationEnd.MaxMultiplicity == "*" || int.Parse(associationEnd.MaxMultiplicity) > 1)
            //    {
            //        return className.EndsWith("y") ? className.Substring(0, className.Length - 1) + "ies" : string.Format("{0}s", className);
            //    }
            //    return associationEnd.Class.Name;
            //}

            return(associationEnd.Name);
        }
Esempio n. 11
0
        public override string PropertyBefore(AssociationEndModel associationEnd)
        {
            if (associationEnd.RequiresForeignKey())
            {
                if (associationEnd.OtherEnd().Element.HasStereotype("Foreign Key"))
                {
                    return(base.PropertyBefore(associationEnd));
                }

                var foreignKeyType = associationEnd.Class.GetSurrogateKeyType(Template.Types) ?? Template.UseType(_foreignKeyType);
                return($@"
{foreignKeyType}{ (associationEnd.IsNullable ? "?" : "") } { associationEnd.Name().ToPascalCase() }Id {{ get; }}");
            }
            return(base.PropertyBefore(associationEnd));
        }
        private static string GetForeignKeyLambda(AssociationEndModel associationEnd)
        {
            var columns = ((associationEnd.IsSourceEnd()
                               ? (associationEnd as AssociationSourceEndModel).GetForeignKey()?.ColumnName()
                               : (associationEnd as AssociationTargetEndModel).GetForeignKey()?.ColumnName())
                           ?? associationEnd.OtherEnd().Name.ToPascalCase() + "Id")
                .Split(',') // upgrade stereotype to have a selection list.
                .Select(x => x.Trim())
                .ToList();

            if (columns.Count == 1)
            {
                return $"x => x.{columns.Single()}";
            }

            return $"x => new {{ {string.Join(", ", columns.Select(x => "x." + x))}}}";
        }
Esempio n. 13
0
        public static RelationshipType Relationship(this AssociationEndModel associationEnd)
        {
            if (!associationEnd.IsCollection && !associationEnd.OtherEnd().IsCollection)
            {
                return(RelationshipType.OneToOne);
            }
            if (!associationEnd.IsCollection && associationEnd.OtherEnd().IsCollection)
            {
                return(RelationshipType.OneToMany);
            }
            if (associationEnd.IsCollection && !associationEnd.OtherEnd().IsCollection)
            {
                return(RelationshipType.ManyToOne);
            }
            if (associationEnd.IsCollection && associationEnd.OtherEnd().IsCollection)
            {
                return(RelationshipType.ManyToMany);
            }

            throw new Exception($"The relationship type from [{associationEnd.Element.Name}] to [{associationEnd.OtherEnd().Element.Name}] could not be determined.");
        }
        public static RelationshipType Relationship(this AssociationEndModel associationEnd)
        {
            if ((associationEnd.Multiplicity == Multiplicity.One || associationEnd.Multiplicity == Multiplicity.ZeroToOne) && (associationEnd.OtherEnd().Multiplicity == Multiplicity.One || associationEnd.OtherEnd().Multiplicity == Multiplicity.ZeroToOne))
            {
                return(RelationshipType.OneToOne);
            }
            if ((associationEnd.Multiplicity == Multiplicity.One || associationEnd.Multiplicity == Multiplicity.ZeroToOne) && associationEnd.OtherEnd().Multiplicity == Multiplicity.Many)
            {
                return(RelationshipType.OneToMany);
            }
            if (associationEnd.Multiplicity == Multiplicity.Many && (associationEnd.OtherEnd().Multiplicity == Multiplicity.One || associationEnd.OtherEnd().Multiplicity == Multiplicity.ZeroToOne))
            {
                return(RelationshipType.ManyToOne);
            }
            if (associationEnd.Multiplicity == Multiplicity.Many && associationEnd.OtherEnd().Multiplicity == Multiplicity.Many)
            {
                return(RelationshipType.ManyToMany);
            }

            throw new Exception($"The relationship type from [{associationEnd.Class.Name}] to [{associationEnd.OtherEnd().Class.Name}] could not be determined.");
        }
Esempio n. 15
0
 public virtual bool CanWriteDefaultAssociation(AssociationEndModel association)
 {
     return(true);
 }
Esempio n. 16
0
 public virtual string PropertyAnnotations(AssociationEndModel associationEnd)
 {
     return(null);
 }
Esempio n. 17
0
 public virtual string AssociationAccessors(AssociationEndModel associationEnd)
 {
     return(null);
 }
 public bool CanWriteDefaultAssociation(AssociationEndModel association)
 {
     return(GetDecorators().All(x => x.CanWriteDefaultAssociation(association)));
 }
Esempio n. 19
0
 public virtual string PropertyBefore(AssociationEndModel associationEnd)
 {
     return(null);
 }
 public string PropertySetterAfter(AssociationEndModel associationEnd)
 {
     return(GetDecorators().Aggregate(x => x.PropertySetterAfter(associationEnd)));
 }
 public string AssociationAfter(AssociationEndModel associationEnd)
 {
     return(GetDecorators().Aggregate(x => x.AssociationAfter(associationEnd)));
 }
        private string GetAssociationMapping(AssociationEndModel associationEnd)
        {
            var statements = new List<string>();

            switch (associationEnd.Association.GetRelationshipType())
            {
                case RelationshipType.OneToOne:
                    statements.Add($"builder.HasOne(x => x.{associationEnd.Name.ToPascalCase()})");
                    statements.Add($".WithOne({ (associationEnd.OtherEnd().IsNavigable ? $"x => x.{associationEnd.OtherEnd().Name.ToPascalCase()}" : "") })");
                    statements.Add($".HasForeignKey<{ Model.Name }>({GetForeignKeyLambda(associationEnd.OtherEnd())})");
                    if (!associationEnd.OtherEnd().IsNullable)
                    {
                        statements.Add($".IsRequired()");
                        statements.Add($".OnDelete(DeleteBehavior.Cascade)");
                    }
                    else
                    {
                        statements.Add($".OnDelete(DeleteBehavior.Restrict)");
                    }

                    break;
                case RelationshipType.ManyToOne:
                    statements.Add($"builder.HasOne(x => x.{associationEnd.Name.ToPascalCase()})");
                    statements.Add($".WithMany({ (associationEnd.OtherEnd().IsNavigable ? "x => x." + associationEnd.OtherEnd().Name.ToPascalCase() : "") })");
                    statements.Add($".HasForeignKey({GetForeignKeyLambda(associationEnd.OtherEnd())})");
                    statements.Add($".OnDelete(DeleteBehavior.Restrict)");
                    break;
                case RelationshipType.OneToMany:
                    statements.Add($"builder.HasMany(x => x.{associationEnd.Name.ToPascalCase()})");
                    statements.Add($".WithOne(x => x.{ associationEnd.OtherEnd().Name.ToPascalCase() })");
                    statements.Add($".HasForeignKey({GetForeignKeyLambda(associationEnd)})");
                    if (!associationEnd.OtherEnd().IsNullable)
                    {
                        statements.Add($".IsRequired()");
                        statements.Add($".OnDelete(DeleteBehavior.Cascade)");
                    }

                    break;
                case RelationshipType.ManyToMany:
                    if (Project.GetProject().IsNetCore2App || Project.GetProject().IsNetCore3App)
                    {
                        statements.Add($"builder.Ignore(x => x.{associationEnd.Name.ToPascalCase()})");

                        Logging.Log.Warning($@"Intent.EntityFrameworkCore: Cannot create mapping relationship from {Model.Name} to {associationEnd.Class.Name}. It has been ignored, and will not be persisted.
    Many-to-Many relationships are not yet supported by EntityFrameworkCore as yet.
    Either upgrade your solution to .NET 5.0 or create a joining-table entity (e.g. [{Model.Name}] 1 --> * [{Model.Name}{associationEnd.Class.Name}] * --> 1 [{associationEnd.Class.Name}])
    For more information, please see https://github.com/aspnet/EntityFrameworkCore/issues/1368");
                    }
                    else // if .NET 5.0 or above...
                    {
                        statements.Add($"builder.HasMany(x => x.{associationEnd.Name.ToPascalCase()})");
                        statements.Add($".WithMany(x => x.{associationEnd.OtherEnd().Name.ToPascalCase()})");
                        statements.Add($".UsingEntity(x => x.ToTable(\"{associationEnd.OtherEnd().Class.Name}{associationEnd.Class.Name.ToPluralName()}\"))");
                    }
                    break;
                default:
                    throw new Exception($"Relationship type for association [{Model.Name}.{associationEnd.Name}] could not be determined.");
            }
            return $@"
            {string.Join(@"
                ", statements)};";
        }
 public string PropertyAnnotations(AssociationEndModel associationEnd)
 {
     return(GetDecorators().Aggregate(x => x.PropertyAnnotations(associationEnd)));
 }
 private static bool IsManyToVariantsOfOne(AssociationEndModel associationEnd)
 {
     return((associationEnd.Multiplicity == Multiplicity.One || associationEnd.Multiplicity == Multiplicity.ZeroToOne) &&
            associationEnd.OtherEnd().Multiplicity == Multiplicity.Many);
 }
 public override string PropertyAnnotations(AssociationEndModel associationEnd)
 {
     return("        [DataMember]");
 }
 private static bool IsSelfReferencingZeroToOne(AssociationEndModel associationEnd)
 {
     return(associationEnd.Multiplicity == Multiplicity.ZeroToOne && associationEnd.Association.TargetEnd.Class == associationEnd.Association.SourceEnd.Class);
 }
 public static bool RequiresForeignKey(this AssociationEndModel associationEnd)
 {
     return(IsManyToVariantsOfOne(associationEnd) || IsSelfReferencingZeroToOne(associationEnd));
 }
 public virtual string BeforeField(AssociationEndModel model) => null;
Esempio n. 29
0
 public virtual string PropertySetterAfter(AssociationEndModel associationEnd)
 {
     return(null);
 }
Esempio n. 30
0
 public virtual string AssociationAfter(AssociationEndModel associationEnd)
 {
     return(null);
 }