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

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -