public RecognizedRelationship Invert() { var ret = new RecognizedRelationship { primary = secondary, secondary = primary, isIdentifying = isIdentifying, label = label }; switch (primarySideRelationship) { case "}o": ret.secondarySideRelationship = "o{"; break; case "|o": ret.secondarySideRelationship = "o|"; break; default: ret.secondarySideRelationship = primarySideRelationship; break; } switch (secondarySideRelationship) { case "o{": ret.primarySideRelationship = "}o"; break; case "o|": ret.primarySideRelationship = "|o"; break; default: ret.primarySideRelationship = secondarySideRelationship; break; } return(ret); }
private string GenerateRelationshipSection(RecognizedRelationship relationship) { var ret = $"\t{relationship.primary.Name.ToUpper()} {relationship.primarySideRelationship}"; if (relationship.isIdentifying) { ret += $"--"; } else { ret += $".."; } if (relationship.secondary.IsEnum) { ret += $"{relationship.secondarySideRelationship} {relationship.secondary.Name.ToUpper()}_ENUM : {relationship.label}"; } else { ret += $"{relationship.secondarySideRelationship} {relationship.secondary.Name.ToUpper()} : {relationship.label}"; } return(ret); }
private void GenerateRelationships(Type entity) { foreach (var prop in entity.GetProperties()) { if (_assemblyTypes.Any(t => t.Name.ToUpper() == prop.PropertyType.Name.ToUpper())) // Possible relationship found { var relationship = new RecognizedRelationship { primary = entity, secondary = prop.PropertyType, }; // If the property is nullable, by convention that means it's optional if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { relationship.primarySideRelationship = "|o"; relationship.label = "may have"; relationship.secondarySideRelationship = "o|"; relationship.isIdentifying = false; } else { // One to one relationship // TODO: Refine this check; need the secondary types foreign key property to match entity's ID type, for example if (prop.PropertyType.GetProperties().Any(p => p.PropertyType == entity) && prop.PropertyType.GetProperties().Any(p => p.Name.StartsWith(entity.Name))) { relationship.primarySideRelationship = "||"; relationship.label = "has"; relationship.secondarySideRelationship = "||"; relationship.isIdentifying = true; } // many to one from secondary else if (prop.PropertyType.GetProperties().Any(p => typeof(IEnumerable).IsAssignableFrom(p.PropertyType) && p.PropertyType != typeof(string) && p.PropertyType.GetEnumeratedType() == entity) ) { relationship.primarySideRelationship = "}o"; relationship.label = "has"; relationship.secondarySideRelationship = "||"; relationship.isIdentifying = true; } // optional foreign key else { // TODO: probably do some additional checking here relationship.primarySideRelationship = "||"; relationship.label = "has"; relationship.secondarySideRelationship = "o|"; relationship.isIdentifying = false; } } if (!_recognizedRelationships.RelationshipExists(relationship)) { _recognizedRelationships.Add(relationship); } } else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string) && _assemblyTypes.Any(t => t.Name == prop.PropertyType.GetEnumeratedType().Name)) // Found a one-to-many relationship by convention { // TODO: Handle arrays here var relationship = new RecognizedRelationship { primary = entity, secondary = prop.PropertyType.GetEnumeratedType(), primarySideRelationship = "||", label = "has", secondarySideRelationship = "o{" }; if (relationship.secondary.GetProperties().Any(p => p.Name.StartsWith(entity.Name)) || relationship.secondary.GetProperties().Any(p => p.PropertyType == entity)) { relationship.isIdentifying = true; } else { relationship.isIdentifying = false; } if (!_recognizedRelationships.RelationshipExists(relationship)) { _recognizedRelationships.Add(relationship); } } } }
public static bool RelationshipExists(this List <RecognizedRelationship> relationships, RecognizedRelationship relationship) { var invert = relationship.Invert(); var found = relationships.Contains(invert); return(found); }