Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Work with Objects in an Inheritance Hierarchy
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Working with Objects > How to: Work with Objects in an Inheritance Hierarchy

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

Inheritance is used in OpenAccess as it is in object-oriented programming. Also it does not depend on the type of the model mapping (flat, vertical, etc.) except one case - when horizontal mapping is used. Then instances of the base class cannot be persisted as there is no table for that type. Working with objects in a hierarchy does not differ from working with regular persistence-capable objects.

 

To illustrate the functionality of retrieving and working with such objects, the following class hierarchy will be used:

Creating and persisting objects

The console application below demonstrates creating, updating and storing persistence-capable objects:

C# Copy Code
class Program
{
  
private static IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
  
static void InsertPets(Pet p)
  {
      
//add some pets
      
scope.Add(new Pet() { Name = "myPet" });
      scope.Add(
new Cat() { Name = "myCat" });
      scope.Add(
new Rottweiler() { Name = "myRottweiler" });
      scope.Add(
new WienerDog() { Name = "myWiener" });
      
//add the passed as parameter pet
      
scope.Add(p);
      scope.Transaction.Commit();
  }
  
static void Main(string[] args)
  {
      scope.TransactionProperties.AutomaticBegin = true;
      Dog d =
new Dog() { Name = "myDog" };
      InsertPets(d);
      var result = scope.GetOqlQuery(
"select * from PetExtent").Execute();
      
foreach(Pet pet in result)
      {
          Console.WriteLine(pet.Name +
" - " + pet.GetType().ToString());
      }
      d.Name =
"Tom";
      scope.Transaction.Commit();
  }
}
VB .NET Copy Code
Module Module1
    Private scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope()
    Sub InsertPets(ByVal p As Pet)
        'add some pets
        scope.Add(New Pet() With {.Name = "myPet"})
        scope.Add(New Cat() With {.Name = "myCat"})
        scope.Add(New Rottweiler() With {.Name = "myRottweiler"})
        scope.Add(New WienerDog() With {.Name = "myWiener"})
        'add the passed as parameter pet
        scope.Add(p)
        scope.Transaction.Commit()
    End Sub
    Sub Main(ByVal args As String())
        scope.TransactionProperties.AutomaticBegin = True
        Dim d As New Dog() With {.Name = "myDog"}
        InsertPets(d)
        Dim result = scope.GetOqlQuery("select * from PetExtent").Execute()
        For Each pet As Pet In result
            Console.WriteLine(pet.Name + " - " + pet.[GetType]().ToString())
        Next
        d.Name = "Tom"
        scope.Transaction.Commit()
    End Sub
End Module

Retrieving objects from the base class

 Using Linq query:

C#

Copy Code
IQueryable<Pet> pets = from p in scope.Extent<Pet>()
                      select p;
VB .NET Copy Code
Dim pets As IQueryable(Of Pet) = From p In scope.Extent(Of Pet)() _
                                 Select p

Using OQL query:

C# Copy Code
QueryResultList<Pet> pets = scope.GetOqlQuery<Pet>().ExecuteList();
VB .NET Copy Code
Dim pets As QueryResultList(Of Pet) = scope.GetOqlQuery(Of Pet)().ExecuteList()

The resulting collections contain all Dogs and Cats in the scope as they are also Pets.

 

Retrieving objects from specific child type

Using Linq query:

C# Copy Code
IQueryable<Rottweiler> evilDogs = from r in scope.Extent<Rottweiler>()
                                 select r;
VB .NET Copy Code
Dim evilDogs As IQueryable(Of Rottweiler) = From r In scope.Extent(Of Rottweiler)() _
                                            Select r

This Linq query will collect all Rottweiler dogs. The corresponding OQL query can be found below.

C# Copy Code
QueryResultList<Rottweiler> evilDogs = scope.GetOqlQuery<Rottweiler>().ExecuteList();
VB .NET Copy Code
Dim evilDogs As QueryResultList(Of Rottweiler) = scope.GetOqlQuery(Of Rottweiler)().ExecuteList()