c# - How to return validation errors from service class methods? -


can give me example of how return validation errors service class used in web application. think approach below?

using system; using system.linq; using system.web.mvc;  using app.data; using app.security;  public interface imembershipservice {     bool validateuser(string username, string password, modelstatedictionary model = null); }  public class membershipservice : imembershipservice {     private databasecontext db;      public membershipservice(databasecontext db)     {         this.db = db;     }      public bool validateuser(string username, string password, modelstatedictionary model)     {         if (string.isnullorwhitespace(username) || username.length > 128 ||             string.isnullorwhitespace(password) || password.length > 256)         {             tryaddmodelerror(model, "username or password provided incorrect.");             return false;         }          var user = this.db.users.singleordefault(u => u.username == username);          if (user == null || !passwordhash.validate(password, user.passwordhash, user.passwordsalt))         {             tryaddmodelerror(model, "username or password provided incorrect.");             return false;         }          if (!user.isapproved)         {             tryaddmodelerror(model, "your account suspended.");             return false;         }          user.lastlogindate = datetime.utcnow;         this.db.savechanges();          return true;     }      private static void tryaddmodelerror(modelstatedictionary model, string errormessage)     {         if (model != null)         {             model.addmodelerror(string.empty, errormessage);         }     } } 

usage sample:

[authorize] public class accountcontroller : controller {     private readonly imembershipservice membershipservice;      public accountcontroller(imembershipservice membershipservice)     {         this.membershipservice = membershipservice;     }      [httppost, allowanonymous, validateantiforgerytoken]     public login(loginmodel model, string returnurl)     {         if (modelstate.isvalid && this.membershipservice.validateuser(             model.username, model.password, modelstate: modelstate))         {             formsauthentication.setauthcookie(username, true);             return redirecttolocal(returnurl);         }          return view(model);     } } 

try instead:

public bool validateuser(string username, string password) {     if (string.isnullorwhitespace(username) || username.length > 128 ||     string.isnullorwhitespace(password) || password.length > 256)     {         throw new providerexception("username , password required");     }      var user = this.db.users.singleordefault(u => u.username == username);      if (user == null || !passwordhash.validate(password, user.passwordhash, user.passwordsalt))     {         throw new providerexception("incorrect password or username");     }      return true; } 

usage of membership service:

... try {     var result = membership.validateuser(username, password);     ... } catch (providerexception e) {     model.addmodelerror(string.empty, e.message); } ... 

this way, membershipservice responsible validation , validateuser method validates username , password. done validation result user of membershipservice. referred single responsibility principle


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -