This is a migrated thread and some comments may be shown as answers.

Generic way to create an object key?

4 Answers 88 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kendall Bennett
Top achievements
Rank 2
Kendall Bennett asked on 14 Jul 2011, 03:30 AM
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?

4 Answers, 1 is accepted

Sort by
0
Kendall Bennett
Top achievements
Rank 2
answered on 17 Jul 2011, 03:50 AM
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
0
Ady
Telerik team
answered on 18 Jul 2011, 02:55 PM
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!

0
Kendall Bennett
Top achievements
Rank 2
answered on 18 Jul 2011, 03:55 PM
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.
0
Ady
Telerik team
answered on 19 Jul 2011, 11:30 AM
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!

Tags
Development (API, general questions)
Asked by
Kendall Bennett
Top achievements
Rank 2
Answers by
Kendall Bennett
Top achievements
Rank 2
Ady
Telerik team
Share this question
or