2013-05-01 33 views
1

我在這裏有一個問題,我正在撞牆。不確定如何建模和體系結構這一個

我有一個對象,是我的模型的一部分。讓我們稱之爲MyObject。它具有以下特性:

  • Property1
  • Property2
  • Property3
  • Property4.1
  • Property4.2
  • Property4.3

前三個屬性獨立。第4,第5和第6屬於一起,它們是一組,我們稱之爲Properties4Set。

Properties4Set取決於屬性1,2和3. 所以如果這些值中的任何值發生改變,整個集合必須重新計算。

計算工作原理如下: 我有一個文件,其中我有一個Property1和默認的Properties4Set之間的映射。 因此,基於Property1,我必須加載這個默認設置,之後應用Property2和3的規則。

我有一種mvvm應用程序在這裏進行。我創建了一個可以創建我的域對象並使用正確的值返回的存儲庫。 一旦我想改變Property1,我的問題就開始了。因爲這意味着我需要一個新的默認Properties4Set。

目前我看到兩個選項來處理這個問題。

選項1: 實施「GetNewDefaultSet(property1)」方法到存儲庫中,並將存儲庫注入MyObject。一旦Property1變化,加載從存儲庫一組新的,重新計算和基於2和3

選項2的值: 實現一個「GetAllDefaultSets()」方法到庫中並注入Properties4Sets的整個集合到MyObject來。 Property1更改後,從列表中加載相應的設置。

我先選擇了option1,但後來發現如果將存儲庫注入到您的域對象中,那麼設計就會很糟糕。他們不應該關心如何獲得他們需要的數據。 我對option2的問題是,如果對象一次只需要一個集合,那麼注入整個集合列表似乎有點矯枉過正。

那麼,該怎麼處理呢?你看到另一種選擇嗎?

編輯

這是不好的,你有沒有具體的實施例子在那裏。

確定這裏是一些代碼:

/// <summary> 
/// Just a DTO for how the set is actually stored, can be fetched from the repository 
/// </summary> 
public class MySetMapping 
{ 
    private int value1; 
    private float value4; 
    private float value5; 
    private float value6; 

    public MySetMapping(int value1, float value4, float value5, float value6) 
    { 
     this.value1 = value1; 
     this.value4 = value4; 
     this.value5 = value5; 
     this.value6 = value6; 
    } 

    public int Value1 
    { 
     get { return this.value1; } 
    } 

    public float Value4 
    { 
     get { return this.value4; } 
    } 

    public float Value5 
    { 
     get { return this.value5; } 
    } 

    public float Value6 
    { 
     get { return this.value6; } 
    } 
} 

他是一組類:

public class MySet 
{ 
    private float value4; 
    private float value5; 
    private float value6; 

    public MySet(float value4, float value5, float value6) 
    { 
     this.value4 = value4; 
     this.value5 = value5; 
     this.value6 = value6; 
    } 

    public float Value4 
    { 
     get { return this.value4; } 
    } 

    public float Value5 
    { 
     get { return this.value4; } 
    } 

    public float Value6 
    { 
     get { return this.value4; } 
    } 
} 

這裏實際的對象,我需要:

/// <summary> 
/// the main business object, has actually more properties 
/// </summary> 
public class MyObject 
{ 
    private int value1; 
    private int value2; 
    private int value3; 
    private MySet mySet; 

    public MyObject(int value1, int value2, int value3) 
    { 
     this.value1 = value1; 
     this.value2 = value2; 
     this.value3 = value3; 

     //needs something to get the correct set for the three values 
    } 

    public int Value1 
    { 
     get { return this.value1; } 
     set 
     { 
      this.value1 = value; 
      //adjust set 
     } 
    } 

    public int Value2 
    { 
     get { return this.value2; } 
     set 
     { 
      this.value2 = value; 
      //adjust set 
     } 
    } 

    public int Value3 
    { 
     get { return this.value3; } 
     set 
     { 
      this.value3 = value; 
      //adjust set 
     } 
    } 

    public float Value41 
    { 
     get 
     { 
      return this.mySet.Value4; 
     } 
    } 

    public float Value42 
    { 
     get 
     { 
      return this.mySet.Value5; 
     } 
    } 

    public float Value43 
    { 
     get 
     { 
      return this.mySet.Value6; 
     } 
    } 

也許你現在可以更好地理解它。

+0

如果您有互相依賴和有些相關的幾個屬性,沒有那個戒指給你任何鐘聲?它不應該是一個單獨的課程嗎?一個很好的例子是地址 - 街道,城市等...您不會將這些單獨存儲爲用戶的字段,對吧?這與你的場景不一樣嗎? – walther 2013-05-01 12:33:39

+0

實際上,我已經將4.1,4.2,4.3分爲另一類。但這並沒有改變我的問題。因爲現在我仍然必須從某處獲取property1的正確對象。 – Egi 2013-05-01 12:44:03

回答

0

在這裏沒有具體的實現例子是很糟糕的。很難想象什麼是Property4Set,或者它是如何從存儲庫獲取數據的。當Property2或Property3發生變化時會發生什麼。你期望什麼是默認值。

從目前的信息來看,依然依賴於情況,我認爲這個建設者模式對你有好處。

例子:

public class MyObjectBuilder{ 
    // inject the repository 
    public MyObjectBuilder(IMyObjectBuilderRepository repository) 

    private Property1 property1; 
    public void SetProperty1(Property1 prop){} 

    public MyObject GetMyObject(){ 
    MyObject result = new MyObject(); // optional to use constructor injection 
    result.Property1 = this.property1; 

    // based on this.property1, set the Property4Set object 
    Property4Set property4 = repository.GetProperty4Set(this.property1); 

    result.Property4 = property4; 
    return result; 
    } 
} 
相關問題