First, we start with the basic pluralizer:
1: public static string GetPluralOfText(string text)
2: {3: string pluralString = text;
4: string lastCharacter = pluralString.Substring(pluralString.Length - 1).ToLower();
5: 6: // y's become ies (such as Category to Categories)
7: if (string.Equals(lastCharacter, "y", StringComparison.InvariantCultureIgnoreCase))
8: { 9: pluralString = pluralString.Remove(pluralString.Length - 1);10: pluralString += "ie";
11: }12: // ch's become ches (such as Pirch to Pirches)
13: if (string.Equals(pluralString.Substring(pluralString.Length - 2), "ch", StringComparison.InvariantCultureIgnoreCase))
14: { pluralString += "e"; }
15: 16: switch (lastCharacter)
17: {18: case "s":
19: return pluralString + "es";
20: default:
21: return pluralString + "s";
22: } 23: }I also added a method for stripping out some of the class name suffixes that are prevalent in the project:
1: public static string GetTextWithoutObjectOrientedConventions(string text)
2: {3: string newText = text;
4: if (newText.Contains("Base") && string.Equals(newText.Substring(newText.Length - 4), "base", StringComparison.InvariantCultureIgnoreCase))
5: { newText = newText.Replace(newText.Substring(newText.Length - 4), string.Empty); }
6: 7: return newText;
8: }And finally, my methods that tie it all together:
1: public static string GetTablename<TClass>()
2: {3: return GetTablename(typeof (TClass));
4: } 5: 6: public static string GetTablename(Type tClass)
7: {8: string tableName = tClass.Name;
9: tableName = GetTextWithoutObjectOrientedConventions(tableName); 10: tableName = GetPluralOfText(tableName); 11: 12: return tableName;
13: }So far, it's worked out pretty well. All of my FNH conventions utilize these methods regularly. Anyhow, hope this post helps someone else out there :)
1 comments:
I like your strategy for pluralizing table names. A lot of your code could benefit from a review though, so here is some suggestions for possible improvements. I hope you find them useful.
You have:
`if (newText.Contains("Base") && string.Equals(newText.Substring(newText.Length - 4), "base", StringComparison.InvariantCultureIgnoreCase))`
... which can be reduced:
`if (newText.EndsWith("Base", StringComparison.InvariantCultureIgnoreCase))`
You also have:
`newText = newText.Replace(newText.Substring(newText.Length - 4), string.Empty);`
... which should rather be:
`newText = newText.Remove(newText.Length - 4);`
Post a Comment