2010-06-23 102 views
5

我有兩個對象MetaItems和Items。C#泛型和集合

MetaItem是對象的模板,而Items包含實際值。例如,「部門」被視爲元項目,「銷售」,「英國地區」,「亞洲地區」被視爲項目。

此外,我想維護這些元項目和項目上的親子關係。對於同一

我有下面的代碼 -

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WpfApplication12 
{ 
    public interface IEntity 
    { 
     int Id { get; set; } 

     string Name { get; set; } 
    } 

    public interface IHierachy<T> 
    { 
     IHierachy<T> Parent { get; } 

     List<IHierachy<T>> ChildItems { get; } 

     List<IHierachy<T>> LinkedItems { get; } 

    } 

    public class Entity : IHierachy<IEntity>, IEntity 
    { 

     #region IObject Members 

     private int _id; 
     public int Id 
     { 
      get 
      { 
       return _id; 
      } 
      set 
      { 
       _id = value; 
      } 
     } 

     private string _name; 

     public string Name 
     { 
      get 
      { 
       return _name; 
      } 
      set 
      { 
       _name = value; 
      } 
     } 

     #endregion 

     #region IHierachy<IEntity> Members 

     public IHierachy<IEntity> _parent; 
     public IHierachy<IEntity> Parent 
     { 
      get 
      { 
       return _parent; 
      } 
     } 

     private List<IHierachy<IEntity>> _childItems; 

     public List<IHierachy<IEntity>> ChildItems 
     { 
      get 
      { 
       if (_childItems == null) 
       { 
        _childItems = new List<IHierachy<IEntity>>(); 
       } 
       return _childItems; 
      } 
     } 

     private List<IHierachy<IEntity>> _linkedItems; 

     public List<IHierachy<IEntity>> LinkedItems 
     { 
      get 
      { 
       if (_linkedItems == null) 
       { 
        _linkedItems = new List<IHierachy<IEntity>>(); 
       } 
       return _linkedItems; 
      } 
     } 
     #endregion 
    } 


    public class Item : Entity 
    { 
    } 

    public class MetaItem : Entity 
    { 
    } 

} 

以下是我的測試類 -

public class Test 
{ 
    public void Test1() 
    { 
     MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"}; 

     MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"}; 

     Item meta3 = new Item() { Id = 101, Name = "Item 1" }; 


     **meta1.ChildItems.Add(meta3);** // this line should not compile. 
     meta1.ChildItems.Add(meta2) // This is valid and gets compiled. 
    } 
} 

在測試類,當我buidling父子關係,我可以添加項目作爲元項目對象的子對象。在這裏我想編譯錯誤生成。

有人可以幫助我做到這一點。

-Regards 拉吉

+0

如果元項目只是一個包含其他項目的項目,那麼它不是真的只是一個項目反正,其中'T = Item'? – 2010-06-23 13:52:45

+2

與您的問題無關,但對於Entity.Id和Name,自動實施的屬性是您的朋友。 – dahlbyk 2010-06-23 13:58:15

回答

0

爲什麼你不覺得這行應該編譯?它看起來完全有效。

ChildItems列表是公開的。如果你不希望他們能夠添加到列表中,那麼你將需要包裝自己的收藏或使用ReadOnlyCollection<IHierachy<IEntity>>

哦,你已經解決了你的問題。我認爲解決方案是使實體類通用。

using System.Collections.Generic; 

namespace WpfApplication12 
{ 
    public interface IEntity 
    { 
     int Id { get; set; } 
     string Name { get; set; } 
    } 

    public interface IHierachy<T> 
    { 
     IHierachy<T> Parent { get; } 
     List<IHierachy<T>> ChildItems { get; } 
     List<IHierachy<T>> LinkedItems { get; } 
    } 

    public class Entity<T> : IHierachy<T>, IEntity 
    { 
     private int _id; 
     public int Id 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     private string _name; 
     public string Name 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     public IHierachy<T> _parent; 
     public IHierachy<T> Parent 
     { 
      get 
      { 
       return _parent; 
      } 
     } 

     private List<IHierachy<T>> _childItems; 
     public List<IHierachy<T>> ChildItems 
     { 
      get 
      { 
       if(_childItems == null) 
       { 
        _childItems = new List<IHierachy<T>>(); 
       } 
       return _childItems; 
      } 
     } 

     private List<IHierachy<T>> _linkedItems; 
     public List<IHierachy<T>> LinkedItems 
     { 
      get 
      { 
       if(_linkedItems == null) 
       { 
        _linkedItems = new List<IHierachy<T>>(); 
       } 
       return _linkedItems; 
      } 
     } 
    } 


    public class Item : Entity<Item> 
    { 
    } 

    public class MetaItem : Entity<MetaItem> 
    { 
    } 

    public class Test 
    { 
     public void Test1() 
     { 
      MetaItem meta1 = new MetaItem() { Id = 1, Name = "MetaItem1"}; 
      MetaItem meta2 = new MetaItem() { Id = 1, Name = "MetaItem 1.1"}; 
      Item meta3 = new Item() { Id = 101, Name = "Item 1" }; 

      meta1.ChildItems.Add(meta3); // this line should not compile. 
      meta1.ChildItems.Add(meta2); // This is valid and gets compiled. 
     } 
    } 

} 
+0

我認爲他不想允許添加Item作爲MetaItem的子項(儘管代碼明智,都是Entity類型)。換句話說,只有MetaItem.ChildItems應該只包含其他的MetaItem。我不熟悉實體的東西,所以我的猜測是將ChildItem更改爲MetaItem的列表... – Alan 2010-06-23 13:55:10

+0

啊,是的,他已經解決了這個問題,你完全正確。您需要使Entities/Hierachies更通用。我正在修復我的解決方案。 – 2010-06-23 14:05:04

3

的代碼被編譯因爲ChildItemsIList<Entity>,其中既包括ItemMetaItem。如果你是做Entity通用:

public class Entity<T> : IHierachy<T>, IEntity where T : IEntity { ... } 

那麼就需要定義ItemMetaItem這樣的:

public class Item : Entity<Item> { } 

public class MetaItem : Entity<MetaItem> { } 

在這種情況下,他們的ChildItems將是正確的,更受限制,類型。

+0

好的,我在'Entity '內將'IEntity'的引用更改爲'T'後,我得到了它的工作。繼承自己的專業化是一項有趣的技術。 – 2010-06-23 14:18:25

+0

是的,完成'實體'變化是作爲一個練習。 :)絕對是一個有用的技術,爲特定的場景。 – dahlbyk 2010-06-23 14:20:12

+0

Hi dahlbyk, Ya。在上面添加必要的更改會使其工作完美無缺。 謝謝。 – Raj 2010-06-23 14:25:37