2010-08-25 128 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