asp.net mvc - Validation on my domain objects or in the view model? Where should it go? -


i trying keep service layer free of asp.net mvc dependcies have ran problem. want use compare part of asp.net mvc library. should do?

i have domain class(that later used fluent nhibernate)

public class user() {    [required(errormessage = "required")]    [compare()] // can't because not user domain needs know mvc ,   rather not    public virtual string email (get; set;}    public virtual string confirmemail (get; set;} // not mapped fluent nhibernate }  public class userviewmodel() {    public user user {get; set;} }   public actionmethod method() {   if(!this.modelstate.isvalid)   {     } } 

i have domain object made find kinda pointless move properties usersviewamodel put validation on them later on through domain model used commit(nhibernate commit)

you should use "buddy class" model class hold validation attributes:

// note partial keyword below:  full definition of mymodel  // can put in file in model [metadatatype(typeof(mymodelmetadata))] public partial class mymodel {}  public class mymodelmetadata {     //properties validation attributes (dataannotations)  go here... } 

the validation attributes 1 item not cleanly split between mvc concerns: relate client validation in views server validation in domain, @ point data stored database , therefore needs validation.

so @jfar, saying should go in viewmodel, wrong person says should in domain.

in fact, imho, totally incorrect put validation attributes in viewmodel. viewmodel should container data sent view. should not port logic of own. logic, in case validation attributes, should defined outside of viewmodel , buddy class best option that, if using automated orm, not in special case.

this question illustration of fact not every issue maps neatly mvc, getting stalinistic validation attributes should go plain wrong.

my advice: put them in 1 place, separate other concerns. way use buddy classes.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -