69
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 using System.Linq;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3 using FluentValidation;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 using SilverlightValidation.Interfaces;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 using SilverlightValidation.Validators;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 using SilverlightValidation.Models;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8 namespace SilverlightValidation.ViewModels
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9 {
|
78
|
10 public class UserViewModel : ViewModelBase, IUserModel
|
69
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12 #region Fields
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14 private readonly UserModelValidator _validator;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 private UserModel _data;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 #region Constructor
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21 public UserViewModel(UserModel model, UserModelValidator validator)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23 _validator = validator;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24 _data = model;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 #region Properties
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 private const string UsernameProperty = "Username";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 public string Username
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34 get { return _data.Username; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35 set
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 if (_data.Username != value)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39 _data.Username = value;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 RaisePropertyChanged(UsernameProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
41 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
42
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
43 ClearError(UsernameProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
44 var validationResult = _validator.Validate(this, UsernameProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
45 if (!validationResult.IsValid)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
46 validationResult.Errors.ToList().ForEach(x => SetError(UsernameProperty, x.ErrorMessage));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
47 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
48 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
49
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
50 private const string PasswordProperty = "Password";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
51 public string Password
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
52 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
53 get { return _data.Password; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
54 set
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
55 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
56 if (_data.Password != value)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
57 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
58 _data.Password = value;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
59 RaisePropertyChanged(PasswordProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
60 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
61
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
62 ClearError(PasswordProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
63 var validationResult = _validator.Validate(this, PasswordProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
64 if (!validationResult.IsValid)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
65 validationResult.Errors.ToList().ForEach(x => SetError(PasswordProperty, x.ErrorMessage));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
66 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
67 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
68
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
69 private const string EmailProperty = "Email";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
70 public string Email
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
71 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
72 get { return _data.Email; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
73 set
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
74 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
75 if (_data.Email != value)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
76 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
77 _data.Email = value;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
78 RaisePropertyChanged(EmailProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
79 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
80
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
81 ClearError(EmailProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
82 var validationResult = _validator.Validate(this, EmailProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
83 if (!validationResult.IsValid)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
84 validationResult.Errors.ToList().ForEach(x => SetError(EmailProperty, x.ErrorMessage));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
85 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
86 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
87
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
88 private const string DateOfBirthProperty = "DateOfBirth";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
89 public DateTime? DateOfBirth
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
90 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
91 get { return _data.DateOfBirth; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
92 set
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
93 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
94 if (_data.DateOfBirth != value)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
95 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
96 _data.DateOfBirth = value;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
97 RaisePropertyChanged(DateOfBirthProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
98 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
99
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
100 ClearError(DateOfBirthProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
101 var validationResult = _validator.Validate(this, DateOfBirthProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
102 if (!validationResult.IsValid)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
103 validationResult.Errors.ToList().ForEach(x => SetError(DateOfBirthProperty, x.ErrorMessage));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
104 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
105 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
106
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
107 private const string DescriptionProperty = "Description";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
108 public string Description
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
109 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
110 get { return _data.Description; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
111 set
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
112 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
113 if (_data.Description != value)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
114 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
115 _data.Description = value;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
116 RaisePropertyChanged(DescriptionProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
117 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
118
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
119 ClearError(DescriptionProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
120 var validationResult = _validator.Validate(this, DescriptionProperty);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
121 if (!validationResult.IsValid)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
122 validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
123 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
124 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
125
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
126 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
127 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
128 }
|