Collection of Property/Field Names in a Class

3 posts, 1 answers
  1. y2kdis
    y2kdis avatar
    9 posts
    Member since:
    Nov 2012

    Posted 04 Dec 2012 Link to this post


    I'm looking for a way to reference the property/field names available in class 
    So if I generated an 'Employee' class out of employee table, I would like to be able to retrieve any of the field names without directly typing it as a text.

    For example, instead of
    if grdView.Columns(i).Name = "EmployeeName" then

    I can have
    'Employee.ColumnNames.EmployeeName has a value of "EmployeeName"
    if grdView.Columns(i).Name = Employee.ColumnNames.EmployeeName then
     
    This for me reduces the chances of mistyping.

     
  2. Answer
    Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 06 Dec 2012 Link to this post

    Hi,

    In general if you want to customize your domain model classes you could change our code generation template that is generating the entities. In order to do that you could follow the steps below: 

    1. Find your OpenAccess installation folder (e.g. C:\Program Files\Telerik\OpenAccess ORM)
    2. Open dsl2008, dsl2010 or dsl2012 folder depending on the version of your Visual Studio.
    3. Go to CodeGenerationTemplates -> VisualBasic -> Includes (e.g. the final path should be something like that: "C:\Program Files\Telerik\OpenAccess ORM\dslXXXX\CodeGenerationTemplates\VisualBasic\Includes")
    4. Replace Specific.ttinclude file with the one I have prepared for you. Please find it attached.
    5. Save your rlinq file to regenerate your model.
     If you don't want to replace the whole file you could edit the existing one like below:
    1. Add this.ColumnNames(codeClass.Properties); in the GenerateClass method like this:
    ...
    if
    (!codeClass.DoubleDerived)
        {
            this.ColumnNames(codeClass.Properties); // add this row
            this.GenerateProperties(codeClass.Properties);
            ...
        }
    ...
    2. Add the new method.
    ...
    /// <summary>
    /// Generates column names.
    /// </summary>
    /// <param name="properties">The list of properties.</param>
    private void ColumnNames(System.Collections.Generic.IEnumerable<Telerik.OpenAccess.CodeGeneration.CodeProperty> properties)
    {
        this.PushIndent("\t");
        this.WriteLine("Public Class ColumnNames");
        this.PushIndent("\t");
        foreach(var property in properties)
        {
            this.WriteLine("public const " + property.Name + " As String = \"" + property.Name + "\"");
        }
         
        this.PopIndent();
        this.WriteLine("End Class");
        this.WriteLine("");
        this.PopIndent();
        this.PopIndent();
    }
    ...

    After these steps you should be able to retrieve any of your persistent classes field names like:
    Employee.ColumnNames.EmployeeName

    For your convenience I have also attached a sample application that is using the Northwind database demonstrating this approach.

    You could find more information about best practices for customizing the code generation templates in this help article. Also here you could find how to modify the code generation templates used by OpenAccess Visual Designer, in order to implement automatically INotifyPropertyChanging/ed interfaces for VB persistent classes.

    I hope this helps. Let me know if you need additional directions/assistance.

    All the best,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
  4. y2kdis
    y2kdis avatar
    9 posts
    Member since:
    Nov 2012

    Posted 10 Dec 2012 Link to this post

    Thank you very much!

    I've also been hand-coding partial classes based on the generated classes so the sample and link you provided will help me a lot in understanding how to work with the code-generation templates.
Back to Top