2012-07-21 24 views
0

我試圖在我使用一個商業ORM的小項目上使用IoC。添加僅由接口表示的對象到ORM生成的實體集合

在我的實體'項目'中有'用戶'實體的集合。當我將接口傳遞給方法時,我無法將eobject添加到'User'實體的集合中,因爲它由一些ORM EntityCollection類表示。

例子:

//'this' is a partial class to the modeled Entity<int> 
public void AddToCollection(IUser user) 
{ 
    this.Users.Add(user); //this.Users is type of EntityCollection. 
} 

我在這裏看到了兩種可能性,我不知道哪一個是最好的做法。

  1. 更改EntityCollection到IEnumerable的(這是不可能的)
  2. 演員IUSER到用戶

,或者可以我會完全錯誤的室內用的方式IoC的,什麼是最好的做法嗎?

+1

By IoC do you mean you're using dependency injection and a container?如果你正在通過你的ORM加載和保存'User'實例,那麼爲什麼你有一個'IUser'接口,以及它如何與使用DI容器相關,目前還不清楚。 – Lee 2012-07-21 15:50:28

回答

0

至於我,它不是一個很好的使用接口的實體。更喜歡基類。 最好的方面

+1

構建存儲庫層時,爲實體使用接口會付出代價。然後,您可以針對不同的ORM擁有多個存儲庫實現。另一方面,爲EF,Linq2SQL和NH生成的實體具有相同的基類真的很困難(特別是當實體自動生成時)。 – 2012-07-21 16:04:26

0

演員。例如:

//'this' is a partial class to the modeled Entity<int> 
public void AddToCollection(IUser user) 
{ 
    if (user is User) 
     this.Users.Add((User)user); 
    else 
     throw new ArgumentException(); 
} 
相關問題