我不知道是否可以做。我有两个类:从基础班的儿童班的访问
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