Print Page | Close Window

Access custom properties from EntityAspect

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4001
Printed Date: 07-Sep-2025 at 7:06am


Topic: Access custom properties from EntityAspect
Posted By: Vonzkie
Subject: Access custom properties from EntityAspect
Date Posted: 22-Feb-2013 at 1:48am
Hi,

I've created a custom string property "strFullName" on my entity which basically just returns a concatenated string of several properties such as "strLastName", "strFirstName", and "strMiddleName". I created a generic method that access my entity base, and from here I can only access the EntityAspect property of my entity (not actual field names..). So when I try to access the fields strLastName", "strFirstName", and "strMiddleName", I am able to, but not "strFullName". It throws me an error "DataProperty: 'strFullName' does not exist on type 'EntityAspect'". How can I make it so that I can be able to check the value by using this code?

_currentRecord.EntityAspect.GetDataProperty("strFullName")

(I'm thinking I might need to add some attributes or something on my custom property to make it accessible within the EntityAspect..?)

Thanks,
Von



Replies:
Posted By: kimj
Date Posted: 22-Feb-2013 at 10:01am
You can "register" a custom property with DevForce via the PropertyMetadata class and a new DataEntityProperty field, but it may be more work than you want for a simple calculated property. 
 
Registering a backing DataEntityProperty for a custom property looks something like this:
 
public partial class Customer {
  public string FullName {
    get { return PropertyMetadata.FullName.GetValue(this); }
  }
 
  public new partial class EntityPropertyNames  {
    public const string FullName = "FullName";
  }
 
  public partial class PropertyMetadata {
    public static readonly DataEntityProperty<Customer, string> FullName =
    new DataEntityProperty<Customer, string>(
      propertyName: EntityPropertyNames.FullName,
      isNullable: false,
      isPartOfKey: false,
      concurrencyStrategy: ConcurrencyStrategy.None,
      isAutoIncrementing: false,
      relatedNavigationPropertyName: null,
      isNativeProperty: false);
  }
}
 
The problem is that once you've tied into the PropertyMetadata you won't be able to use a simple property getter like you're probably now using, but will need to do some sort of property interception.  Something like this:
 
[AfterGet(EntityPropertyNames.FullName)]
public void FullNameGetInterceptor(PropertyInterceptorArgs<Customer, String> args) {
  args.Value = FirstName + LastName;
}
 
If this all looks like more than you need, then simple reflection will also allow you to get and set your custom property from your entity base class.

 



Print Page | Close Window