2012-02-21 32 views
2

在下面的例子中,我怎麼能得到SpecialThingDB返回一個SpecialThings列表,而不必像我做的那樣重新實現ThingDB的所有方法?在C#中,派生管理器類如何在不重新實現所有內容的情況下返回派生類的實例?

public class Thing 
{ 
    public readonly string Color; 
    public readonly int Weight; 

    public Thing(string color, int weight) 
    { 
     Color = color; 
     Weight = weight; 
    } 

    // Some methods 
} 

public class ThingDB 
{ 
    public List<Thing> GetByColor(string color) 
    { 
     var things = new List<Thing>(); 
     // Get things from the database's Thing table 
     return things; 
    } 

    public List<Thing> GetByWeight(int weight) 
    { 
     var things = new List<Thing>(); 
     // Get things from the database's Thing table 
     return things; 
    } 
} 

public class SpecialThing : Thing 
{ 
    public SpecialThing(string color, int weight) : base(color, weight) 
    { 
    } 

    // Some special methods 
} 

public class SpecialThingDB : ThingDB 
{ 
    // How do I have GetByColor and GetByWeight return SpecialThings here 
    // without completely re-implementing the base methods like below? 

    public List<SpecialThing> GetByColor(string color) 
    { 
     var specialThings = new List<SpecialThing>(); 
     // Get things from the database's Thing table 
     return specialThings; 
    } 

    public List<SpecialThing> GetByWeight(int weight) 
    { 
     var specialThings = new List<SpecialThing>(); 
     // Get things from the database's Thing table 
     return specialThings; 
    } 
} 

此外,有沒有更好的模式不必在我的數據庫中每個表兩班(一個代表一個記錄,另一個是一個管理器類)其他? (!感謝,the_joric)

UPDATE

給出的解決,這裏就是我改變了一下上面的代碼:

public class ThingDB<T> where T : Thing 
{ 
    public List<T> GetByColor(string color) 
    { 
     var things = new List<T>(); 
     // Do some database stuff to fill things by color 
     return things; 
    } 

    public List<T> GetByWeight(int weight) 
    { 
     var things = new List<T>(); 
     // Do some database stuff to fill things by weight 
     return things; 
    } 
} 

public class SpecialThing : Thing 
{ 
    public SpecialThing(string color, int weight) 
     : base(color, weight) 
    { 
    } 

    // Some special methods 
} 

public class SpecialThingDB : ThingDB<SpecialThing> { } 

public class ThingDB : ThingDB<Thing> { } // For backward-compatibility 
+0

也許我不完全理解你,但不應該'base.GetByColor(color).Cast ()'工作嗎? – 2012-02-21 16:09:54

+0

可能,但我希望儘可能幹,並且不要在派生類中重新實現每個方法,即使這只是使用強制類型顯式調用基本方法。 – 2012-02-21 19:23:13

回答

2

您可以使用泛型。像下面這樣:

public class ThingDB<T> where T: Thing 
{ 
    public List<T> GetByColor(string color) 
    { 
     var things = new List<T>(); 
     // Get things from the database's Thing table 
     return things; 
    } 

    public List<T> GetByWeight(int weight) 
    { 
     var things = new List<T>(); 
     // Get things from the database's Thing table 
     return things; 
    } 
} 
+0

有趣。那麼我是否也可以在這個解決方案中添加'public class ThingDB:ThingDB {}'以便不破壞對ThingDB的現有引用? – 2012-02-21 17:54:06

+0

似乎我必須添加構造函數到這樣一個派生類[額頭巴掌],但除此之外,這似乎到目前爲止... – 2012-02-21 18:05:51

0

泛型:

[the_joric打我給它在這一個]

另外:也許,是的。使用對象關係映射器爲你做這個:http://en.wikipedia.org/wiki/Object-relational_mapping

+0

是的,這是一個本土ORM(riiiiight ...)的輸出, ,大量改編的http://csharpdatatier.sourceforge.net/ – 2012-02-21 16:35:39

相關問題