Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice/Pattern to Populate 'ViewModel' objects

Folks,

I'm trying to figure out a pattern or best practice to translate data from the domain objects/data source over to the 'ViewModel' object. In my particular example, the domain object is the WCF proxy objects. Any call to the WCF methods will populate these proxy objects which finally gets transferred over to the 'ViewModel' objects. See Architectural diagram

This microsoft article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx talks about using a Repository pattern. It's mentioned under 'The Data Model and Repository' section. Is that the best approach? Can someone advice me?

dorman

like image 340
dormantroot Avatar asked Dec 29 '25 03:12

dormantroot


2 Answers

Repository pattern is related to data access layer (what is inside your WCF). This has nothing to do with presentation layer.

I would suggest you declare your Model and ViewModel classes in MVC project based on the requirements for the UI (without taking your database schema into consideration). You can then use AutoMapper to simplify the code needed to copy values from WCF proxy classes to your Models. You might want to encapsulate this code in an Adapter class that will shield the rest of app from AutoMapper code.

EDIT:

Do not use your WCF proxies as model classes. Declare separate model classes based on your UI and use AutoMapper to copy data between them.

like image 74
Jakub Konecki Avatar answered Dec 30 '25 18:12

Jakub Konecki


Yes, you need to have a repository. That will give you some advantages, like easy testing of the view model; also an easy switching to another kind of repository later.

In terms of the article you provided, - it gave them an advantage of unit-testing the view-model logic; and, later on, they can switch from XML files repository to the SQL server repository (as an example).

Just make sure to have abstract repository injected into ViewModel.

like image 27
Tengiz Avatar answered Dec 30 '25 17:12

Tengiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!