T4 FieldName in camelCase without Underscore?

随声附和 提交于 2019-12-21 05:41:21

问题


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.


回答1:


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.




回答2:


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




回答3:


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...



来源:https://stackoverflow.com/questions/2944974/t4-fieldname-in-camelcase-without-underscore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!