Up POCO
DevForce 2010 Resource Center » DevForce development » Additional techniques » POCO » Obtain access to entity services

Obtain access to entity services

Last modified on August 15, 2012 17:21

DevForce entities have an EntityAspect property through which entity-related services are provided.  POCO objects by their nature do not have such a property, but there are still ways to obtain access to entity services.


Access to EntityAspect

There are two ways in which to obtain an EntityAspect for a POCO object:

  • Wrap your POCO object at runtime with a class that provides access to EntityAspect services.
  • Implement the IHasPocoEntityAspect interface on your POCO class.


Wrap your POCO object


Use the EntityAspect.Wrap static method to obtain an EntityAspect for the POCO object.

C#
var stateAspect = EntityAspect.Wrap(aState);
VB
Dim stateAspect = EntityAspect.Wrap(aState)

Having done that, you can then work with the object as an entity and query its EntityState.  For example:

C#
stateAspect.AddToManager();
bool isAdded = stateAspect.EntityState.IsAdded();
VB
stateAspect.AddToManager()
Dim isAdded As Boolean = stateAspect.EntityState.IsAdded()

…and of course, use any of the other facilities of the EntityAspect.

Implement IHasPocoEntityAspect

The IHasPocoEntityAspect is a simple interface that contains a single property of type EntityAspect.  To add this implementation to a POCO type simply implement the interface and add an EntityAspect property with a default getter and setter as shown below:

C#
public class State : IHasPocoEntityAspect {
 //…[snip]
 [IgnoreDataMember]
 public EntityAspect EntityAspect {
   get;
   set;
  }
}
VB
Public Class State _
 Implements IHasPocoEntityAspect
 '…[snip]
 <IgnoreDataMember> _
 Public Property EntityAspect() As EntityAspect
End Class

Once you have implemented this interface your POCO entity will have direct access to this EntityAspect property, and DevForce will insure that it is properly initialized and configured.

The downside to implementing this interface is that your class is no longer a "pure" POCO class, but the upside is that you'll be able to use your POCO objects as you would any other DevForce entity.

The EntityAspect property should not be serialized with your object.  In order to indicate this you can use the IgnoreDataMember attribute, as we did above; or, when using DataContract and DataMember attributes, not mark the property as a data member.

Use IHasPocoEntityAspect for improved performance

DevForce has to keep track of its own bookkeeping data about each POCO instance.  By default, DevForce uses a hashtable to maintain this data, and therefore needs to perform a lookup operation anytime you need access to any EntityAspect services.  

What the IHasPocoEntityAspect interface does is provide a place for DevForce to keep its own bookkeeping data about each POCO instance on the instance itself, hence no need for the hashtable or the lookup.

Tags: POCO
Created by DevForce on December 10, 2010 11:05

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade