[Silverlight entry series] Validation: Is inotifypropertychanged and inotifydataerrorinfo implemented in model or viewmodel?

Source: Internet
Author: User

Validation is divided into the following types:

  1. Type and length: for example, \ D {} can contain up to three digits, which must be numbers.
  2. Range: for example, a-Za-Z, greater than 100 and less than 200
  3. Business logic: Special, such as checking that a database name is unique (the backend service must be called) or other business logic.

In silverlight4, validation implements the inotifypropertychanged and inotifydataerrorinfo interfaces, which can be either placed in the model or viewmodel. (Do not implement both ). If your validation rules are relatively simple, for example, the first two types of rules above, it is more appropriate to put them in the model. However, if your validation rules are complex and special, you need to use business logic to call backend services asynchronously, and so on, it is more appropriate to put them in viewmodel.

The inotifydataerrorinfo interface is added to silverlight4.0:

 
1:NamespaceSystem. componentmodel
 
2 :{
 
3:Public InterfaceInotifydataerrorinfo
 
4 :{
5:BoolHaserrors {Get;}
 
6:EventEventhandler <dataerrorschangedeventargs> errorschanged;
 
7: ienumerable geterrors (StringPropertyname );
 
8 :}
 
9 :}

There is also the inotifypropertychanged interface:

 
1:NamespaceSystem. componentmodel
 
2 :{
 
3:Public InterfaceInotifypropertychanged
 
4 :{
 
5:EventPropertychangedeventhandler propertychanged;
 
6 :}
7 :}

Here is an example:

 
1:Public Abstract ClassMyxxx: inotifypropertychanged, inotifydataerrorinfo
 
2 :{
 
3:PrivateErrorscontainer <validationresult> errorscontainer;
 
4:
 
5:/// <Summary>
 
6:/// Initializes a new instance of the <see CREF = "domainobject"/> class.
 
7:/// </Summary>
 
8:ProtectedMyxxx ()
 
9 :{
 
10 :}
11:
 
12:/// <Summary>
 
13:/// Event raised when a property value changes.
 
14:/// </Summary>
 
15:/// <Seealso CREF = "inotifypropertychanged"/>
 
16:Public EventPropertychangedeventhandler propertychanged;
 
17:
 
18:/// <Summary>
 
19:/// Event raised when the validation status changes.
 
20:/// </Summary>
21:/// <Seealso CREF = "inotifydataerrorinfo"/>
 
22:Public EventEventhandler <dataerrorschangedeventargs> errorschanged;
 
23:
 
24:/// <Summary>
 
25:/// Gets the error status.
 
26:/// </Summary>
 
27:/// <Seealso CREF = "inotifydataerrorinfo"/>
 
28:Public BoolHaserrors
 
29 :{
30:Get{Return This. Errorscontainer. haserrors ;}
 
31 :}
 
32:
 
33:/// <Summary>
 
34:/// Gets the container for errors in the properties of the domain object.
 
35:/// </Summary>
 
36:ProtectedErrorscontainer <validationresult> errorscontainer
 
37 :{
 
38:Get
 
39 :{
40:If(This. Errorscontainer =Null)
 
41 :{
 
42:This. Errorscontainer =
 
43:NewErrorscontainer <validationresult> (Pn =>This. Raiseerrorschanged (PN ));
 
44 :}
 
45:
 
46:Return This. Errorscontainer;
 
47 :}
 
48 :}
 
49:
 
50:/// <Summary>
51:/// Returns the Errors for <paramref name = "propertyname"/>.
 
52:/// </Summary>
 
53:/// <Param name = "propertyname"> the name of the property for which the errors are requested. </param>
 
54:/// <Returns> An enumerable with the errors. </returns>
 
55:/// <Seealso CREF = "inotifydataerrorinfo"/>
 
56:PublicIenumerable geterrors (StringPropertyname)
 
57 :{
58:Return This. Errorscontainer. geterrors (propertyname );
 
59 :}
 
60:
 
61:/// <Summary>
 
62:/// Raises the <see CREF = "propertychanged"/> event.
 
63:/// </Summary>
 
64:/// <Param name = "propertyname"> the name of the changed property. </param>
 
65: [suppressmessage ("Microsoft. Design","Ca1030: useeventswhereappropriate", Justification ="Method supports event.")]
66:Protected VoidRaisepropertychanged (StringPropertyname)
 
67 :{
 
68:This. Onpropertychanged (NewPropertychangedeventargs (propertyname ));
 
69 :}
 
70:
 
71:/// <Summary>
 
72:/// Raises the <see CREF = "propertychanged"/> event.
 
73:/// </Summary>
 
74:/// <Param name = "E"> the argument for the event. </param>
75:Protected Virtual VoidOnpropertychanged (propertychangedeventargs E)
 
76 :{
 
77: var handler =This. Propertychanged;
 
78:If(Handler! =Null)
 
79 :{
 
80: handler (This, E );
 
81 :}
 
82 :}
 
83:
 
84:/// <Summary>
85:/// Validates <paramref name = "value"/> as the value for the property named <paramref name = "propertyname"/>.
 
86:/// </Summary>
 
87:/// <Param name = "propertyname"> The Name Of The property. </param>
 
88:/// <Param name = "value"> the value for the property. </param>
 
89:Protected VoidValidateproperty (StringPropertyname,Object Value)
 
90 :{
91:If(String. Isnullorempty (propertyname ))
 
92 :{
 
93:Throw NewArgumentnullexception ("Propertyname");
 
94 :}
 
95:
 
96:This. Validateproperty (NewValidationcontext (This,Null,Null) {Membername = propertyname },Value);
 
97 :}
 
98:
 
99:/// <Summary>
100:/// Validates <paramref name = "value"/> as the value for the property specified
 
101:/// <Paramref name = "validationcontext"/> using data annotations validation attributes.
 
102:/// </Summary>
 
103:/// <Param name = "validationcontext"> the context for the validation. </param>
 
104:/// <Param name = "value"> the value for the property. </param>
105:Protected Virtual VoidValidateproperty (validationcontext,Object Value)
 
106 :{
 
107:If(Validationcontext =Null)
 
108 :{
 
109:Throw NewArgumentnullexception ("Validationcontext");
 
110 :}
 
111:
 
112: List <validationresult> validationresults =NewList <validationresult> ();
113: validator. tryvalidateproperty (Value, Validationcontext, validationresults );
 
114:
 
115:This. Errorscontainer. seterrors (validationcontext. membername, validationresults );
 
116 :}
 
117:
 
118:/// <Summary>
 
119:/// Raises the <see CREF = "errorschanged"/> event.
 
120:/// </Summary>
 
121:/// <Param name = "propertyname"> the name of the property which changed its error status. </param>
122: [suppressmessage ("Microsoft. Design","Ca1030: useeventswhereappropriate", Justification ="Method supports event.")]
 
123:Protected VoidRaiseerrorschanged (StringPropertyname)
 
124 :{
 
125:This. Onerrorschanged (NewDataerrorschangedeventargs (propertyname ));
 
126 :}
 
127:
 
128:/// <Summary>
 
129:/// Raises the <see CREF = "errorschanged"/> event.
130:/// </Summary>
 
131:/// <Param name = "E"> the argument for the event. </param>
 
132:Protected Virtual VoidOnerrorschanged (dataerrorschangedeventargs E)
 
133 :{
 
134: var handler =This. Errorschanged;
 
135:If(Handler! =Null)
 
136 :{
 
137: handler (This, E );
 
138 :}
 
139 :}
 
140 :}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.