Obtaining an Object by Using an ObjectKey
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
The primary use of the ObjectKey is to obtain the instance identified by the key. The OpenAccessContext exposes four methods querying an object without having to construct and execute a query - GetObjectByKey, GetObjectByKey<T> and their counterparts, TryGetObjectByKey and TryGetObjectByKey<T>. However, there are several important things that should be pointed out here. The runtime will first look in the existing instantiated objects to see whether the object has already been retrieved. If it is found, this is what will be returned. If the 2-nd Level Cache is enabled, the context checks the cache to see if the object exists there, and if so returns its reference. If the object is not found yet, a query to the data store will be executed automatically. If a row with the specified identity is found, the corresponding object instance is materialized and returned. If a row is not found in the database, a NoSuchObjectException will be thrown.
If the ObjectKey has version information, Telerik Data Access performs an in-memory check of the version of the fetched object against the requested version. If the versions match, the object is returned, else an OptimisticVerificationException is thrown.
In case you need to obtain the object by ignoring the version, you need to call the ObjectKey.GetWithoutVersion method on the existing key first, and then to call GetObjectByKey method.
Another thing that should be considered is that Telerik Data Access performs runtime the following validation on the metadata specified in the ObjectKey:
- Whether the TypeName points to a valid persistent type within the Domain Model.
- Whether the number of the key members and the name of each key are valid for the specified type.
The following examples demonstrate how to obtain object by using the various methods exposed by the OpenAccessContext class.
-
Using the OpenAccessContext.GetObjectByKey method.
private static void ObtainObject(ObjectKey objectKey) { using (EntitiesModel dbContext = new EntitiesModel()) { MyEntity myObject = dbContext.GetObjectByKey(objectKey) as MyEntity; } }
Private Shared Sub ObtainObject(ByVal _objectKey As ObjectKey) Using dbContext As New EntitiesModel() Dim myObject As MyEntity = TryCast(dbContext.GetObjectByKey(_objectKey), MyEntity) End Using End Sub
-
Using the OpenAccessContext.TryGetObjectByKey method.
private static void ObtainObject(ObjectKey objectKey) { using (EntitiesModel dbContext = new EntitiesModel()) { object myObject null; if( dbContext.TryGetObjectByKey(objectKey, out myObject)) { // ... } } }
Private Shared Sub ObtainObject(ByVal _objectKey As ObjectKey) Using dbContext As New EntitiesModel() Object myObject Nothing If dbContext.TryGetObjectByKey(_objectKey, myObject) Then ' ... End If End Using End Sub
-
Using the OpenAccessContext.GetObjectByKey<T> method.
private static void ObtainObject(ObjectKey objectKey) { using (EntitiesModel dbContext = new EntitiesModel()) { SingleIdentity myObject = dbContext.GetObjectByKey<SingleIdentity>(objectKey); } }
Private Shared Sub ObtainObject(ByVal _objectKey As ObjectKey) Using dbContext As New EntitiesModel() Dim myObject As SingleIdentity = dbContext.GetObjectByKey(Of SingleIdentity)(_objectKey) End Using End Sub
-
Using the OpenAccessContext.TryGetObjectByKey<T> method.
private void ObtainObject(ObjectKey objectKey) { using (EntitiesModel dbContext = new EntitiesModel()) { MyEntity myObject = null; if( dbContext.TryGetObjectByKey<MyEntity>(objectKey, out myObject)) { // ... } } }
Private Sub ObtainObject(ByVal _objectKey As ObjectKey) Using dbContext As New EntitiesModel() Dim myObject As MyEntity = Nothing If dbContext.TryGetObjectByKey(Of MyEntity)(_objectKey, myObject) Then ' ... End If End Using End Sub
If you try to obtain an object that does not exist in the database by using either the GetObjectByKey method or its counterpart, GetObjectByKey<T>, a NoSuchObjectException will be thrown. To avoid the exception, you could use the TryGetObjectByKey or TryGetObjectByKey<T> methods.
-
Using the OpenAccessContext.LookupObjectByKey<T> and LookupObjectByKey methods. The LookupObjectByKey returns an object identified by the specified object key if available in the cache.
private void ObtainObject( ObjectKey objectKey ) { using ( EntitiesModel dbContext = new EntitiesModel() ) { MyEntity myObject = dbContext.LookupObjectByKey<MyEntity>( objectKey); } }
Private Sub ObtainObject(ByVal _objectKey As ObjectKey) Using dbContext As New EntitiesModel() Dim myObject As MyEntity = dbContext.LookupObjectByKey(Of MyEntity)(_objectKey) End Using End Sub