我不知道是否可以做。我有兩個類:從基礎班的兒童班的訪問
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
)。 任何建議,任何雖然將非常感激。 非常希望你的幫助! 在此先感謝!
「文檔」類不僅適用於視圖。它由另一個應用程序使用。所以我不想用這個渲染視圖所需的其他東西來混淆這個實體。實際上'field3'是'field2'的字符串表示。所以'field2'是'field3'的關鍵。這就像'KeyValuePair'。 – kseen
您將需要創建一個新類型,然後專門爲該視圖提供這個附加屬性,並且根本不使用文檔檢索代碼。 – devdigital
謝謝@devdigital!你能舉個例子嗎? – kseen