I have a modified T4 template that builds classes from my edmx and its working smoothly except for derived classes.
Product : BaseItem // works fine as do all top level classes
TranslatedProduct : Product : BaseItem // dang
I’m confused about how and where I can conditionally set the T4 template to ignore : BaseItem in the case of a derived class – ie
TranslatedProduct : Product
For example:
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#> : BaseItem
In my head i imagined it like –
if(code.Escape(entity.BaseType).Equals(string.empty)
{
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#> : BaseItem
}
else
{
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
}
But I receive syntax errors so I’d like to see if anyone else has tried this and if I’m on the right path
The scripts you provided hard-code
: BaseItemto always appear. This seems broken.The original code is:
This uses a class defined in:
The portions of the script between
<#= #>tags are just C# expressions, and the strings returned by those expressions are inserted inline.The
code.Escapemethod will return either the type name (as a string) or an empty string.The
code.StringBeforewill append the first string (" : ") before the second string (the base type name), but only if the second string isn’t null or empty.To do what you’re trying to accomplish, you can use the same trick they use, but in reverse. Unfortunately you can’t use their existing class, because they don’t have some sort of
AppendIfNotDefinedmethod. So we’ll just use a more complicated expression.Instead of:
We’ll write:
Here’s the whole line, all crammed together: