How to: Customize Collection Generation (VB)

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic applies to VB.NET only. In order to modify collection generation in C#, please refer to How to: Customize Collection Generation (C#).

By default, when you create a new domain model all collections are generated in the following manner:

Partial Public Class Category
    Private _cars As IList(Of Car) = New List(Of Car)
    Public Overridable ReadOnly Property Cars As IList(Of Car)
        Get
            Return Me._cars
        End Get
    End Property
End Class

However, in data-binding scenarios you may need to use other types of collections.

This example demonstrates how to modify the default code generation templates so that the navigation properties are of type TrackedList<T> or TrackedBindingList<T> instead of List<T>.

TrackedList<T> and TrackedBindingList<T> can be found in the Telerik.OpenAccess namespace and are used by Telerik Data Access to monitor the changes in the content of the navigation properties.

The process includes the following steps:

  1. Copy the Templates Locally
  2. Preparation of the templates

1. Copy the Templates Locally

  • At the Code Generation Settings screen of either Add Model wizard when creating a new model or Model Settings dialog when editing an existing one, select the Custom Defined radio button and click the Copy Default To... button.

    The Copy Default To... button is part of the code generation improvements of Telerik Data Access and is available only in Visual Studio 2010 and Visual Studio 2012. Visual Studio 2008 uses the previous version of the code generation process. This article demonstrates the usage of the newer version of the process only.

  • The Select Folder dialog displays the directory structure of your project. Select an already existing folder, or create a new one to place the code generation templates in and click OK. The path to the entry template, DefaultTemplateVB.tt, will be automatically selected in the path text box. The templates will be copied to the selected directory.

  • Click Finish if you are using the wizard or OK for the Model Settings dialog.

2. Custom changes in the templates

At this stage you are ready to perform the necessary custom changes in the templates:

  • Open PropertiesGenerator.ttinclude
  • Place the following code at the end of the GenerateFieldForProperty() method:

    if (property.IsNavigationProperty && property.IsIEnumerable)
    {
        propertyType = "TrackedList(Of " + property.Type + ")";
        initialValue = "= new TrackedList(Of " + property.Type + ")";
    }
    
  • Place the following code at the end of the GenerateClassPropertySignature() method:

    if (property.IsNavigationProperty && property.IsIEnumerable)
    {
        propertyType = "TrackedList(Of " + property.Type + ")";
    }
    

For TrackedBindingList<T> you only need to changed propertyType in the above code snippets.

The outcome should be similar to this one:

Partial Public Class Category
    Private _cars As TrackedList(Of Car) = New TrackedList(Of Car)
    Public Overridable ReadOnly Property Cars As TrackedList(Of Car)
        Get
            Return Me._cars
        End Get
    End Property
End Class

In the cases when your project is tracked by a source control system, we recommend that the whole OpenAccessTemplates folder is checked-in with it. That allows the entire team to work with the hosting project without any risk for the changes in the templates or in the generated service files to be rewritten.