Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Artificial Types
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Artificial Fields and Types > Artificial Types

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 types are types that are not present in the model itself but are defined during runtime. These new persistent types are described in XML configuration entries.

Defining artificial type:

The syntax for adding those classes is the same as for the artificial fields with the only difference that the class name should not match any class name in the given namespace. For example, if you have a class defined like this in the app.config file:

app.config Copy Code
<mappings current="mssqlMapping">
  <mapping id="mssqlMapping">
    <extension key="artificial-types" value=""/>
    <namespace name="OpenAccessData">
      <class name="Address">
        <extension key="db-table-name" value="address" />
        <field name="addressId">
          <extension key="db-column">
            <extension key="db-type" value="INTEGER" />
            <extension key="db-column-name" value="address_id" />
          </extension>
        </field>
        <field name="lcation">
          <extension key="db-column">
            <extension key="db-type" value="VARCHAR" />
            <extension key="db-column-name" value="lcation" />
            <extension key="db-length" value="255" />
          </extension>
        </field>
        <extension key="db-optimistic-locking" value="version">
          <extension key="db-column">
            <extension key="db-type" value="SMALLINT" />
            <extension key="db-column-name" value="voa_version" />
          </extension>
        </extension>
      </class>
    </namespace>
  </mapping>
</mappings>

And an artificial node defined like this:

app.config Copy Code
<artificial>
     
<mapping id="ArtificialMapping">
       
<namespace name="OpenAccessData">
         
<class name="Address">
           
<field name="country" clr="System.String"/>
         
</class>
       
</namespace>
     
</mapping>
   
</artificial>  

Then the fields of the artificial node will extend the Address class from the model. If the name of the class in the artificial mapping is not matching any of the model classes than a new artificial type will be created that will be forward mapped to the database.

Referring Artificial Types from Types defined in the model:

Since the artificial type does not have direct representation in the model but it does have one on the database you can easily create properties in the model that reference such type in the app.config file. For example if we have an artificial Person type defined as follows:

app.config Copy Code
<artificial>
     
<mapping id="ArtificialMapping">
       
<namespace name="OpenAccessData">
         
<class name="Person" identityfieldtype="System.Int32" >            
           
<field name="age" clr="System.Int32" />
           
<field name="name" clr="System.String"/>
           
<extension key="db-key-generator" value="AUTOINC" />
         
</class>          
       
</namespace>
     
</mapping>
   
</artificial>

We can easily extent the Address class which we have in the model with just adding this few lines of XML to the app.config file

app.config Copy Code
<class name="Address">            
           
<field name="Person" clr=" OpenAccessData.Person"/>
         
</class>

This will add a property of Person type to the address class. When you execute the DDL script for this change it will create a personID column that will reference the newly created person table.

Using Artificial Types:

The usage of Artificial type is similar to the use of artificial fields. To create instances of artificial types you will have to take advantage of the IPersistentTypeDescriptor interface:

C# Copy Code
IPersistentTypeDescriptor addressTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor("OpenAccessData.Address");
object addressInstance
= addressTypeDescriptor.CreateInstance(4);
IPersistentTypeDescriptor personTypeDescriptor
= scope.PersistentMetaData.GetPersistentTypeDescriptor("OpenAccessData.Person");
object personInstance
= personTypeDescriptor.CreateInstance(null);
VB.NET Copy Code
Dim addressTypeDescriptor As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor("OpenAccessData.Address")
Dim addressInstance As Object = addressTypeDescriptor.CreateInstance(4)
Dim personTypeDescriptor As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor("OpenAccessData.Person")
Dim personInstance As Object = personTypeDescriptor.CreateInstance(Nothing)

Note that the CreateInstance() method can be called with either a value representing the Key of this entry or null if an auto key generation mechanism is used.
You can set the person instance properties using the same methods as setting any other artificial field.

C# Copy Code
PropertyDescriptor personProperty = addressTypeDescriptor.GetProperties()["person"];
PropertyDescriptor personAge = personTypeDescriptor.GetProperties()
["age"];    
 scope.Transaction.Begin();
            scope.Add(addressInstance);
           scope.Add(personInstance);
            personProperty.SetValue(addressInstance,personInstance);
           personAge.SetValue(personInstance, 21);            
           scope.Transaction.Commit();
VB.NET Copy Code
Dim personProperty As PropertyDescriptor = addressTypeDescriptor.GetProperties()("person")
Dim personAge As PropertyDescriptor = personTypeDescriptor.GetProperties()("age")
  scope.Transaction.Begin()
    scope.Add(addressInstance)
   scope.Add(personInstance)
    personProperty.SetValue(addressInstance,personInstance)
   personAge.SetValue(personInstance, 21)
   scope.Transaction.Commit()
When defining artificial types you can use all the extension that are available for normal classes such as auto inc.