5

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.

Jehof
  • 34,674
  • 10
  • 123
  • 155
Phil Murray
  • 6,396
  • 9
  • 45
  • 95
  • I had a quick look through our code. I'm not an expert, but wherever we have a similar structure we are using a List<> and not an IEnumerable<> – GarethOwen Dec 20 '12 at 09:48
  • 1
    The underlying object was a List but I tried changing the Property to an IList but its still not working. – Phil Murray Dec 20 '12 at 09:52
  • can you post your query method of the DomainService? – Jehof Jan 03 '13 at 12:58
  • Each Id needs to be unique otherwise your hierarchical structure gets corrupted on client side – Jehof Jan 03 '13 at 13:02
  • It might be useful if you post a simple hierarchy, including all properties (at least `ID` and `ParentID`), and the jumbled result you see client-side. – Sphinxxx Jan 05 '13 at 15:55

1 Answers1

2

We found we had to use Guids for the item Key and assign a unique value to it on the server before passing back to the client.

So your class definition would become:

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

Then when you create a new element set the ID:

ID = Guid.NewGuid();
ChrisF
  • 134,786
  • 31
  • 255
  • 325