locked
Get reference to entity by string? RRS feed

  • Question

  • In code I want to get reference to entity by name(string) without hardcoding it ApplicationData.TestEntity...

    any ideas how this can be achieved?

    Friday, January 4, 2013 11:34 AM

Answers

  • The APIs for most of the concepts in LightSwitch provide a weakly-typed API in addition to the normally used strongly-typed API.  You're asking to make use of the weakly-typed API.  You can access the weakly-typed API by navigating to the Details property on the object in question.  In your example, you want to access, in a weakly-typed manner, the "TestEntities" entity set from the "ApplicationData" data service.  To do this you would write code like the following:

    IEntitySet entitySet = (IEntitySet)this.ApplicationData.Details.Properties["TestEntities"].Value;
    The Details object allows you to get all sorts of advanced information about the object from which you retrieved it.  Most of the objects in LightSwitch have such a Details object, including entities, screens, and even each of the properties of an entity or screen.  So you could write your whole application in a weakly-typed manner if you really wanted.
    Friday, January 4, 2013 2:44 PM

All replies

  • The APIs for most of the concepts in LightSwitch provide a weakly-typed API in addition to the normally used strongly-typed API.  You're asking to make use of the weakly-typed API.  You can access the weakly-typed API by navigating to the Details property on the object in question.  In your example, you want to access, in a weakly-typed manner, the "TestEntities" entity set from the "ApplicationData" data service.  To do this you would write code like the following:

    IEntitySet entitySet = (IEntitySet)this.ApplicationData.Details.Properties["TestEntities"].Value;
    The Details object allows you to get all sorts of advanced information about the object from which you retrieved it.  Most of the objects in LightSwitch have such a Details object, including entities, screens, and even each of the properties of an entity or screen.  So you could write your whole application in a weakly-typed manner if you really wanted.
    Friday, January 4, 2013 2:44 PM
  • Wonderful!

    The more dynamic, the better.


    Garth Henderson - Vanguard Business Technology

    Saturday, January 5, 2013 1:59 AM
  • Just for illustrating how brilliant this weakly-typed API is:

    Following extension method allows you to retrieve a entity based on the weakly typed entity type (e.g. "Customers") and the Key Segment (the Id field in most cases).

     public static class DataServiceExtensions
        {
            public static IEntityObject GetEntityByKey(this IDataService dataService,
    string entitySetName, params object[] keySegments)
            {
                ICreateQueryMethod singleOrDefaultQuery = dataService.Details.Methods[entitySetName +
     "_SingleOrDefault"] as ICreateQueryMethod;
     
                if (singleOrDefaultQuery == null)
                {
                    throw new ArgumentException("An EntitySet SingleOfDefault query does
    not exist for the specified EntitySet.", entitySetName);
                }
     
                ICreateQueryMethodInvocation singleOrDefaultQueryInvocation = singleOrDefaultQuery.CreateInvocation(keySegments);
     
                return singleOrDefaultQueryInvocation.Execute() as IEntityObject;
            }
     
        }

    So, it can be called as follows:

    IEntityObject entityObject = this.DataWorkspace.ApplicationData.GetEntityByKey("Customers", 45);
     

    paul van bladel

    Saturday, January 5, 2013 8:26 AM