Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between the Repository Pattern and the View Model Pattern

I am trying to create a site using the ASP MVC Framework. Some of the documentation use the IRepository pattern to abstract the information being sent to the view for rendering, while others recommend using a ViewModel (as in MVVC).

What is the difference?

Aren't these the same concept?

Thanks in advance

like image 639
Mark Sayewich Avatar asked Mar 21 '10 13:03

Mark Sayewich


2 Answers

The Repository pattern has more to do with how data is persisted and retrieved from the database, while the ViewModel pattern is a UI pattern that defines how to bind data to the UI. One is at the database level, while one is at the UI level, so they're completely different in that way.

Read this for Repository Pattern, and read this for MVVM.

like image 83
David Morton Avatar answered Nov 15 '22 12:11

David Morton


The Repository and ViewModel patterns work perfectly together: the ViewModel has access to a Repository and the View is bound to ViewModel.

Repository -> ViewModel -> View

Some examples may skip the ViewModel part, but using Repository directly in the View in a real application is a bad idea in the matter of separation of concerns.

like image 30
Artur Avatar answered Nov 15 '22 14:11

Artur