0

Just imagine that I have a model with 20 fields and I have 1000 records now but my database is growing up every day, then I want to list them in a razor page and show all of them but just 5 of fields needed.

I just want to know is it OK if I return the model with all fields to my view and choose the fields I need in my view or I should create a viewModel with that fields I just need ? I am worried about the performance.

Sepehr Estaki
  • 331
  • 5
  • 19

4 Answers4

5

Definitely create a viewmodel to expose just the fields you need. Don't expose any fields that you don't need for both performance and possibly security reasons.

Harriet
  • 97
  • 3
1

Of course you could filter your data on server side or client side. But the best approach is filtering data on server side to keep your server load and network traffic as less as possible. It also makes your application more responsive and with better performance. Use IQueryable Interface for server side filtering (check here for more information).

One side effect of using server side filtering is having better security.

Community
  • 1
  • 1
Hadee
  • 1,392
  • 1
  • 14
  • 25
0

For small numbers like this, it really doesn't matter. Performance wise, you won't see much of a difference. I also would not worry about security.

What I would worry about is organization of your code. Do you want to couple your views to your database models? If that's ok with you, than go for it!

Edit: I still think the decision to return the model or not should not be based on potential performance hits, but instead about your code organization.

When you are talking about displaying a larger set of records, then you'll need a way to display them (i.e. some grid with paging ...), I would sugguest you consider keeping an IQueryable all the way. There are producs that can take an IQueryable and provide paging, sorting, searching on your data.

Grid.Mvc Telerik

hatcyl
  • 2,190
  • 2
  • 21
  • 24
  • you know, my database is growing up every day, what about the future ? I have experienced that if I want to have some conditions for select records I have to select just fields I required, but how about the select all records without any condition? just imagine next year I'll have 1,000,000 records., what do you mean about couple views to database models ? – Sepehr Estaki Nov 17 '15 at 19:01
  • A million records might be worth mentioning in the question where you say a thousand. https://www.stevefenton.co.uk/2013/03/Why-You-Never-Expose-Your-Domain-Model-As-Your-MVC-Model/ – Steve Greene Nov 17 '15 at 19:06
  • If you have a million records, the fact that you included all columns will not be your performance hit. It will be the million records. Then you need to start thinking about pagination / loading. I would remove the 1,000 part of your question though. – hatcyl Nov 17 '15 at 19:19
0

With the viewModel you can encapsulate your properties by only passing the fields that are required on the page. When you work with a large amount of fields then yes there might be a performance gain in using the ViewModel. And by using the ViewModal you keep your application clean and all the logic separated. :)

Erick Boshoff
  • 1,443
  • 2
  • 18
  • 30