[英]Expose a object via Ria Services that implements an interface
有關在帶有RIA服務的presentationModels上使用接口的問題。
是否可以通過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]屬性,但是隨后出現以下錯誤:必須在根實體'TestInterface'的KnownTypeAttribute中聲明派生實體類型'TestPresentationModel'。
我試圖添加[KnownTypeAttribute]屬性,但是隨后出現以下編譯錯誤:屬性'KnownType'在此聲明類型上無效。 它僅對“類,結構”聲明有效。
似乎Ria服務試圖將接口視為一個實體? 我們如何克服這個問題?
問候,
斯特凡
可以為服務器和客戶端上需要的類(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;}
}
文件:TestPresentationModel.ITestInterface.shared.cs
public partial class TestPresentationModel : ITestInterface {
// can be empty cause the interface implementation is in TestPresentation.cs
}
一種可能性是讓您的客戶端實體實現此接口。 這就是我所做的。 將一個文件添加到您的Silverlight應用程序中,該文件與您的實體位於同一命名空間中,然后僅擴展這些實體(它們均在部分類中定義):
namespace My.Model.Namespace
{
public partial class TestPresentationModel : TestInterface
{
...
}
}
然后,只有您的客戶端實體具有此接口,因此可能不是您要拍攝的,但對我來說效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.