asp.net mvc - How is service validation communicated back to the controller? -
i think i'm missing fundamental here, i've been digging on 2 hours, , must overlooking obvious. if so, apologize.
i have mvc 3 site, , have service layer controllers talk to, , repository service layer talks (all decoupled using ioc container, that's irrelevant here). domain models ef4 entities, shared between controller, services, , repositories.
i have view model signing looks this:
public class signupviewmodel { [displayname("email")] [required, regularexpression(@"^.+@.+\..+$")] public string email { get; set; } [displayname("password")] [required] public string password { get; set; } [displayname("confirm password")] [required, compare("password")] public string passwordconfirmation { get; set; } }
my user domain model looks this:
public class user { public int id { get; set; } public string email { get; set; } public string passwordhash { get; set; } public string passwordsalt { get; set; } }
i have service method used register user looks this:
public void register(user user, string plantextpassword) { //make sure email address not taken. bool emailistaken = isemailtaken(user.email); if (emailistaken) { //what do here? } else { //create user. } }
and lastly, controller action tie together:
public actionresult signup(signupviewmodel signupviewmodel) { if (modelstate.isvalid) { _accountservice.register(mapper.map<signupviewmodel, user>(signupviewmodel), signupviewmodel.password); _unitofwork.commit(); } return view(signupviewmodel); }
as can see in comment in service method, do when service-level validation fails? discussed in this question, shouldn't throw exceptions. i'm not sure follow ryan suggesting, or if that's still applicable (i.e. there better ways handle mvc 3, or other tools available fluent validation).
like said, think can't see forest trees @ point, having spent last two-three hours trying figure out on own, thought may have better luck asking question.
thanks in advance.
i once tried ryan talking about. says service should this:
public class theservice{ validationresult validate(yourtype item); save(yourtype item); }
the usage first call validate , if validationresult successful call save. save validate entity again make sure aren't forgetting validate it. difference if there error when save call validate exception thrown.
personally have done way once , won't again. think throwing exception makes clearer , gives less code maintain.
to error info controller need create own exception. this:
public class rulesexception : exception{ public ienumerable<errorinfo> errors {get;set;} } public class errorinfo{ public string propertyname {get;set;} public string errormessage {get;set;} }
something along lines start. better implementation in source code of xval
note: if going exceptions try avoid writing code doing try/catch inside loop. important thing avoid when comes exceptions more expensive compared database call example it's cheap.
Comments
Post a Comment