Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Working with IPersistentTypeDescriptor
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Generic Access to Persistent MetaData > Working with IPersistentTypeDescriptor

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.

The IPersistentTypeDescriptor interface implements the System.ComponentModel.ICustomTypeDescriptor and extends it with some valuable properties like PersistentIdentity, PersistentVersion, IdentityFields etc..

The PropertyDescriptors that the IPersistentTypeDescriptor returns are actually descriptors for the fields of the persistent classes due to the fact that OpenAccess manipulates fields of persistent classes internally.


IPersistentTypeDescriptor properties:


PersistentIdentity – returns a PropertyDescriptor for the IObjectId identity of the persistent instance.

PersistentVersion – returns a PropertyDescriptor via which the version field of the persistent instance can be accessed. That includes the internal version field(voa_version) as well.


IdentityFields – returns a List<FieldInfo> containing the identity fields of a persistent class. That list contains one element in single identity scenarios and more than one FieldInfo element in composite identity scenarios.


BaseType – returns the IPersistentTypeDescriptor of the base class in inheritance scenarios.

DiscriminatorValue – returns the value of the Discriminator column for the persistent class.

ClassId – returns the class Id.

HasArtificialFields – bool property indicating whether the persistent type represented by the IPersistentPropertyDescriptor contains any artificial fields.


 

IPersistentTypeProperties important methods:


GetProperties() – returns a PropertyDescriptorCollection object which contains the PropertyDescriptor objects that describe every field of the persistent class.

IsArtificialField(PropertyDescriptor pdc) – checks whether a property descriptor is describing an artificial field.


Getting and setting values of object fields using the Persistent Metadata approach:


 The workflow for using the PersistentMetaData API is the following:
-Obtaining the IPersistentTypeDescriptor for a persistence capable instance
-Obtaining the PropertyDescriptorCollection from the IPersistentTypeDescriptor
-Using the GetValue() and SetValue() methods of the PropertyDescriptor objects to manipulate the values of the fields of the persistent class.

C# Copy Code
public void GenericAccessSample(Customer cust, IObjectScope scope)
   {
       IPersistentTypeDescriptor ptd = scope.PersistentMetaData.GetPersistentTypeDescriptor(
typeof(Person));
       PropertyDescriptorCollection pdc = ptd.GetProperties();
       PropertyDescriptor firstNameDesc = pdc[
"firstName"];
       PropertyDescriptor lastNameDesc = pdc[
"lastName"];
       PropertyDescriptor fullNameDesc = pdc[
"fullName"];
       
string firstName = (string)firstNameDesc.GetValue(cust);
       
string lastName = (string)lastNameDesc.GetValue(cust);
       
string fullName = firstName + " " + lastName;
       scope.Transaction.Begin();
       fullNameDesc.SetValue(cust, fullName);
       scope.Transaction.Commit();
   }
VB .NET Copy Code
Public Sub GenericAccessSample(ByVal cust As Customer, ByVal scope As IObjectScope)
  Dim ptd As IPersistentTypeDescriptor = scope.PersistentMetaData.GetPersistentTypeDescriptor(GetType(Person))
  Dim pdc As PropertyDescriptorCollection = ptd.GetProperties()
  Dim firstNameDesc As PropertyDescriptor = pdc("firstName")
  Dim lastNameDesc As PropertyDescriptor = pdc("lastName")
  Dim fullNameDesc As PropertyDescriptor = pdc("fullName")
  Dim firstName As String = CStr(firstNameDesc.GetValue(cust))
  Dim lastName As String = CStr(lastNameDesc.GetValue(cust))
  Dim fullName As String = firstName & " " & lastName
  scope.Transaction.Begin()
  fullNameDesc.SetValue(cust, fullName)
  scope.Transaction.Commit()
End Sub