有关将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/