2011-07-13 68 views
0

我不知道是否可以做。我有兩個類:從基礎班的兒童班的訪問

public class Document 
    { 
    public uint location; 

    public Document(uint documentId) 
    { 
     // Here complex logic of retrieving information from DB follows 
     OracleCommand documentCommand = new OracleCommand("select field1, field2, field3 from table(usr.common_pck.GetDocument(:pDocumentId))", conn); 
     documentCommand.Parameters.Add("pDocumentId", documentId); 
     OracleDataReader documentReader = documentCommand.ExecuteReader(); 
     if (documentReader.HasRows) 
     { 
      this.id = documentId; 
      this.serial = documentReader.GetString("field2"); 
      this.location = Convert.ToUInt32(documentReader.GetInt32("field1"));  
      // Here I want to call "manualSetLocationStr(field3)" of DocumentViewModel 
     } 
    } 
} 

在這裏,第二類:

 public class DocumentViewModel : Document { 
      private OracleConnection connection; 
      private string _locationStr { get; set; } 

      protected void manualSetLocationStr(string value) 
      { 
       _locationStr = value; 
      } 

      public string typeStr { get { return ((Dictionary<int, string>)HttpContext.Current.Application["DocumentTypesList"]).Single(mbox => mbox.Key == type).Value; } } 

public string locationStr { 
       get { 
        if (_locationStr == null) { 
         OracleCommand getNameCommand = new OracleCommand("select usr.common_pck.GetName(:id) as name from dual", connection); 
         getNameCommand.Parameters.Add("id", this.location); 
         OracleDataReader NameReader = getNameCommand.ExecuteReader(); 
         NameReader.Read(); 
         _locationStr = NameReader.GetString("name"); 
        } 
        return _locationStr; 
       } 
      }  
     } 

所以我的問題是我怎麼能叫manualSetLocationStr()在文檔的構造器?我需要這樣做以避免從locationStr獲取訪問者的數據庫中檢索信息,因爲我已經擁有了它(這是文檔構造器中的field3)。 任何建議,任何雖然將非常感激。 非常希望你的幫助! 在此先感謝!

回答

1

爲什麼field3值不能作爲Document的屬性存儲?而且,對於視圖模型來說,包含模型實例更爲標準,而不是從它們派生出來。

更新

這取決於你的架構,但你可以創建一個DocumentInfo類型例如它擁有所有你需要查看的屬性。然後,您需要爲您的視圖填充這些集合,您可以使用提供數據訪問抽象的DocumentInfoRepository。該存儲庫可以直接在您的實體或您的控制器中訪問,具體取決於您的域模型的類型和您希望使用的模式。

+0

「文檔」類不僅適用於視圖。它由另一個應用程序使用。所以我不想用這個渲染視圖所需的其他東西來混淆這個實體。實際上'field3'是'field2'的字符串表示。所以'field2'是'field3'的關鍵。這就像'KeyValuePair'。 – kseen

+0

您將需要創建一個新類型,然後專門爲該視圖提供這個附加屬性,並且根本不使用文檔檢索代碼。 – devdigital

+0

謝謝@devdigital!你能舉個例子嗎? – kseen

0

看到您對視圖模型類的OracleConnection屬性如何 - 它看起來似乎是錯誤的。假設你已經創建了DocumentViewModel類,我強烈要求你將它填充到控制器中,並從中省略OracleCOnnection。您可以使用AutoMapper來代替從Document繼承,而不是從Document繼承。這涉及兩個簡單的步驟:

你這樣做每個應用程序啓動一次:

AutoMapper.Mapper.CreateMap<Document, DocumentViewModel>(); 

然後在控制器使用它:

var viewModel = AutoMapper.Mapper.Map<Document, DocumentViewModel>(documentInstance); 

這將創建DocumentViewModel的實例,並儘可能多的地圖屬性,因爲它可以。通過這樣做,您可以避免從Document類繼承。

至於你的視圖模型的結構。而不是有一個方法typeStr,有一個屬性TypeStr在控制器操作中自動映射後設置。你的typeStr方法也一樣。

視圖模型的整體思想是爲UI提供一個愚蠢的數據呈現的可能表示。所以理想情況下,沒有方法調用,沒有延遲加載(應該預加載)。

您還可以使用AutoMapper您發佈的視圖模型映射回文檔實例在POST方法,像這樣:

添加反向映射到應用程序啓動:

AutoMapper.Mapper.CreateMap<DocumentViewModel, Document>(); 

並在控制器:

[HttpPost] 
public ActionResult Edit(DocumentViewModel viewModel) 
{ 
    var document = AutoMapper.Mapper.Map<DocumentViewModel, Document>(viewModel); 
} 

希望這會有所幫助。

編輯:回答你的問題。您可以在自動映射之前調用Document實例上的manualSetLocationStr()以查看模型。

+0

實際上viewmodel類中沒有連接字段。這只是例如。 – kseen

+0

關於AutoMapper的性能,你可以說些什麼?因爲我的項目似乎非常龐大,所以最好只使用原生的東西來解決這個問題。 – kseen

+0

請問您可以提供一些關於您的問題編輯的例子嗎?非常感謝您的幫助。 – kseen