none
Remote Lazy Loading? RRS feed

  • Question

  • Is it possible for a client to receive an entity from the server and then remotely load if necessary?

    If not now, could this be a possible future enhancement?

     

    Thank you

    Wednesday, May 14, 2008 6:12 PM

Answers

  • Two comments:

     

    1) Sounds like you are using LINQ to SQL rather than the Entity Framework.  So I'm going to move this thread to the LINQ to SQL forum.

     

    2) I suspect the answer to this is that, no you will not be able to get automatic loading of related entities over a web service.  For many folks part of the point of introducing a web service boundary into their architecture is to get more control over the abstractions in a place like this.  Of course this doesn't prevent you from adding some of your own code to your data classes which would implement lazy loading by checking if things have been loaded and if not calling a web service to do the load.

     

    - Danny

     

    Friday, May 16, 2008 4:02 AM
  • Allexey,

    As Danny mentions, the reason this doesn't work is that once you have serialized the entity over the service boundary, you have an object that is not attached to a DataContext. Also, it coud be just a thin proxy that has been generated after the serialization contract of the original entity.

    If you want to have the Image on the client, I would recommend implementing one of two options:

    1. Simply add a separate operation in you web service that returns the image for certain ImageID, and invoke that as needed from the client.

    2. Always load and serialize the image with eager (as opposed as deferred) loading. To do that you can use the DataLoadOptions.LoadWith() method. Some code in you web service would look like this:

    Code Snippet

    var loadOptions = new System.Data.Linq.DataLoadOptions();

    loadOptions.LoadWith<Part>(p => p.Image);

    context.LoadOptions = loadOptions;

    var query = from p in context.Parts

                where p.PartID == partID

                select p;

     

     

    Hope this helps,

    Diego

    Sunday, May 18, 2008 8:38 AM

All replies

  • With our standard entity classes lazy loading is always explicit and only happens on navigation properties. If you query for certain entity, you get the full entity. If you wanted, you could get first only the key, and then query for the whole entity when you need it.

    Besides that, one of my colleagues in our team has recently released a set of experimental classes for EF that implement lazy loading in such a way that you can load the key only at first and then lazy load the rest of the entity data on the first access to a property. It is a very interesting project but I have to repeat that it is at the time experimental and not supported.

    You can read more about it here.

    Also, I would rather like to know more details about your scenario. Blindly using lazy loading can cause many hidden requests to the database, performance and data consistency problems. If you do it remotely, this is aggravated by the fact that each request to the database is a network roundtrip.

    I recently added a few links in this blog post that you may be interested in reading.

    Hope this helps,

    Diego

    Thursday, May 15, 2008 1:18 AM
  • Greetings everyone,

    Diego, I've got this situation:

    I have WCF service that is connected to DB, and client app (WPF) that connects to WCF service and has no connection to DB.

    In DB I have tables named Parts and Images. Both tables are referenced in dbml file on service side as Part & Image classes respectively .
    Both have Serialization Mode = Unidirectional.
    In Part class I have ImageId that refers to Image.Id, using this I have Association Image_Part and property Part.Image.

    And the problem:
    using test OperationContract on service side I get instance of Part. Then I serialize it and send to a client, deserialize and work with it. The thing is - when I work on server side if I access Image property of Part in automatically queries DB and returns the data, but on client side it does not work - it returns null.

    Is there any way to make it query Image from client side using wcf service?

    Thanks in advance,

    Alexey
    Thursday, May 15, 2008 10:14 AM
  • Two comments:

     

    1) Sounds like you are using LINQ to SQL rather than the Entity Framework.  So I'm going to move this thread to the LINQ to SQL forum.

     

    2) I suspect the answer to this is that, no you will not be able to get automatic loading of related entities over a web service.  For many folks part of the point of introducing a web service boundary into their architecture is to get more control over the abstractions in a place like this.  Of course this doesn't prevent you from adding some of your own code to your data classes which would implement lazy loading by checking if things have been loaded and if not calling a web service to do the load.

     

    - Danny

     

    Friday, May 16, 2008 4:02 AM
  • Allexey,

    As Danny mentions, the reason this doesn't work is that once you have serialized the entity over the service boundary, you have an object that is not attached to a DataContext. Also, it coud be just a thin proxy that has been generated after the serialization contract of the original entity.

    If you want to have the Image on the client, I would recommend implementing one of two options:

    1. Simply add a separate operation in you web service that returns the image for certain ImageID, and invoke that as needed from the client.

    2. Always load and serialize the image with eager (as opposed as deferred) loading. To do that you can use the DataLoadOptions.LoadWith() method. Some code in you web service would look like this:

    Code Snippet

    var loadOptions = new System.Data.Linq.DataLoadOptions();

    loadOptions.LoadWith<Part>(p => p.Image);

    context.LoadOptions = loadOptions;

    var query = from p in context.Parts

                where p.PartID == partID

                select p;

     

     

    Hope this helps,

    Diego

    Sunday, May 18, 2008 8:38 AM
  • Danny, Diego

    thanks both of you for your answers. I suspected something of this, but hoped that somehow it is possible to avoid much code writing :-)
    Monday, May 19, 2008 7:14 AM