Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Use Artificial Fields
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Working with Objects > How to: Use Artificial Fields

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.

Artificial fields are fields that extend the structure of a persistent class by adding new persistent fields during runtime. These new fields are described in XML configuration entries. They are accessible via the Generic Metadata Access which is based on the
System.ComponentModel API and adds OpenAccess specific functionality.

Imagine that you are entering data in your database and you suddenly decide that you need an extra field. Adding the field dynamically is available now by just using simple xml entries in the app.config.

This is an example how the definition should look like in the app.config:

app.config Copy Code
<artificial>
     
<mapping id="Artificialmapping">
       
<namespace name="ComplexModel">
         
<class name="Person">
           
<field name="age" clr="System.Int32" />
         
</class>
         
<class name="Player">
           
<field name="girlFriend" clr="System.String" />
           
<field name="isChampion" clr="System.Boolean" />
           
<field name="bestFriend" clr="ComplexModel.Player" />
           
<field name="dateOfBirth" clr="System.DateTime" />            
         
</class>
         
<class name="FootballPlayer">
           
<field name="goalsScored" clr="System.Int16" />
           
<field name="goalsConceded" clr="System.Nullable`1[System.Int32]" />
           
<field name="goalsPerGame" clr="System.Decimal" />
           
<field name="initialLetter" clr="System.Char" />
         
</class>
       
</namespace>
     
</mapping>
   
</artificial>

The definition above will add four fields to the Player class and four to the FootballPlayer. Note that we need to specify a unique mapping id for the artificial fields node. Further we need the namespace and the class name so that we can provide the exact path to the class. This code can be added dynamically to the app.config file using a simple xml writer.


After creating the fields we need a way to access them. This can be done using the Generic Access API that handles such cases.

For example in the class Person we need to access the field 'age'; we will need to use a persistent type descriptor like:

C# Copy Code
IPersistentTypeDescriptor ptd = scope.PersistentMetaData.GetPersistentTypeDescriptor(typeof(Player));
           PropertyDescriptor age = ptd.GetProperties()
["age"];
VB.NET Copy Code
Dim ptd As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor(GetType(Player))
   Dim age As PropertyDescriptor = ptd.GetProperties()("age")

First we need a variable of type IPersistentTypeDescriptor that will hold the properties of our class (both the original fields and the artificial fields). We can initialize that variable with the GetPersistentTypeDescriptor method. To access a single persistent field we need to have a variable of type PropertyDescriptor that will hold the artificial field descriptor. Now if you like to read or write values you can use normal .NET property descriptor functionality even if you access the field directly with it.

Reading:

C# Copy Code
IPersistentTypeDescriptor ptd = scope.PersistentMetaData.GetPersistentTypeDescriptor(typeof(FootballPlayer));
           PropertyDescriptor isChampion = ptd.GetProperties()
["isChampion"];
FootballPlayer player =
new FootballPlayer();            
isChampion.GetValue(player);

 

VB.NET Copy Code
Dim ptd As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor(GetType(FootballPlayer))
   Dim isChampion As PropertyDescriptor = ptd.GetProperties()("isChampion")
Dim player As New FootballPlayer()
isChampion.GetValue(player)

Writing:

C# Copy Code
IPersistentTypeDescriptor ptd = scope.PersistentMetaData.GetPersistentTypeDescriptor(typeof(FootballPlayer));
           PropertyDescriptor isChampion = ptd.GetProperties()
["isChampion"];
FootballPlayer player =
new FootballPlayer();            
isChampion.SetValue(player, true);
VB.NET Copy Code
Dim ptd As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor(GetType(FootballPlayer))
   Dim isChampion As PropertyDescriptor = ptd.GetProperties()("isChampion")
Dim player As New FootballPlayer()
isChampion.SetValue(player, True)