Telerik blogs

The good thing about the code generation is that you can easily modify it to suite your needs. Today I will show you how to modify the code generation template so that it generates string properties that HtmlEncode and HtmlDecode the values in their respective setter and getter. The desired outcome will look like : 

private string firstName;
 
public virtual string FirstName 
    get
    {
        return HttpContext.Current.Server.HtmlDecode(this.firstName);
    }
    set
    {
        this.firstName = HttpContext.Current.Server.HtmlEncode(value);
    }
}


You can achieve this in a couple of steps.

First we need to locate the T4 templates that OpenAccess uses. The C# templates are usually located under C:\Program Files (x86)\Telerik\OpenAccess ORM\dsl\CodeGenerationTemplates\CSharp. Note that it would be better to copy this folder and not modify the default templates. You can specify which template the code generation is using through the model settings dialog.

What we need to modify is the Specific.ttinclude file. Then you should locate the InitializeDefaultUsings() method and add the System.Web using to the default usings. Your code should look like : 

private void InitializeDefaultUsings()
{
    if (Usings.Count < 1)
    {
        Usings.Add("System");
        Usings.Add("System.Data");
        Usings.Add("System.Linq");
        Usings.Add("System.Linq.Expressions");
        Usings.Add("System.Data.Common");      
        Usings.Add("System.Collections.Generic");
        Usings.Add("System.Web"); // the added using
        Usings.Add("Telerik.OpenAccess");
    }
}

Then you have to find the code for adding a property. It should look like this prior to any changes : 

private <#= this.GetTypeStringPresentation(property) #> <#= property.FieldName #><#= initialValue #>;
 
<#+ GenerateCustomAttributes(property.Attributes); #>
public virtual <#= this.GetTypeStringPresentation(property) #> <#= property.Name #> 
<#+
    if(property.HasGetter)
    {
#>
    get
    {
        return this.<#= property.FieldName #>;
    }
<#+
    }
    if(property.HasSetter)
    {
#>
    set
    {
        this.<#= property.FieldName #> = value;
    }
<#+
    }
#>
}

In order to make it generate code that encodes and decodes these properties you should change it to look like this :

private <#= this.GetTypeStringPresentation(property) #> <#= property.FieldName #><#= initialValue #>;
 
<#+ GenerateCustomAttributes(property.Attributes); #>
public virtual <#= this.GetTypeStringPresentation(property) #> <#= property.Name #> 
<#+
    if(property.HasGetter)
    {
#>
    get
    {
<#+ 
        string type = this.GetTypeStringPresentation(property);
        if(type == "String" || type == "string")
        {
#>
        return HttpContext.Current.Server.HtmlDecode(this.<#= property.FieldName #>);
<#+ 
        }
        else
        {
#>
        return this.<#= property.FieldName #>;
<#+ 
        
#>
    }
<#+
    }
    if(property.HasSetter)
    {
#>
    set
    {
<#+ 
        string type = this.GetTypeStringPresentation(property);
        if(type == "String" || type == "string")
        {
#>
        this.<#= property.FieldName #> = HttpContext.Current.Server.HtmlEncode(value);
<#+ 
        }
        else
        {
#>
        this.<#= property.FieldName #> = value;
<#+ 
        
#>
    }
<#+
    }
#>
}


In two easy steps we have created a custom template that web developers could find very handy. Just imagine the possibilities . One can easily implement interfaces such as the INotifyPropertyChanged for example or add data annotations attributes.

We would really like to hear from you. Please share any neat ideas you might have about custom code generation that you would like to see in the product.









Comments

Comments are disabled in preview mode.