2010-08-25 116 views
2

关于使用presentationModel上带有RIA服务接口的问题通过实现接口的Ria服务公开一个对象

有可能通过Ria Services公开一个对象来实现一个接口

接口:

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'在此声明类型上无效。它只对'class,struct'声明有效。

Ria服务似乎试图将接口视为实体?我们如何克服这个问题?

问候,

斯特凡

回答

4

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

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

文件:ITestInterface.shared.cs

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

文件:TestPresentationModel.cs

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

File:TestPresentationModel .ITestInterface.shared.cs

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

一种可能是让你的客户端实体实现此接口。这就是我所做的。将文件添加到您的Silverlight应用程序,在同一个命名空间为您的实体,那么就延长了实体(它们在局部类中的所有定义):

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

那么只有您的客户端实体有这个接口,所以这可能不是你正在拍摄的东西,但它对我来说效果很好。

+0

我需要在服务器端和客户端上的接口。我想我碰到了RIA服务的限制。 – Stephane 2010-08-26 06:47:50

+0

它不是一个限制,我们在我们的实现中使用接口,是一个POCO类还是EF生成的? – kmacmahon 2010-09-10 23:21:49

+0

限制是您无法从查询操作中暴露接口。 'public IQueryable GetMyInts()'不支持'public IQueryable GetMyInts()'。 – 2011-06-24 16:47:48