【问题标题】:Expose a object via Ria Services that implements an interface通过实现接口的 Ria 服务公开对象
【发布时间】:2010-08-25 15:27:00
【问题描述】:

关于在带有 RIA 服务的presentationModels 上使用接口的问题。

可以通过实现接口的 Ria 服务公开对象

界面:

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

我们有一个presentationModel:

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

我现在得到一个编译错误: DomainService 'SomeDomainService' 中的实体 'TestInterface' 没有定义键。由 DomainService 操作公开的实体必须至少具有一个标有 KeyAttribute 的公共属性。

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

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

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

问候,

斯蒂芬

【问题讨论】:

    标签: c# silverlight interface wcf-ria-services


    【解决方案1】:

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

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

    文件: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
    }
    

    【讨论】:

      【解决方案2】:

      一种可能性是让您的客户端实体实现此接口。这就是我所做的。将文件添加到您的 Silverlight 应用程序中,该文件与您的实体在同一命名空间中,然后只需扩展实体(它们都在部分类中定义):

      namespace My.Model.Namespace
      {
          public partial class TestPresentationModel : TestInterface
          {
              ...
          }
      }
      

      那么只有你的客户端实体有这个接口,所以这可能不是你想要的,但它对我来说效果很好。

      【讨论】:

      • 我需要服务器端和客户端的接口。我想我遇到了 RIA 服务的限制。
      • 这不是限制,我们在实现中使用接口,是 POCO 类还是 EF 生成的?
      • 限制是您不能从查询操作中公开接口。 'public IQueryable GetMyInts()' 不受支持,而 'public IQueryable GetMyInts()' 支持。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多