asp.net-mvcmodel-view-controllerdatamapperasp.net-mvc-viewmodeldata-layer

Return view model directly from DAL to improve performance in MVC?


The pattern I always see in MVC is to load the domain model (or repository) from the database and then map that into a view model.

I have a question on this pattern - when you have a situation where the view model contains only a few properties out of a large domain model object. And because it's a website which might get high traffic, this seems like it's not the ultimate solution performance-wise, it's like doing SELECT * FROM TABLE, instead of just selecting the columns required.

The alternative I could think of is that the DAL would return a view model instead of a domain model, but I've never seen such a pettern used before, is this a bad pattern to adopt? Is there any other pattern? Or should I just ignore that overhead of loading a big domain model even though I need only several properties out of it?


Solution

  • That's really opinion based question, but i was on HighLoad++ conference where was Marco Cecconi one of Stack Exchange developers with his presentation Stack Overflow - It's all about performance!.

    As you know stackoverflow was written with ASP.NET MVC and Marco said that SO project has plain ADO requests right in controller. And they write it like this becouse of perfomance. There are troubles with C# GC when you have lots of requests and create many intermediate objects (DAL model, BL model, ViewModel etc..).

    So if they are doing it like that in the sake of perfomance why you can't do it?