Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC create a view model from multiple domain models distantly related
    text
    copied!<p>I have been searching for a way to join 2 distantly related domain models into one view model with no luck.</p> <p>I am working on an existing application and have been asked to add a field to a result of fax log search. My controller is returning a viewModel, and I am just wanting to add an additional field to it. Which sounds like it should be an easy task.</p> <p><strong>Background Info:</strong></p> <p>This is the original viewModel:</p> <pre><code>public class VOEIFaxLogSearchListViewModel { public DateTime DateTimeAdded { get; set; } public string Processor { get; set; } public string FaxStatusCode { get; set; } public string VendorOrderID { get; set; } public string FromFaxNumber { get; set; } } </code></pre> <p>I want to add an additional field to this viewmodel:</p> <pre><code> public string CustomerName { get; set; } </code></pre> <p>The way that the application is designed, a stored procedure is called to return a dataset of the search results. (I won't the search method (GetFaxLogSearchResult) or its SQL call as it isn't necessary)</p> <pre><code>var resultFaxDS = vOEIDAO.GetFaxLogSearchResult(startDate,endDate,userName,faxType); </code></pre> <p>The resulting DataSet is then converted to a DataTable.</p> <pre><code>DataTable faxTable = resultFaxDS.Tables[0]; </code></pre> <p>A For loop interates through each of the result records and puts them into a Domain Model named FaxModel. Which is mapped with Automapper to the viewModel named FaxLogSearchListViewModel.</p> <pre><code>for (int i=0; i&lt;faxTable.Rows.Count;i++) { var row = faxTable.Rows[i]; var faxLogModel = vOEIDAO.DRToFaxModel(row); faxViewModel.Add(Mapper.Map&lt;FaxModel,FaxLogSearchListViewModel&gt;(faxLogModel)); } } return faxViewModel; } </code></pre> <p>Here is what I have done so far to add this result field:</p> <p>1) added the new property to the view model.</p> <p>2) modified stored procedure that pulls back the search results so it returns CustomerName in the dataset</p> <p><strong>The dilemna:</strong></p> <p>The method adding each row of the dataset into the Domain model (DRToFaxModel) is doing just that... it is populating a domain model(FaxModel). The field that I want to add isn't in the Domain model. As a result, I don't want to add a field to the domain model if it doesn't belong to the concrete class.</p> <p>Here is the domain model and the method used to populate it with each row from the search results:</p> <pre><code>public class FaxModel { public int FaxID { get; set; } public int FaxStatusID { get; set; } public string ToFaxNumber { get; set; } public string FromFaxNumber { get; set; } public DateTime DateTimeAdded { get; set; } public string FaxStatusCode { get; set; } public string Processor { get; set; } public string VendorOrderID { get; set; } } public FaxModel DRToFaxModel(DataRow dr) { FaxModel voObj = new FaxModel(); voObj.FaxID = GetVOInt(dr["FaxID"]); voObj.FaxStatusID = GetVOSmallInt(dr["FaxStatusID"]); voObj.ToFaxNumber = GetVOStr(dr["ToFaxNumber"]); voObj.FromFaxNumber = GetVOStr(dr["FromFaxNumber"]); voObj.DateTimeAdded = GetVODateTime(dr["DateTimeAdded"]); voObj.FaxStatusCode = GetVOStr(dr["FaxStatusCode"]); voObj.Processor = GetVOStr(dr["Processor"]); voObj.VendorOrderID = GetVOStr(dr["VendorOrderID"]); //Cant add CustomerName to the model without modifying the FaxModel domain model. //Shouldn't do that because it is a domain model. //CustomerName is in the CustomerModel domain Model // voObj.CustomerName = GetVOStr(dr["CustomerName"]); return voObj; } </code></pre> <p>So currently, my ViewModel with the added CustomerName property is returned with a null for CustomerName.</p> <p>My domain models are distantly related. In the database the FAX table can be joined joined to the CUSTOMER table but only by joining through an ORDER table. (the FAX table has an orderID field and the ORDER table has a CustomerID field)</p> <p>So my resulting question is: how do you use autoMapper to map a Fax domain model to a Customer domain model since the 2 domains don't have any common fields to build the relationship without joining through another table?</p> <p>Or can you map more than 2 tables into 1 viewModel using automapper? how is this done?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload