Linking existing objects to a new one, without reading them from the database

7 posts, 1 answers
  1. gmendez
    gmendez avatar
    82 posts
    Member since:
    Jun 2012

    Posted 21 Jan 2011 Link to this post

    Hello, I'm developing a stock management system in which I have the following scenario:

    There are "Products" that already exist in the warehouse. At some time, some products has to be sent to customers so "Packages" are definied.
    A Package is an object that has some properties such as the date, the customer and a list of products definied as IList<Product>.
    When creating the package, the only way I figured out to assign products to it, is to read the actual Product objects to be included and add them to the list at the Package object. I can do that because I know their IDs in advance (but I don't have the actual Product objects in memory at that moment). Is there a better or more efficient way to assign Products to the Package based on their IDs, I mean, without having to read them from the database?
    Thanks in advance,

    Gonzalo
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 21 Jan 2011 Link to this post

    Hi gmendez,
    You can have an id field parallel to the reference field. In the mapping the id and the reference field have to point to the same column with the same settings as the pk column on the client side.

    Best wishes,
    Jan Blessenohl
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
  4. gmendez
    gmendez avatar
    82 posts
    Member since:
    Jun 2012

    Posted 25 Jan 2011 Link to this post

    Hi Jan, I've been reading about this topic at documentation and some forum posts.
    The information I've found is about a single instance of class A referencing another single instance of class B.
    My case is different because instance of class A references many instances of class B. I mean class A has an IList<B>, it doesn't have any direct reference to class B.
    Could you explain it a bit more to me please?
    Thanks in advance and best regards.

    Gonzalo
  5. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 25 Jan 2011 Link to this post

    Hi gmendez,

    Sorry for inteferring the thread by maybe I can elaborate further:

    Even in the 1:m scenario you have, where class A have a list of class B you can use the parallel field (pointing to the same column as the reference field).
    This implies that you use an inverse field in the child table class instead of a join table. In your example the table of class B
    In the 1:m scenario it will just be the foreign key field (the inverse field) of the class B.

    So something like this:

    public class A
    {
       private IList<B> listOfB = new List<B>();
    }

    public class B
    {
       // The inverse field - mapped to for example the column "fk_A"
       private A owningA;
       // The id of the instance A owning this instance of B - also mapped to the column "fk_A"
       private int idOfOwningA;
    }

    So whether you add an instance of B to A.ListOfB or you create an instance of B setting B.IdOfOwningA to the id of A should be the same since they are mapped to the same physical field in the table representing class B.

    regards

    Henrik
  6. gmendez
    gmendez avatar
    82 posts
    Member since:
    Jun 2012

    Posted 25 Jan 2011 Link to this post

    Hello Henrik

    Thanks a lot for 'interferring' the thread :D
    I think I got it.
    All will try your suggestion ASAP an let you know.
    Best regards,

    Gonzalo
  7. gmendez
    gmendez avatar
    82 posts
    Member since:
    Jun 2012

    Posted 23 Mar 2011 Link to this post

    Hello again,

    Similar to this situation I'd like to know if it is some how possible to update records without reading them first.
    Imagine a service-based architecture in which clients consume services that internally need to update some record(s) but they don't really need to read those records first. In a similar fashion, perhaps the clients could send information but they are not "OpenAccess Aware" in the sence that they can't use an ObjectContainer to wrap and to keep track of data objects.
    How could this be done using OA?
    Thanks in advance.

    Best regards,

    Gonzalo

  8. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 28 Mar 2011 Link to this post

    Hello gmendez,

    Normally for web service scenarios we recommend using additional set of transport objects instead of exposing the persistent classes through the service. This way you have better control over the data contracts and do not need to "show" details about your data access layer implementation, like that you are using OpenAccess. Imagine that for each persistent object you have a transport object, which wraps the persistent data and is used to transmit it through the service.

    The case of updating a single object would look like this:
    1. A transport object is passed to a method of the service;
    2. In the service implementation the original persistent object is retrieved by the ID of the transport object;
    3. The persistent object is updated with the data received from the transport object and is committed to the database.

    Updating the ID of a referenced object is also possible, as already discussed in the thread. I am afraid that there is no way to update a persistent object without loading it first.

    Greetings,
    Alexander
    the Telerik team
Back to Top
DevCraft banner