c# - Need a good architecture for rules of validations -
i hope guys can give me suggestions. thinking how make architecture c# development. trying best explain scenarios because im not @ english:
1) 2 classes: blue bank , red bank
2) third class : rules of validations
3) blue , red banks have several fields (values) such accountnumber, amount, invoiceperiod etc... example here (xml):
blue bank
<accountnumber>maria 5987593457</accountnumber> <amount>200.00</amount> <invoiceperiod>1</invoiceperiod>
red bank
<accountnumber>8529458</accountnumber> <amount>300.00</amount> <invoiceperiod>0</invoiceperiod>
red/blue banks have same rules of validations amount fields must numberic. red/blue banks have different rules of validations -- accountnumber field must alphanumberic in blue bank whereas accountnumber must numberic in red bank otherwise failed. invoiceperiod field must default 1 in red bank whereas must default 0 in blue bank otherwise failed.
my idea is:
i want create each class of red / blue bank different rules of validation , create rules of validation class same rules blue/red banks have.
my code here:
blue bank class:
- validate accountnumber must alphanumberic otherwise failed
- validate invoiceperiod must default 1 otherwise failed
red bank class:
- validate accountnumber must numberic otherwise failed
- validate invoiceperiod must default 0 otherwise failed
rulesofvalidation class
- validate amount must numberic (same rules both red / blue bank classes)
how work dictonary <,> these classes? or better suggestion example codes?
your highly appreciated.
you should use system.componentmodel.dataannotations
first create abstract class bank
abstract class bank { #region fields private list<string> errormessages = new list<string>(); #endregion #region publioc methods public virtual void validate() { validaterulesatributes(); } #endregion #region helper methods private void validaterulesatributes() { var validationcontext = new validationcontext(this, null, null); //validationcontext -> reference system.componentmodel.dataannotations var result = new list<validationresult>(); validator.tryvalidateobject(this, validationcontext, result, true); result.foreach(p => { errormessages.add(p.errormessage); }); if (errormessages.any()) throw new exception(errormessages.aggregate((m1, m2) => string.concat(m1, environment.newline, m2))); } protected void validate(list<string> messages) { if (errormessages == null) errormessages = new list<string>(); if (messages != null) messages.foreach(p => { errormessages.add(p); }); validaterulesatributes(); } #endregion #region properties //abstract indicate validation atributes public abstract string accountnumber { get; set; } public abstract double amount { get; set; } public abstract int invoiceperiod { get; set; } #endregion }
second create red , blue bank data anotations
class bluebank : bank { //all ok, no validate public override string accountnumber { get; set; } public override double amount { get; set; } public override int invoiceperiod { get; set; } } class redbank : bank { [required()] public override string accountnumber { get; set; } public override double amount { get; set; } [range(0,0)] public override int invoiceperiod { get; set; } public override void validate() { list<string> errors=new list<string>(); if (accountnumber != "test") errors.add("worng account"); base.validate(errors); } }
use atributes validate ranges, required,etc.. override validate() complex validations
this simple example
class program { static void main(string[] args) { redbank red = new redbank(); red.accountnumber = "test"; red.amount=0; red.validate(); //this no fail red.invoiceperiod = 3; //this fail; red.validate(); red.accountnumber = "pd"; red.validate(); //this fal: } }
Comments
Post a Comment