[RESOLVED]ViewModel and Repository question

I’m new to the Repository pattern, struggling to understand how everything should tie together.

Repository

void IEnumerable<Order> GetAllOrders()

public class OrderViewModel {

OrderId,

ClientId,

ClientName <—-this does not exist on the Order table

OrderDescription

}

I’m using the OrderViewModel with Kendo UI grid.

Here’s the questions.

1. I’m returning the data model in Repository eg. GetAllOrders. Is this correct? or should I return OrderViewModel?

2. If I have extra fields that doesn’t belong to the Order Table, eg. Client Name (this exists in the Client table). But I need to map it to the Kendo Ui Grid.

    Where would I fill it in? I can’t fill it in the Repo, because it only returns the Order table. Would I need to do another select in the controller to fetch that Client Name?

ViewModels belong to the presentation layer. Your repository should know nothing about them. I would include the ClientName as part of the GetAllOrders method. Then you populate your ViewModel in the controller. I assume the model you pass to the View itself
will contain an Enumerable<OrderViewModel>.

I’m simply returning the Order table in thebGetAllOrders.
The client name actually belongs to another table named Client.
How would you return data from both tables?

Use Include to include data from other tables: http://msdn.microsoft.com/en-gb/data/jj574232.aspx

Then map the fields in the controller?

Yes. You could use AutoMapper for that: http://automapper.org/.

Leave a Reply