Generic way to create an object key?

5 posts, 0 answers
  1. Kendall Bennett
    Kendall Bennett avatar
    80 posts
    Member since:
    Feb 2010

    Posted 13 Jul 2011 Link to this post

    Does anyone know of a way to create an object key generically for an entity from a list of primary key values? I want to implement a function like the following generically for any entity in a generic class, rather than having to implement is specifically for each class as I do now:

            /// <summary>
            /// Lookup an entity record by primary key values. Note that the entity is returned using
            /// the cached read-only context but is not detached from the context so navigation properties
            /// will work.
            /// </summary>
            /// <param name="keyValues">List of key values for the entity to delete</param>
            /// <returns>Entity record if found, false if not</returns>
            TEntity Find(params object[] keyValues);

    This is identical to an API function that is implemented in the Entity Framework DbContext API, and it is a nice surface to work against in web applications where you need to look up entities by primary key values a lot, when the primary key values are passed around as part of a web request. I would like to be able to call GetObjectByKey() on my context to implement this, but to do that, I need to create an object key and right now the only way to do that is to create an instance of the entity and assign the primary keys to it.

    I can easily do that in each specific repository type, because it is easy once I know the type of the entity, but is there some way to implement this in such a way that the object key can be created by matching the primary keys with those in the entity? In most cases there would only be one primary key, but some cases it might be a combination of fields.

    I assume something like this could be done with reflection, or inspecting the metadata in the metadata container for the type of entity? But I am not sure where to start.

    Any suggestions on how to go about implementing something like this?
  2. Kendall Bennett
    Kendall Bennett avatar
    80 posts
    Member since:
    Feb 2010

    Posted 16 Jul 2011 Link to this post

    I was able to implement this using the method mentioned here to get a list of primary keys for an entity, and then using that to create an object key:

    http://www.telerik.com/community/forums/orm/development/retrieve-primary-key-fieldnames.aspx
  3. DevCraft banner
  4. Ady
    Admin
    Ady avatar
    588 posts

    Posted 18 Jul 2011 Link to this post

    Hello Kendall Bennett,

     I'm glad that you could implement this behavior. One word of caution though (which you might have already taken care of ) - you need to pass the primary key values in the same order , in the object[] keyValues parameter, as the primary key fields obtained from the metadata. i.e. the values need to match 1-1 to the primary key field.

    Regards,
    Ady
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  5. Kendall Bennett
    Kendall Bennett avatar
    80 posts
    Member since:
    Feb 2010

    Posted 18 Jul 2011 Link to this post

    Yes, the properties in the metadata are sorted in alphabetical order so we need to pass the keys in that order. A little different to Entity Framework but it works.
  6. Ady
    Admin
    Ady avatar
    588 posts

    Posted 19 Jul 2011 Link to this post

    Hi Kendall Bennett,

     Yes, you a right.

    Do get back in case you need further assistance.

    Regards,
    Ady
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top
DevCraft banner