2010-02-12 32 views
0

首先,我想爲我的英語道歉,而不是我最強烈的一面。SubSonic,SimpleRepository和實體界面

回答這個問題。在我目前的項目中,我有我的實體的接口,所以我可以在我的頭部使用Subsonic屬性,並且我希望將來能夠無縫地切換O/R映射器。 無論如何,當我嘗試使用我的接口和SimpleRepositorys類(如單個<>,全部<>等)時出現錯誤。 我知道我爲什麼會收到錯誤消息,但我需要幫助才能找到解決方法。 錯誤消息:

System.InvalidCastException:無法轉換類型 'SubSonic.DomainObjects.User' 的目的爲類型 'Core.DomainObjects.IUser'。

代碼:
public IUser FindById(int id) {
var user = _repository.Single<User>(x => x.Id == id);
return (IUser)user;
}

正如你可以看到我已經試圖讓用戶以IUSER爲了工作時,我想補充的數據,但沒有成功。我能做些什麼來完成這項工作?

謝謝
Timmie

回答

0

我不認爲亞音速是在這種情況下的問題。此代碼將工作:

namespace Core.Objects 
{ 
    public interface ICustomer 
    { 
     int CustomerID { get; set; } 
     string Name { get; set; } 
    } 

} 

代碼的實際類:

namespace Business.Entities 
{ 
     public class Customer: Core.Objects.ICustomer 
     { 
      public int CustomerID { get; set; } 

      [SubSonicStringLength(50)] 
      public string Name { get; set; } 
     } 
} 

最後,函數來獲取客戶:

private static ICustomer CustomerByID(int id) 
{ 
     var repos = new SimpleRepository("Test", SimpleRepositoryOptions.None); 
     var customer = repos.Single<Customer>(c => c.CustomerID == id); 

     return (ICustomer) customer; 
} 
+0

惱人的,忘了,包括界面。 –

相關問題