c# - 通过实现接口(interface)的 Ria 服务公开对象

标签 c# silverlight interface wcf-ria-services

有关将presentationModels 上的接口(interface)与RIA 服务一起使用的问题

可以通过实现接口(interface)的 Ria 服务公开对象吗?

界面:

public interface TestInterface
{
    public int ID {get;set;}
}

我们有一个演示模型:

public class TestPresentationModel : TestInterface
{
   [Key]
   public int ID {get;set;}
}

我现在收到编译错误: DomainService“SomeDomainService”中的实体“TestInterface”没有定义键。 DomainService 操作公开的实体必须至少有一个用 KeyAttribute 标记的公共(public)属性。

我尝试添加 [Key] 属性,但随后出现以下错误: 派生实体类型“TestPresentationModel”必须在根实体“TestInterface”的 KnownTypeAttribute 中声明。

我尝试添加 [KnownTypeAttribute] 属性,但随后出现以下编译错误: 属性“KnownType”在此声明类型上无效。它仅对“类、结构”声明有效。

Ria服务似乎试图将接口(interface)视为一个实体?我们怎样才能克服这个问题呢?

问候,

史蒂芬

最佳答案

可以在服务器端和客户端使用您需要的类(viewModel)的接口(interface)。为此,您需要与接口(interface)实现共享接口(interface)和部分 View 模型类。

在您的情况下,您需要在服务器项目中定义类和文件,如下所示:

文件:ITestInterface.shared.cs

public interface TestInterface{
  public int ID {get;set;}
}

文件:TestPresentationModel.cs

public partial class TestPresentationModel {
  [Key]
  public int ID {get;set;}
}

文件:TestPresentationModel.ITestInterface.shared.cs

public partial class TestPresentationModel : ITestInterface {
   // can be empty cause the interface implementation is in TestPresentation.cs
}

关于c# - 通过实现接口(interface)的 Ria 服务公开对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3567560/

相关文章:

wpf - 在 Silverlight 4 中以编程方式设置控件的样式?

.net - WPF - 控制半透明层之间的混合

c# - 将数组本身实现为 `.CopyTo(Array,int)` 作为目标数组(要复制到)

Java 接口(interface)和返回类型

c# - 如何将委托(delegate)添加到接口(interface) C#

c# - 创建实体时发现重复记录错误

c# - LINQ 调试时间代码更改...是否可以在 vs2008 中使用?

c# - Entity Framework 迁移 - 启用 AutoMigrations 以及添加的迁移

c# - 为什么我的 Html.DropDownList 会生成一个充满 "System.Web.Mvc.SelectListItem"字符串的列表?

wpf - Silverlight 4等效于WPF “x:static”