c# - Data validation in Entity Framework -


i'm using entity framework asp.net web pages (razor 3). have validation code ready, i'm using entity frameworks own validation process. there problem, example there decimal field named amount in model , database. use this:

item.amount = decimal.parse(request.form["itemamount"]); 

here problem, if input non numeric, error (obviously). can fix checking if input numeric if that, there many validation checks way. mean, check if it's numeric first in code, entity framework checks value again i'm using 2 different validation process, seems bad me.

ofcourse there client side too, maybe can use numeric textboxes, still i'm not sure.

any ideas?

here full code, it's horrible, i'm pretty new entity framework (like 3-4 days):

if (ispost) {     try     {         worker curworker = new worker();         try         {             curworker = m.workers.find(decimal.parse(request.form["workerid"]));         }         catch (exception)         {             errors += "lütfen bir personel seçin. <br />";         }          overhour.worker = curworker;         overhour.phaseid = curphase.phaseid;          try         {             overhour.overhouramount = decimal.parse(request.form["overhouramount"]);         }         catch (exception)         {             errors += "süre (saat) alanı sayısal olmalıdır. <br />";         }          overhour.overhourdate = datetime.today;          curworker.overhours.add(overhour);          accounting accounting = new accounting();         accounting.accountingmethod = 0;         accounting.accountingnote = curworker.workerfullname + " adlı personelin, " + overhour.overhouramount + " saat süreli, " + datetime.today.toshortdatestring() + " tarihli mesai kaydı.";         accounting.accountingtype = 3; // maaş         accounting.accountingborc = 0;         accounting.accountingalacak = curworker.workeroverworksalary * overhour.overhouramount;          accounting.phases = curphase;          curworker.accountings.add(accounting);          if (errors != "")         {             throw new wrongvalueexception(errors);         }          m.savechanges();         response.redirect(page.parentpage);     }     catch (dbentityvalidationexception ex)     {         errors = kstatic.getvalidationerrors(ex.entityvalidationerrors, "<br />");     }     catch (wrongvalueexception ex)     {         errors = ex.message.tostring();     }     catch (exception ex)     {         errors = "bilinmeyen hata, teknik detaylar: " + ex.message;     } } 

what i'd make extension method, example these two.

    public static decimal asdecimal(this string value) {     decimal result;     return decimal.tryparse(value, out result) ? result : 0; }  public static decimal? asnullabledecimal(this string value) {      decimal result;     bool isvalid = decimal.tryparse(value, out result);      if (isvalid)     {         return result;     }     else     {         return null;     }  } 

then check value of decimal this.

decimal? value = request.form["workerid"].tostring().asdecimal()  if (value.hasvalue) //asdecimal overloaded can make return 0 if wish  //dome else //do something. 

i think should point in right direction. on note client validation bad users can change using tools developer tools. don't rely on client validation.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -