locked
Entity by index ? What is IProperty ?

    Question

  • I want to be able to store a integer that references a specific entity in a data source. Other screens can know the entity being processed by knowing the integer.

    var ArrayOfEntities = this.DataWorkspace.ApplicationData.Details.Properties.All().OfType<Microsoft.LightSwitch.Details.IProperty>().ToArray();

     Returns an array of IProperty that contains each entity in ApplicationData. Is the order (index) of IProperty stable ? Do I have to worry about objects other than entities in this Array ?

    Basically, What Is IProperty and is it the easiest way to enumerate entities? Should I use IDataServiceEntitySetProperty or just Properties.All() ?

    Thanks, Mike



    Friday, May 06, 2011 2:13 PM

Answers

  • I wouldn't rely on the order of the entity properties to be consistent, especially while you are developing your application and possibly change properties on entities. To an even lesser extent, I wouldn't rely on the order of the entity sets either.

    What you can do is format your parameters so that it's a key/value pair of the type of object and the name of the object (e.g. "entity:customer" and "property:name"). Then parse the identifiers out and use those to find the correct parts of the model that you want.

    As for the code, there's a shorter way to get to the list of entity types without using a data workspace:

    IEntityType entityType = this.Application.Details.GetModel().GlobalItems.OfType<IEntityType>().Single(e => e.Name.Equals(entityName, StringComparison.Ordinal));
    IEntityPropertyDefinition entityProperty = entityType.Properties.Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal));

    and for getting a property value off on an entity:

    object value = this.ScreenEntityName.Details.Properties.All().Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal)).Value;
    

    Justin Anderson, LightSwitch Development Team
    • Marked as answer by TejanoSMA Friday, May 06, 2011 9:14 PM
    Friday, May 06, 2011 8:45 PM

All replies

  • string EntityPropertyName = this.DataWorkspace.ApplicationData.GetModel().EntitySets.ElementAt(2).EntityType.Members.ElementAt(3).Name;

    Is the name of the 4th property of the 3rd entity, without having any of that entity data at hand. On another screen with 3rd entity data:

    this.ScreenEntityName.Details.Properties.All().OfType<Microsoft.LightSwitch.Details.IEntityTrackedProperty>().ToArray()[3].Value.ToString();

    will give me a string to display/print for the value of the 4th property of the entity.

    My concern is how long am I going to go before I find out this does not work ?

    Is the order of the Member Names on the model of the entity going to stay the same as the order of the IEntityTrackedProperties of the data entity details ?

    Scenario is entering records of formatting a field on one screen with no actual data, referenced as "entity#, property#", then display/print of the field on another screen with data and a formatting record.

    Mike

    Friday, May 06, 2011 5:59 PM
  • I wouldn't rely on the order of the entity properties to be consistent, especially while you are developing your application and possibly change properties on entities. To an even lesser extent, I wouldn't rely on the order of the entity sets either.

    What you can do is format your parameters so that it's a key/value pair of the type of object and the name of the object (e.g. "entity:customer" and "property:name"). Then parse the identifiers out and use those to find the correct parts of the model that you want.

    As for the code, there's a shorter way to get to the list of entity types without using a data workspace:

    IEntityType entityType = this.Application.Details.GetModel().GlobalItems.OfType<IEntityType>().Single(e => e.Name.Equals(entityName, StringComparison.Ordinal));
    IEntityPropertyDefinition entityProperty = entityType.Properties.Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal));

    and for getting a property value off on an entity:

    object value = this.ScreenEntityName.Details.Properties.All().Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal)).Value;
    

    Justin Anderson, LightSwitch Development Team
    • Marked as answer by TejanoSMA Friday, May 06, 2011 9:14 PM
    Friday, May 06, 2011 8:45 PM
  • Thanks Justin.

    Mike

    Friday, May 06, 2011 9:14 PM
  • I wouldn't rely on the order of the entity properties to be consistent, especially while you are developing your application and possibly change properties on entities. To an even lesser extent, I wouldn't rely on the order of the entity sets either.

    What you can do is format your parameters so that it's a key/value pair of the type of object and the name of the object (e.g. "entity:customer" and "property:name"). Then parse the identifiers out and use those to find the correct parts of the model that you want.

    As for the code, there's a shorter way to get to the list of entity types without using a data workspace:

    IEntityType entityType = this.Application.Details.GetModel().GlobalItems.OfType<IEntityType>().Single(e => e.Name.Equals(entityName, StringComparison.Ordinal));
    IEntityPropertyDefinition entityProperty = entityType.Properties.Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal));

    and for getting a property value off on an entity:

    object value = this.ScreenEntityName.Details.Properties.All().Single(p => p.Name.Equals(propertyName, StringComparison.Ordinal)).Value;
    

    Justin Anderson, LightSwitch Development Team


    I would suggest using the property indexer versus using the Single query operator.  The property indexer has a built in performance optimization which is useful if this is frequently run code.  In addition, the indexer makes the code easier to read IMO.

     

    object value = this.ScreenEntityName.Details.Properties[propertyName];
    

    Monday, May 09, 2011 1:24 PM