c# - In repository pattern, should I use database model as my View model or should I create separate ViewModel for that? -


self explanatory, have model map 1:1 db example:

public class user {     [key]     public int userid { get; set; }     public string firstname { get; set; }     public string lastname { get; set; } } 

is there downside if use in view cshtml example :

@model user 

or should create viewmodel this

public class userviewmodel {     [required]     public string firstname { get; set; }     [required]     public string lastname { get; set; } } 

and bind view

@model userviewmodel 

the idea of programming view should not know data comes ... using database model in view breaks sign.

the best use viewmodel, , in future pleased choice example, view might 1:1 database table, imagine designer wants add recent "messages", that's new table call based on user...

with viewmodel can add , edit view , controller, if use 1:1 need create brand new viewmodel... then, views have viewmodels , don't... messy!

to out, can use automapper viewmodels construction, automapper automagically populates destination class data original class.

var userviewmodel = automapper.mapper.map<userviewmodel>(user); 

keep in mind separate concerns, controller should not know data comes (hence, using repositories) , view should not data manipulation (hence controller).


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -