Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 593
Phil Murray Avatar asked Nov 04 '22 08:11

Phil Murray


1 Answers

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();
like image 198
ChrisF Avatar answered Nov 15 '22 05:11

ChrisF