New Posts New Posts RSS Feed: Using Dynamic Properties to Access Values in a Dynamic Entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Using Dynamic Properties to Access Values in a Dynamic Entity

 Post Reply Post Reply
Author
Customer View Drop Down
Senior Member
Senior Member
Avatar
User Submitted Questions to Support

Joined: 30-May-2007
Location: United States
Posts: 260
Post Options Post Options   Quote Customer Quote  Post ReplyReply Direct Link To This Post Topic: Using Dynamic Properties to Access Values in a Dynamic Entity
    Posted: 06-Jun-2007 at 12:57pm

Question:

 

How do we access the fields of a property of a dynamic entity? I have the following code:

 

        Dim DynType As Type

        DynType = DynamicEntityTypeBuilder.CreateType("TestDS", "Default")

        Dim qry1 As IEntityQuery

        qry1 = New PassthruRdbQuery(DynType,

   "SELECT * FROM WORKORDER WHERE WORKORDERID < 200")

        Dim DynEntities As IList =

   PersistenceManager.DefaultManager.GetEntities(Of DynamicEntity)(qry1)

        Me.GridControl1.DataSource = DynEntities

 

The problem is, the gridcontrol only than displays the RowState, HasErrors, RowError, and etc. columns. I can’t get it to display the fields from the select query, e.g., WorkOrderNumber, Description etc. How do we bind to fields of the dynamic entity?  

Contains an Employee class that demonstrates the use of a DynamicEntity type and a PassThruQuery containing an aggregate SUM function.



Edited by Customer - 06-Jun-2007 at 1:02pm
Back to Top
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Posted: 06-Jun-2007 at 12:59pm

Answer:

Dynamic Properties!

Here is a NUnit test I wrote to confirm my approach:

First, the C#:

 

/// <summary>AggregateQuery Test</summary>

/// <remarks>

/// Demonstrates an aggregate query using DynamicTypes

/// and creation of a dynamic property so that we can

/// data bind to the column (field) returned from the query.

/// </remarks>

[Test]

 public void AggregateQueryTest() {

   Type DynType;

   DynType = DynamicEntityTypeBuilder.CreateType("DynTypeTest", "Default");

 

   // Query for count of Employees with a LastName before 'K'

   IEntityQuery qry;

   qry = new PassthruRdbQuery(DynType,

     "SELECT count(*) as TotalEmps FROM Employee WHERE LastName < 'K'");

   EntityList<DynamicEntity> entities =

     PersistenceManager.DefaultManager.GetEntities<DynamicEntity>(qry);

 

   // Add a property descriptor to get a column we want through data binding

   AddTotalEmpsPropertyDescriptor(DynType, entities);

 

 }

 

 /// <summary>

 /// Add a "TotalEmps" dynamic property for a dynamic type

 /// with an integer "TotalEmps" field.

 /// </summary>

 private void AddTotalEmpsPropertyDescriptor(Type pDynType,

   EntityList<DynamicEntity> pEntities) {

 

   // This MUST become a private property list

   // because the next time an instance of

   // DynamicEntity will have completely different columns

   // and we don't want the pd we're about to add to be

   // hanging around!

   pEntities.UsesGlobalPropertyDescriptors = false;

 

   // Make a dynamic property descriptor for TotalEmps

   AdaptedPropertyDescriptor pd =

     new AdaptedPropertyDescriptor("TotalEmps", pDynType, typeof(int), _get_TotalEmps, null);

 

   // Add it to the list's PropertyDescriptors

   pEntities.PropertyDescriptors.Add(pd);

 

   // Confirm that the "getter" delegate returns the same value

   // as the field inside the entity.

   // That means it will work when we data bind to it.

   Assert.AreEqual(pEntities[0]["TotalEmps"], _get_TotalEmps(pEntities[0]));

 }

 

 /// <summary>

 /// The "getterdelegate" used within a dynamic property for a dynamic type

 /// with an integer "TotalEmps" field.

 /// </summary>

 private object _get_TotalEmps(object pEntity) {

   return (int) ((DynamicEntity) pEntity)["TotalEmps"];

 }

 

Now the VB (which I have converted but not tested):

 

''' <summary>AggregateQuery Test</summary>

''' <remarks>

''' Demonstrates an aggregate query using DynamicTypes

''' and creation of a dynamic property so that we can

''' data bind to the column (field) returned from the query.

''' </remarks>

<Test> _

Public Sub AggregateQueryTest()

   Dim DynType As Type

   DynType = DynamicEntityTypeBuilder.CreateType("DynTypeTest", "Default")

 

   ' Query for count of Employees with a LastName before 'K'

   Dim qry As IEntityQuery

   qry = New PassthruRdbQuery(DynType, _

    "SELECT count(*) as TotalEmps FROM Employee WHERE LastName < 'K'")

 

  Dim entities As EntityList(Of DynamicEntity) = _

    PersistenceManager.DefaultManager.GetEntities(Of DynamicEntity)(qry)

 

   ' Add a property descriptor to get a column we want through data binding

   AddTotalEmpsPropertyDescriptor(DynType, entities)

 

End Sub

 

 ''' <summary>

 ''' Add a "TotalEmps" dynamic property for a dynamic type

 ''' with an integer "TotalEmps" field.

 ''' </summary>

 Private Sub AddTotalEmpsPropertyDescriptor( _

   ByVal pDynType As Type, ByVal pEntities As EntityList(Of DynamicEntity))

 

   ' This MUST become a private property list

   ' because next time an instance of

   ' DynamicEntity will have completely different columns

   ' and we don't want the pd we're about to add to be

   ' hanging around!

   pEntities.UsesGlobalPropertyDescriptors = False

 

   ' Make a dynamic property descriptor for TotalEmps

   Dim pd As New AdaptedPropertyDescriptor( _

    "TotalEmps", pDynType, GetType(Integer), AddressOf _get_TotalEmps, Nothing)

 

   ' Add it to the list's PropertyDescriptors

   pEntities.PropertyDescriptors.Add(pd)

 

   ' Confirm that the "getter" delegate returns the same value

   ' as the field inside the entity.

   ' That means it will work when we data bind to it.

   Assert.AreEqual(pEntities(0)("TotalEmps"), _get_TotalEmps(pEntities(0)))

 End Sub

 

 ''' <summary>

 ''' The "getterdelegate" used within a dynamic property for a dynamic type

 ''' with an integer "TotalEmps" field.

 ''' </summary>

 Private Function _get_TotalEmps(ByVal pEntity As Object) As Object

   Return CInt(Fix((CType(pEntity, DynamicEntity))("TotalEmps")))

 End Function

Enjoy !

 

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down