2017-07-04 28 views
2

我正在爲支持足球隊從應用程序收集統計數據的應用程序創建web api。我現在正在執行階段。而且可以說,我在想什麼(如果需要的話)的設計模式的類型將是最好的東西是這樣的:哪種設計模式最適合足球比賽的應用程序

public class Shot 
{ 
    public int Id { get; set; } 
    public int PlayerId { get; set; } 
    public string Comment { get; set; } 
    public bool OnGoal { get; set; } 
    public int GameId { get; set; } 

} 

public class Card 
{ 
    public int Id { get; set; } 
    public int PlayerId { get; set; } 
    public string Comment { get; set; } 
    public bool IsRed{ get; set; } 
    public int GameId { get; set; } 
} 

正如你可以看到一些性質相同。它應該用接口,繼承(f.e. class Action)來實現,或者我應該使用設計模式之一(哪一個)?實體框架最好能避免後期的問題?

+0

爲什麼卡沒有遊戲ID? –

+0

只是錯過了它。我編輯問題 –

回答

2

好吧,你的班級都代表某種遊戲事件 - 射擊和卡片。可以有其他一些比賽項目,比如任意球,投入,替補,罰球或者角球。所有這些事件都應該有ID,遊戲ID,玩家ID,時間戳和可能的評論。所以你的問題是幾個類中的數據重複。它很容易通過繼承來解決。無需模式:

public abstract class GameEvent 
{ 
    public int Id { get; set; } 
    public int GameId { get; set; } 
    public int PlayerId { get; set; } 
    public TimeSpan Time { get; set; } 
    public string Comment { get; set; } 
} 

及各種具體事件

public class Shot : GameEvent 
{  
    public bool OnGoal { get; set; } 
} 

public class Card : GameEvent 
{ 
    public bool IsRed { get; set; } 
} 

你也應該考慮節省的補時時間戳記,因爲你可以同時獲得46分鐘時間跨度(下半年開始)和45+上半場1分鐘。

+1

謝謝謝爾蓋。我打算繼承,但不確定。如果我可以再問一個問題。 我想爲實體框架使用代碼優先的方法。可以使用像'public int PlayerId'這樣的屬性,或者我應該使用'public Player player' –

+2

@MateuszSzymański,使用Player,PlayerId或這兩個屬性都可以。 –

+0

關於時間 - 這將是沙灘足球的應用程序,所以沒有像額外的時間,所以我不在乎這一點。無論如何 - 謝謝 –