Sub-classing and extending Generated Class files

 

During the ORM.NET Data Layer generation process a set of classes are created which derive from the base Template classes. The purpose of these derived classes is to allow developers the ability to override the functionality provided by the generated  Data Layer template classes.  Examples of cases where you would want to override the default template classes may be for data validation or to create extended property information for a class. Unlike the base Template classes the ORM.NET derived classes are not overwritten on subsequent re-generations of the Data Layer so they can be modified.

 

The implementation of the derived object class files are very simple. The generated Student class - Student.cs - from the OleroTraining database is shown below:

 

 

/* (ORM.NET)

 * This is a one time generated class skeleton by ORM.NET.

 * Please do not remove these comments as they are required by ORM.NET to function correctly.

 */

 

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

 

namespace SampleAppBiz

{

       /// <summary>

       /// Wraps a row and it's columns/children/parents

       /// This class should be customized.

       /// </summary>

       public class Student : StudentTemplate

       {

              /// <summary>

              /// Constructor must have a row and data context.

              /// </summary>

internal Student( DataManager dataContext, DataRow ROW)

: base( dataContext, ROW)

              {

                     row = ROW;

              }

       }

}

 

 

 

 

Visual Basic.NET

 

Imports System

Imports System.Data

Imports System.Data.SqlTypes

Imports OrmLib

 

Namespace OleroTraining

 

Public Class Student : Inherits OleroTraining.StudentOrmTemplate

 

        Friend Sub New(ByVal dataContext As DataManager, ByVal dr As DataRow)

 

            MyBase.New(dataContext, dr)

            MyBase.row = dr

 

        End Sub

 

    End Class

 

    Public Class StudentCollection : Inherits OleroTraining.StudentCollectionOrmTemplate

 

    End Class

 

End Namespace

 

 

 

A simple extension to the Student class could be to create a new Property called FullName which returns both the FirstName and LastName properties formatted with a space in the middle.

 

public string FullName()

{

    return (this.FirstName + " " + this.LastName);

}

 

Once the Data Layer assembly has been recompiled you can now reference the new FullName Property as part of the base Student object within your application.

 

Console.WriteLine("Full Name: " + student.FullName);

 

 

The next example shows how to extend the LastName Property of the Student class to perform a validation check to ensure that all LastName’s are set to be lower case before committing the property to the database.

 

 

 

public override string LastName

           {

                 get

                 {

                       return (base.LastName.ToLower());

                 }

                 set

                 {

                        // ensure that the LastName is always set to

                        // lower-case

                       base.LastName = value.ToLower();

                 }    

           }

 

 

Visual Basic.NET

 

 

 Public Shadows Property LastName()

            Get

                Return MyBase.LastName.ToLower()

            End Get

            Set(ByVal Value)

                MyBase.LastName = Value.ToLower()

            End Set

 

 End Property