How do I return an object with a multiple field primary key?

Thread is closed for posting
6 posts, 0 answers
  1. richardFlow
    richardFlow avatar
    62 posts
    Member since:
    Jun 2012

    Posted 09 Mar 2010 Link to this post

    Hi,

    I want to write a simple method for returning an object that is specified using multiple fields.

    the code i use to return an object with a single field primary key is as follows:

      return (Product)scope.GetObjectById(Database.OID.ParseObjectId(typeof(Product), ProductKey));

    However, with what do I replace the string ProductKey when the object has several fields, say KeyA, KeyB and KeyC required to select it?

    Thanks!
              
  2. Matt.F
    Matt.F avatar
    12 posts
    Member since:
    Jun 2007

    Posted 10 Mar 2010 Link to this post

    Hi there,

    Could you not use LINQ and do something along the lines of:

     return (from prod in Scope.Extent<Product>() where prod.Key1 ==  Value1 && prod.Key2 == Value2 && prod.Key3 == Value3  select prod ).First();

    I'm kinda new to this whole ORM business so if I'm suggesting something which is crazy wrong please correct me so that I can make sure to correct my code.

    Matt
  3. richardFlow
    richardFlow avatar
    62 posts
    Member since:
    Jun 2012

    Posted 12 Mar 2010 Link to this post

    Hi,

    Yes, that is currently how we implement the functionality, but from reading the guide i'm of the impression i lose out on all the benefits of the caching in memory of the objects etc...  The GetBojectById only goes to the database if it needs to.

    Richard.
  4. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 15 Mar 2010 Link to this post

    Hi Web Belief Ltd,

    There are classes that implement the IObjectId interface, generated for each of the persistent classes with multiple identity. You could use those types of objects to get persistent objects by their identity. Here is an example with an OrderDetail persistent object which has composite key from the orderID and productID fields.

    scope.GetObjectById<OrderDetail>(new OrderDetail.ID() { orderID = 1, productID = 3 });


    Regards,
    Zoran
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. richardFlow
    richardFlow avatar
    62 posts
    Member since:
    Jun 2012

    Posted 18 Mar 2010 Link to this post

    Thanks Zoran,

    I tried that code but got this error: 
    The non-generic method 'Telerik.OpenAccess.IObjectContext.GetObjectById(Telerik.OpenAccess.IObjectId)' cannot be used with type argument

    I changed it to this and it worked:
    (OrderDetail)scope.GetObjectById(new OrderDetail.ID() { orderID = 1, productID = 3 });

    Why did your code not work? Is the way I've done it as efficient? Does this method avail of the caching?

    Also, is the method you suggested better than the code I use to return an object with a single field primary key (which I detailed in the 1st post)?

    Thanks!
  6. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 22 Mar 2010 Link to this post

    Hi Web Belief Ltd,

    I guess that you are using an older version of OpenAccess, that is why the generic variant of the GetObjectById method is not visible on your side. Nevertheless the performance is not really effected in both cases - no matter if you use the generic or non-generic approach.

    Regarding your later question - yes this method does consider the 1st and 2nd level cache and if you object is already cached - OpenAccess will return it from there instead of visiting the database.

    Greetings,
    Zoran
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top