subsonic / SubSonic-3.0-Templates

T4 Template Project for the peeps
http://subsonic.github.io/
105 stars 46 forks source link

Improving CleanUp method #24

Open jamesewelch opened 15 years ago

jamesewelch commented 15 years ago

I really liked how SubSonic 2.x would rename my database objects to fit into normal coding naming conventions. However, 3.x doesn't do that. I've coded the below update to CleanUp to emulate how 2.x worked. It could probably use a bit of cleanup, but the gist of it is there.

This uses Pascal case to capitalize the object names based on a set of delimiters (just underscore, dash, and space in example below). It capitalizes the first part of each "part" of the object name and the first letter of the name.

inputs: my_table, my_field_name, my-table, MY-TABLE output: MyTable, MyFieldName, MyTable, MyTable

string CleanUp(string tableName)
{
     string result = string.Empty;
     char[] delims = new char[]{ '_', '-', ' '};
     string[] parts = tableName.Split(delims);
     foreach(string part in parts)
     {
          string firstLetter = part.Substring(0,1).ToUpper();
          result += firstLetter + part.Substring(1, part.Length-1).ToLower();
     }
     return result;
}