mardi 14 octobre 2014

Best approach for view model creation to handle single entity and list - return IQuerable


Vote count:

0




Looking for input on the best approach/pattern to meet the following requirement for a view model class:



  • Converts an IQueryable from a repository select to a IQueryable view model query <-- Works fine

  • Converts a single instance of a db entity to a view model instance <-- is not working, returns NULL

  • Both use a single method to map db entity to view model properties to avoid mapping replication


Example of what I am attempting, but it is not working...and seems maybe a bit of a hack:



public class WorkOrderDependencyViewModel : IEntity, IViewModel<WorkOrderDependency, WorkOrderDependencyViewModel>
{
public int Id { get; set; } }
public int WorkOrderHeaderId { get; set; }
public int POHeaderId { get; set; }
public decimal RemainQty { get; set; }

//Re-use this mapping logic for both converting a query and converting a single db entity instance. Used by Kendo Grids
public IQueryable<WorkOrderDependencyViewModel> ConvertClassQueryToViewModelQuery(IQueryable<WorkOrderDependency> entityQuery)
{
var viewModelResultQuery = entityQuery
.Select(x => new WorkOrderDependencyListViewModel()
{
Id = x.Id,
WorkOrderHeaderId = x.WorkOrderHeaderId,
POHeaderId = x.PODetail.POHeaderId,
RemainQty = x.PODetail.QtyOrdered - x.PODetail.QtyReceived
}
);

return viewModelResultQuery;
}

//convert single instance of db entity to view model, but use existing mapping logic from above method
public WorkOrderDependencyViewModel ConvertClassToViewModel(WorkOrderDependency entity)
{
var entityList = new List<WorkOrderDependency>();
entityList.Add(entity);
var viewModel = ConvertClassQueryToViewModelQuery(entityList.AsQueryable()).FirstOrDefault() as WorkOrderDependencyViewModel;
return viewModel; <------ viewModel is NULL
}
}


Why is viewModel returning NULL?



asked 41 secs ago







Best approach for view model creation to handle single entity and list - return IQuerable

Aucun commentaire:

Enregistrer un commentaire