简体   繁体   English

RIA服务返回Silverlight POCO

[英]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. 我正在使用使用RIA服务的Silverlight 5业务应用程序从服务端返回POCO类以填充分层菜单。

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. 我对POCO类的原始问题是SubMenuItems属性没有通过RIA服务传递,尽管它是在服务端填充的。

Original POCO 原创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. 经过进一步调查后,我发现SubMenuItems需要[Include][Association]属性来传递数据。 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. 使用ID => ID关联第一次执行此操作时未提供所需结果,因此我添加了ParentID属性并更改了我的加载代码以填充外键,如下所示。 I also changed the Associate to map from ID to Parent ID. 我还将Associate更改为从ID映射到Parent ID。

Updated POCO class 更新了POCO课程

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. 在服务器端,我正在加载菜单的两个级别,因此顶级项目包含SubItems的集合,但是下面没有其他SubItems。

The problem I have is that when RIA services sends the collection over the wire the hierarchy is being jumbled. 我遇到的问题是,当RIA服务通过线路发送集合时,层次结构正在混乱。 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. 顶级是可以的,但是第二级(SubMenuItems)混合在一起,并且出现了两个更进一步的SubMenuItems级别。

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. 我假设问题出在协会或者同一个POCO对象(BusinessModelMenuDto)被用于多个级别的事实。

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. 我们发现我们必须为项目Key使用Guids,并在返回客户端之前在服务器上为其分配唯一值。

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:

ID = Guid.NewGuid();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM