public DbNaming(AffixPlacing idPlacing, Case namingCase, string idText = null) { TextSource = Source.Default; Scope = Translation.Reflection; IdPlacing = StoredProcedureAffix = idPlacing; ParametersCase = StoredProceduresCase = FieldsCase = TablesCase = namingCase; IdText = idText; }
/// <summary> /// Translates a name. /// </summary> public static string TranslateName(Source source, Translation scope, string reflectionName, string configurationName, Case namingCase, bool forceId, AffixPlacing idPlacing, params string[] affixes) { string name; switch (source) { case Source.Default: default: if (!string.IsNullOrEmpty(configurationName)) { source = Source.Configuration; name = configurationName; } else { source = Source.Reflection; name = reflectionName; } break; case Source.Configuration: name = configurationName; break; case Source.Reflection: name = reflectionName; break; } if (scope == Translation.All || scope == Translation.Configuration && source == Source.Configuration || scope == Translation.Reflection && source == Source.Reflection) { name = Name.Format(name, namingCase, idPlacing, forceId, affixes); } return(name); }
/// <summary> /// Translates a compound name. /// </summary> protected virtual string TranslateCompoundName(Source source, Translation scope, string reflectionName, string configurationName, Case namingCase, bool forceId, AffixPlacing idPlacing, params string[] affixes) { var compoundName = TranslateName(source, Translation.None, reflectionName, configurationName, default(Case), default(bool), default(AffixPlacing)); var parts = SplitParts(compoundName); return(JoinParts(parts.Select(name => TranslateName(source, scope, name, name, namingCase, forceId, idPlacing, affixes)))); }
/// <summary> /// Translates a name. /// </summary> protected virtual string TranslateName(Source source, Translation scope, string reflectionName, string configurationName, Case namingCase, DbKeyConstraint constraint, AffixPlacing idPlacing, params string[] affixes) { bool forceId; switch (constraint) { case DbKeyConstraint.PrimaryKey: case DbKeyConstraint.PrimaryForeignKey: case DbKeyConstraint.ForeignKey: forceId = scope != Translation.None && ForceIdOnKeyColumn; break; default: forceId = false; break; } return(TranslateName(source, scope, reflectionName, configurationName, namingCase, forceId, idPlacing, affixes)); }
/// <summary> /// Formats a name. /// </summary> public static string Format(string name, Case namingCase, AffixPlacing affixPlacing, bool forceId, params string[] affixes) { var words = SplitInWords(name, namingCase); var count = words.Length; var startIndex = 0; if (count > 0 && affixPlacing != AffixPlacing.DoNotChange && affixes?.Length > 0) { var i = IndexOf(affixes, words[0]); var j = count > 1 ? IndexOf(affixes, words[count - 1]) : -1; switch (affixPlacing) { case AffixPlacing.DoNotPlace: var remainingCount = count; if (i >= 0 && remainingCount > 1) { words[0] = string.Empty; remainingCount--; } if (j >= 0 && remainingCount > 1) { words[count - 1] = string.Empty; } break; case AffixPlacing.Prefix: if (i < 0) { if (j >= 0) { startIndex = count - 1; words[startIndex] = affixes[j]; } else if (forceId) { Array.Resize(ref words, ++count); words[startIndex = count - 1] = affixes[0]; } } break; case AffixPlacing.Sufix: if (j < 0) { if (i >= 0) { startIndex = 1; words[0] = affixes[i]; } else if (forceId) { Array.Resize(ref words, ++count); words[count - 1] = affixes[0]; } } break; case AffixPlacing.Whole: if (i >= 0) { return(affixes[i]); } ; if (j >= 0) { return(affixes[j]); } if (forceId) { return(affixes[0]); } break; } } var separator = GetSeparator(namingCase); var result = new StringBuilder(); var first = true; for (var index = 0; index < count; index++) { var word = words[(index + startIndex) % count]; if (!string.IsNullOrEmpty(word)) { if (!first) { result.Append(separator); } result.Append(word); first = false; } } return(result.ToString()); }