Thursday, December 20, 2012

Silverlight POCO returned by RIA services

Silverlight POCO returned by RIA services

I am using a Silverlight 5 Business Application using RIA services to return a POCO class from the service side to populate a hierarchical menu.

The original problem I had with the POCO class was that the SubMenuItems property was not getting passed over RIA services although it was populated on the service side.

Original POCO

public class BusinessModelMenuDto {     [Key]     [Required]     public int ID { get; set; }     public string TextToDisplay { get; set; }     public string ImageSource { get; set; }     public IEnumerable<BusinessModelMenuDto> SubMenuItems { get; set; } } 

Service call

 public IEnumerable<BusinessModelMenuDto> GetCabsHeirarchy() 

Following some further investigation I found that the [Include] and [Association] attributes were required on the SubMenuItems to pass the data over. Doing this the first time with the Association of ID => ID did not give the desired results so I added the ParentID property and changed my loading code to populate the Foreign Key as below. I also changed the Associate to map from ID to Parent ID.

Updated POCO class

public class BusinessModelMenuDto {     [Key]     [Required]     public int ID { get; set; }     public int? ParentID { get; set; }     public string TextToDisplay { get; set; }     public string ImageSource { get; set; }     [Include]     [Association("SubItems", "ID", "ParentID")]     public IEnumerable<BusinessModelMenuDto> SubMenuItems { get; set; } } 

On the server side I am loading two levels of the menu at the moment so the top level item contains a collection of SubItems but there are no further SubItems below that.

The problem I have is that when RIA services sends the collection over the wire the hierarchy is being jumbled. I have confirmed that what I am returned is correctly structured but it does not arrive on the client side correctly. The top level is OK but the second level (SubMenuItems) is mixed up and two furter SubMenuItems levels have appeared.

Any idea what I am doing wrong? I assume that the problem is with the Association or the fact that the same POCO object (BusinessModelMenuDto) is being used for the multiple levels.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog