I don’t know is it possible to do. I have two classes:
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
}
}
}
And here second class:
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;
}
}
}
So my question is how can I call manualSetLocationStr() in contructor of Document? I need this to avoid second retrieving information from DB in locationStr get accessor, ’cause I already have it (this is field3 in Document contructor).
Any advice, any thoughs will be very appreciated.
Strongly hope for your help!
Thanks in advance!
Why can’t the
field3value be stored as a property ofDocument? Also, it is more standard for view models to contain an instance of their model, rather than derive from them.Update
It depends on your architecture, but you could create a
DocumentInfotype for example which has all of the properties required for your view. Then you need to populate a collection of these for your view, you can use aDocumentInfoRepositorywhich provides the abstraction over the data access. This repository can either be accessed directly in your entity or from your controller, depending on the type of your domain model and the patterns you wish to employ.