add a new person. save it to a database. load an existing person from the database. delete a person.a rich user interface will have features that enable a user to:
edit their data, save their changes and close the application. edit their data, save their changes, do further editing, save and close. edit their data, cancel their changes, and close.and so on. in a data entry form the above is typically implemented using ok, cancel and apply buttons. this might not seem particularly problematic. however, a feature of this architecture is that the person object's state is being updated as the user types. if the user cancels their edits it is important to restore the object to the last valid state it was in prior to the edits. this means we have to keep a copy of the current state after each apply operation. in this particular example, this doesn't matter but in a more complex application, say, with collections of persons, it would. and we are trying to illustrate the general principles. the ui developer also needs to know a few other things about a person, such as whether a person is new, modified (dirty), or is currently being edited. for example, it should not be possible to load a new person from the database while one is being edited.
set{ socialsecuritynumber = value; rulebroken("social security number", socialsecuritynumber.length != 11);}in form.txtsocialsecuritynumber_textchanged event. person.socialsecuritynumber = txtsocialsecuritynumber.text; the textchanged event occurs on each keystroke and for each keystroke, the person set_socialsecuritynumber property is called which calls rulebroken. note the expression: socialsecuritynumber.length != 11. this describes as true the condition that makes the rule broken. on the first keystroke social security number is not 11 characters long so it is added to the broken rules collection. on subsequent keystrokes it remains broken and so rulebroken will skip adding it to the collection. but when the 11th character is typed rulebroken receives a false value for its argument, indicating that the rule is no longer broken, so it is removed from the collection. this procedure is repeated for the name and birth date properties until the person object becomes valid.
if (onvalid != null) { onvalid (this, eventargs.empty); }rulebroken executes this code when the count of broken business rules falls to zero. the client handles the event as follows:
// subscribe to person event person.onvalid += new person.eventhandler(person_onvalid); private void person_onvalid(object sender, system.eventargs e) { btnok.enabled = true; btnapply.enabled = true; }this event handler code is simplified compared to the actual implementation. but it illustrates the general idea. the oninvalid event is implemented similarly.
public event eventhandler onnewage;in the person object, once the birth date is valid this event is raised and it is raised again on each subsequent occasion that it becomes valid, if the new age differs from the previous valid age. the client handles the event as follows:
private void person_onnewage(object sender, system.eventargs e) { // update the displayed age lblage.text = person.age.tostring(); }
新闻热点
疑难解答