This is a migrated thread and some comments may be shown as answers.

Collection of Property/Field Names in a Class

2 Answers 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
y2kdis
Top achievements
Rank 1
y2kdis asked on 04 Dec 2012, 06:44 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar Tachev
Telerik team
answered on 06 Dec 2012, 02:27 PM
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.
0
y2kdis
Top achievements
Rank 1
answered on 10 Dec 2012, 09:20 PM
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.
Tags
General Discussions
Asked by
y2kdis
Top achievements
Rank 1
Answers by
Dimitar Tachev
Telerik team
y2kdis
Top achievements
Rank 1
Share this question
or