views:

43

answers:

1

Hello all,

Been around here for a while but this is my first question @ so.

Scenario: Mvc site. Viewmodels for most pages. Each viewmodel contains models or iqueryables acquired from different repositories.

Each source is updated frequently (from outside the scoop of the site) so even if caching local it will be a lot of datasource hammering.

The problem we're facing is that when a viewmodel is created using more then one repository each of them trigger a connection/fetch to the datasource (if they need to repopulate).

This could easily be avoided by returning a complete viewmodel from the repository. Then the repository could use a single connection (in most cases even with only one sql stored procedure) to fetch all needed data.

Someone mentioned that viewmodels should not be involved in repositories. I don't actually see any problems with this so my question is what could be the impact?

A: 

With MVC there are no hard and fast rules - you should always do whatever fits in with your particular scenario. It is unusual to return complete view models from the repository layer, but if populating it is a particularly database intensive set of actions that could be simplified into a single stored procedure then why not.

There is always a potential that the method for retrieving the view data is repeating code that you have elsewhere, and therefore contradicting DRY leading to potential maintenance issues later.

If you find that retrieving all the data that you need for a single page requires multiple calls to multiple different repositories this suggests that your repositories are perhaps not modelled suffiently to meet your requirements and this may be a better place to look at reducing the number of database connections/fetches.

Clicktricity
Yes, that's about as far as I came too. I'll accept this answer, Thank you.
Simon Karlsson