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

Is there an easy way to check wether an object is in objectscope?

6 Answers 29 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.
Wolfgang
Top achievements
Rank 1
Wolfgang asked on 22 Mar 2013, 07:47 AM
Is there an easy way to check wether an object is in objectscope?

I search for something like:

objectScope.ExistObject(object)
or
objectScope.ExistObjectID(OID);
or 
objectScopse.Contains(object/OID);

Is such a thing available?

6 Answers, 1 is accepted

Sort by
0
Accepted
Ralph Waldenmaier
Telerik team
answered on 22 Mar 2013, 10:12 AM
Hello Wolfgang,

Yes it is available. You can use the following code to obtain already loaded instances.
 

objectScope.LookupObjectById(oid);

Hope this helps.
Do come back in case you need further assistance.

Greetings,
Ralph
the Telerik team
Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
0
Wolfgang
Top achievements
Rank 1
answered on 22 Mar 2013, 11:26 AM
Hi,
thx.

Maybe a small comment.

If i search for LookupObjectByID I cannot find it in help (  http://www.telerik.com/help/search.aspx?q=LookupObjectById&ppId=638&pId=638&start=0 )

In the google cache there still is some information ( http://webcache.googleusercontent.com/search?q=cache:BBdMbd76W40J:www.telerik.com/help/openaccess-orm/topic1228.html+openaccess+LookupObjectById&cd=1&hl=de&ct=clnk&gl=de )

Is the method deprecated?

Regards,
Wolfgang


0
Ralph Waldenmaier
Telerik team
answered on 22 Mar 2013, 11:46 AM
Hello Wolfgang,

You are right. This is due to the fact, that our API reference is not online at the moment. You can see the offline documentation for details, which also includes the API documentation. We will do our best to bring the API documentation back online, unfortunately, I can not provide you with at time frame yet.
The mentioned method is not deprecated. 

Feel free to ask if any other question arises.

Regards,
Ralph
the Telerik team
Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
0
Wolfgang
Top achievements
Rank 1
answered on 29 May 2013, 08:22 AM
Hi,

I have not used the solution because i had some problems with it.

I have made 2 methods, one with LookUpObjectByID, one with GetObjectByID (see below). While GetObject seems to be slower if an item could not be found and raises a NoSuchObjectException, LookUpObjectByID does not find valid elements.

For example i do not change the db and do something like 

bool existObject = IsObjectInObjectScope(aSourceObject_intID);
bool existObject2 = IsObjectInObjectScope2(aSourceObject_intID));

i get (always for the same object) 
existObject = false and 
existObject2 = true

Funilly if i call (always for the same object) 
bool existObject2 = IsObjectInObjectScope2(aSourceObject_intID));
bool existObject = IsObjectInObjectScope(aSourceObject_intID);
i get
existObject = true and 
existObject2 = true
what would the objectscope need to find it with LookupObjectById?

It's no matter of functionallity with IsObjectInObjectScope2, it's a matter of performance.

best regards
Wolfgang
//method 1
     public bool IsObjectInObjectScope(IObjectId objectID)
        {
            try
            {
                object test = this.objectScope.LookupObjectById(objectID);
                return (null != test);
            }
            catch (Exception ex)
            {
                return false;
            }
        }


// method 2
 
           public bool IsObjectInObjectScope2(IObjectId objectId)
        {
            try
            {
                Object test = this.objectScope.GetObjectById(objectId);
                return (null != test);
            }
            catch (Telerik.OpenAccess.Exceptions.NoSuchObjectException nsoex)
            {
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
// method 1
        public bool IsObjectInObjectScope(IObjectId objectID)
        {
            try
            {
                object test = this.objectScope.LookupObjectById(objectID);
                return (null != test);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
// method 1
        public bool IsObjectInObjectScope(IObjectId objectID)
        {
            try
            {
                object test = this.objectScope.LookupObjectById(objectID);
                return (null != test);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
0
Ralph Waldenmaier
Telerik team
answered on 29 May 2013, 03:55 PM
Hello Wolfgang,

The behavior you described is expected. Let me explain why.
With LookupObjectById you are asking the first level cache, if there is already an object with the provided OID loaded.
With GetObjectById, we are first checking the cache if it is already loaded and if we don't find it in the cache, then the object will be loaded from the database. This is also the reason, why this method is slower, because it potentially needs to access the database.

This means for your tests, that in the first setup you are checking the cache and then fetching the object from the database. The LookupByObjectId provides the correct answer, false in this case, because the object was not loaded so far. It will be loaded with the second call which then returns true, which is also right.

In your second setup, you are first loading the object from the database and after this you are asking the cache if the object is still there. The results you see are then also correct, as the object was loaded before.

I hope this information is helpful for you.
Do come back in case you need further assistance.

Regards,
Ralph
Telerik
OpenAccess Samples Kit boasts 50+ sample applications providing diverse real-life business solutions. Click to read more and see OpenAccess ORM in action.
0
Wolfgang
Top achievements
Rank 1
answered on 03 Jun 2013, 08:31 AM
Thank you, that clarifies also some other things.
Tags
Development (API, general questions)
Asked by
Wolfgang
Top achievements
Rank 1
Answers by
Ralph Waldenmaier
Telerik team
Wolfgang
Top achievements
Rank 1
Share this question
or