asp.net mvc - Where to put validation for changing an object's children? -


i using asp.net mvc 2 nhibernate. have sales class. sales has many payments. sales' payments should not modified once sales' status becomes confirmed. need suggestions on how enforce this.

i'm stumped on several things when trying put validations:

  • for adding , deleting payments:
    • i can create addpayment , deletepayment methods in sales, must remember use these instead of adding , deleting payments collection directly
    • i don't want hide payments collection because nhibernate needs it, collection used in other parts of software
  • for modifying existing payments:
    • i don't think should put validation in payments setters because nhibernate needs access setters.
  • should throw exception? there's discussion disadvantages of throwing exception prevent object entering invalid state. in case object state after modification may still valid, want block modification. throwing exception reasonable in case? alternatives?

should enforce in controller actions?

you can hide collection making protected or private. nhibernate still find it. alternatively have collection getter return immutable collection when sales status restricted value.

private iset<payment> _payments;  public virtual iset<payment> payments {         {         if (status == salesstatus.confirmed)             return new immutableset<payment>(_payments);          return _payments;     }     private set { _payments = value; } } 

putting validation rules in setters fine. can tell nhibernate access backing field directly (you may have add backing field if you're using auto properties).

<property name="_name" access="field"/> <property name="description" access="nosetter.camelcase-underscore"/> 

edit additional question...

i don't tend throw exceptions until right before save. throwing them earlier, in property setter, can annoying ui developers have go user 1 error @ time. mvc applications too, i've been using system.componentmodel.dataannotations validation attributes of late. while limited in respects, work mvc model checks @ browser , in controller. if use those, however, recommend creating custom interceptor check them right before save. interceptor throw exception if awry.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -