0
class Profile { string name; Book[] books} 

class Book { string name }

Two separate Profiles (A and B) both have a relation to the same book.

When I load the profiles one by one from Silverlight using WCF/RIA. I end up with two book object, with the same name. (same book!). I want RIA to detect that the book is already present in silverlight and use the existing instance instead of creating it a second time.

They are POCO (complex object-not in DB).

How do I tell RIA that the Name is unique and that there can not be two instances in Silverlight(RIA client side) with the same name?

UPDATE Since asking I have implemented a WORKAROUND - I dont not really consider it a elegant solution though! But here it is: The callback method on GetProfile is replaced with a method that traverses all profile.books and check if each book already exist in a local Dictionary. If the book exists locally the local book is used and the second instance is deleted. If it doesn't the book is added to the dictionary and used.

SOLUTION I create my own Book-class clientside, then convert the RIA-created bookss to my own book-type. When I send the updated Books back to the server, I create instances of RIA Books, and before updating any properties I call OnDeserializing() and OnDesriallized() after. That allows me to edit the RIA-Book.

jenspo
  • 504
  • 4
  • 11
  • I assume you are using 2 separate DomainContext instances to obtain your book for each profile? That will result is two different instances of the same Book object. Can you post more code? – iCollect.it Ltd Apr 20 '12 at 08:37
  • AFAIK If one of your property is a key,such as primary key's on db side. EntityFramework manages it. Could you check your key properties on edmx file, and domainService class's attributes. – Davut Gürbüz Apr 20 '12 at 11:19
  • @DavutGürbüz It is NOT Entities. Book objects are created serverside (based on a 3rd party API). I have tried adding [Key] property to the Name property. Which results in the autogenerated Profile class no longer has the books property. – jenspo Apr 20 '12 at 11:52
  • @HiTechMagic No. I use the same DomainContext (DomainService) for each profile instance. It is the same method called at different times. – jenspo Apr 20 '12 at 11:54
  • Ohh.I see. Generated code sometimes makes things harder. Especially when you work with WCF not RIA,I had some WCF datamembers autogenerated code didn't contained my attributes. Then I used a shared class and copied the returned values from wcf service to this shared type etc.Sometimes it doesn't be easy as I guess. I see that you managed it,well. – Davut Gürbüz Apr 20 '12 at 12:22

1 Answers1

0

SOLUTION The callback method on GetProfile is replaced with a method that traverses all profile.books and check if each book already exist in a local Dictionary. If the book exists locally the local book is used and the second instance is deleted. If it doesn't the book is added to the dictionary and used.

I create my own Book-class clientside, then convert the RIA-created bookss to my own book-type. When I send the updated Books back to the server, I create instances of RIA Books, and before updating any properties (like FavouriteBook) I call OnDeserializing() and OnDesriallized() after. That allows me to edit the RIA-Book.

jenspo
  • 504
  • 4
  • 11