开发者

T4 FieldName in camelCase without Underscore?

I'm using T4 to generate some class definitions and find that I'm getting an underscore in front of my field names.

I have set

code.CamelCaseFields = true;

just to be safe (even though I understand that's the default) but still end up with _myField rather than myField.

How can I generate a field name without the '_' character?

Also, where is the documentation for T4? I'm开发者_如何学编程 finding plenty of resources such as

Code Generation and Text Templates and numerous blogs, but I have not found the class-by-class, property-by-property documentation.


You're probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class is included via the <#@ include file="EF.Utility.CS.ttinclude"#> directive, which you can find at "[VSInstallDir]\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude".

The FieldName function is defined as such:

private string FieldName(string name)
{
  if (CamelCaseFields)
  {
    return "_" + CamelCase(name);
  }
  else
  {
    return "_" + name;
  }
}

The "_" is hardcoded in the function. Coding your own shouldn't be difficult. Note that the CodeGenerationTools class is specific to this ttinclude file and isn't a generic and embedded way to generate code in T4.


I've written the following method to make first character upper case, remove spaces/underscores and make next character upper case. See samples below. Feel free to use.

private string CodeName(string name)
{
    name = name.ToLowerInvariant();

    string result = name;
    bool upperCase = false;

    result = string.Empty;
    for (int i = 0; i < name.Length; i++)
    {
        if (name[i] == ' ' || name[i] == '_')
        {
            upperCase = true;
        }
        else
        {
            if (i == 0 || upperCase)
            {
                result += name[i].ToString().ToUpperInvariant();
                upperCase = false;
            }
            else
            {
                result += name[i];
            }
        }
    }

    return result;
}

input/output samples: first_name = FirstName, id = Id, status message = StatusMessage


This is good advice however it doesn't help you in knowing WHERE the right place to put such a function is...

Is there any guidance on DECOMPOSING the EF .tt files or stepping through the output generation to see how it builds the output?

I was able to use the above function successfully by plugging it into a function called (Ef4.3)

public string Property(EdmProperty edmProperty)

Which appears to be used to output the lines like "public int fieldname { get; set; }"

and changed the 3rd (index {2}) param to the formating to wrap with the function to modify the name, like this:

_typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged
UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name"
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged

This is not perfect, eg: it doesn't keep existing "Ucasing" and doesn't care about things like this: customerIP outputs: Customerip which IMO is not very readable...

but its better than what I WAS looking at which was a nightmare because the database was intermingled mess of camelCase, PascalCase and underscore separation, so pretty horrific.

anyway hope this helps someone...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜