PageRenderTime 5891ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/SplitSpike/QuickStarts/QSBusinessLayerCSharp/Person.cs

#
C# | 284 lines | 250 code | 32 blank | 2 comment | 27 complexity | e63ed276a93003784c62c66d8ae149d8 MD5 | raw file
  1. using System;
  2. using System.Reflection;
  3. using ValidationFramework;
  4. namespace QSBusinessLayerCSharp
  5. {
  6. public class Person : NotifyValidatableBase
  7. {
  8. #region Fields
  9. //Define run-time constants here so UI code has compile-time
  10. //checks against misspellings in data binding expressions.
  11. public const string FirstNameMember = "FirstName";
  12. public const string GenderMember = "Gender";
  13. public const string AddressMember = "Address";
  14. public const string LastNameMember = "LastName";
  15. public const string AgeMember = "Age";
  16. public const string EmailAddressMember = "EmailAddress";
  17. public const string CardTypeMember = "CardType";
  18. public const string CreditCardNumberMember = "CreditCardNumber";
  19. public const string OrderTotalMember = "OrderTotal";
  20. public const string PhoneNumberMember = "PhoneNumber";
  21. private Address address;
  22. private string firstName;
  23. private string lastName;
  24. private string phoneNumber;
  25. private int age;
  26. private string emailAddress;
  27. private CardType? cardType;
  28. private string creditCardNumber;
  29. private decimal orderTotal;
  30. private Gender gender;
  31. #endregion
  32. #region Constructor
  33. public Person()
  34. {
  35. PropertyValidationManager.ValidateAll();
  36. }
  37. #endregion
  38. #region Properties
  39. [IsValidValidatorAttribute]
  40. public Address Address
  41. {
  42. get
  43. {
  44. return address;
  45. }
  46. set
  47. {
  48. if (value != address)
  49. {
  50. address = value;
  51. NotifyPropertyChanged(AddressMember);
  52. }
  53. }
  54. }
  55. [RequiredStringRule]
  56. [RequiredStringRule(InitialValue = "aaa", IgnoreCase = true, TrimWhiteSpace = false)]
  57. [LengthStringRule(50)]
  58. public string FirstName
  59. {
  60. get
  61. {
  62. return firstName;
  63. }
  64. set
  65. {
  66. if (value != firstName)
  67. {
  68. firstName = value;
  69. NotifyAndValidate(FirstNameMember);
  70. }
  71. }
  72. }
  73. [RequiredStringRule]
  74. [LengthStringRule(50, Minimum = 2)]
  75. public string LastName
  76. {
  77. get
  78. {
  79. return lastName;
  80. }
  81. set
  82. {
  83. if (value != lastName)
  84. {
  85. lastName = value;
  86. NotifyAndValidate(LastNameMember);
  87. }
  88. }
  89. }
  90. [RequiredIntRule]
  91. [CompareIntRule(18, CompareOperator.GreaterThanEqual)]
  92. [CompareIntRule(100, CompareOperator.LessThanEqual)]
  93. public int Age
  94. {
  95. get
  96. {
  97. return age;
  98. }
  99. set
  100. {
  101. if (value != age)
  102. {
  103. age = value;
  104. NotifyAndValidate(AgeMember);
  105. }
  106. }
  107. }
  108. [RequiredStringRule]
  109. [RegexValidatorAttribute(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Invalid email format.")]
  110. [LengthStringRule(50)]
  111. public string EmailAddress
  112. {
  113. get
  114. {
  115. return emailAddress;
  116. }
  117. set
  118. {
  119. if (value != emailAddress)
  120. {
  121. emailAddress = value;
  122. NotifyAndValidate(EmailAddressMember);
  123. }
  124. }
  125. }
  126. [RequiredEnumRule((long)QSBusinessLayerCSharp.CardType.Visa)]
  127. public CardType? CardType
  128. {
  129. get
  130. {
  131. return cardType;
  132. }
  133. set
  134. {
  135. if (value != cardType)
  136. {
  137. cardType = value;
  138. NotifyAndValidate(CardTypeMember);
  139. NotifyAndValidate(CreditCardNumberMember);
  140. }
  141. }
  142. }
  143. [RequiredStringRule]
  144. [LengthStringRule(16)]
  145. [InlineValidatorAttribute("QSBusinessLayerCSharp.Person,QSBusinessLayerCSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "CheckValidCreditCard", "Validate based on credit card rules.", ErrorMessage = "Credit card is invalid.")]
  146. public string CreditCardNumber
  147. {
  148. get
  149. {
  150. return creditCardNumber;
  151. }
  152. set
  153. {
  154. if (value != creditCardNumber)
  155. {
  156. creditCardNumber = value;
  157. NotifyAndValidate(CreditCardNumberMember);
  158. }
  159. }
  160. }
  161. [RequiredDecimalRule]
  162. [RangeDecimalRule(10, 1000)]
  163. public decimal OrderTotal
  164. {
  165. get
  166. {
  167. return orderTotal;
  168. }
  169. set
  170. {
  171. if (value != orderTotal)
  172. {
  173. orderTotal = value;
  174. NotifyAndValidate(OrderTotalMember);
  175. }
  176. }
  177. }
  178. [RegexValidatorAttribute(@"\d{2}-\d{4}-\d{4}", ErrorMessage = "Phone number must be of the format 11-1111-1111")]
  179. public string PhoneNumber
  180. {
  181. get
  182. {
  183. return phoneNumber;
  184. }
  185. set
  186. {
  187. if (value != phoneNumber)
  188. {
  189. phoneNumber = value;
  190. NotifyAndValidate(PhoneNumberMember);
  191. }
  192. }
  193. }
  194. public int Id { get; set; }
  195. [RequiredEnumRule((long)Gender.Unknown)]
  196. public Gender Gender
  197. {
  198. get
  199. {
  200. return gender;
  201. }
  202. set
  203. {
  204. if (value != gender)
  205. {
  206. gender = value;
  207. NotifyAndValidate(GenderMember);
  208. }
  209. }
  210. }
  211. #endregion
  212. #region Methods
  213. public static void CheckValidCreditCard(object sender, CustomValidationEventArgs e)
  214. {
  215. var person = (Person)e.TargetObjectValue;
  216. var cardType = person.CardType;
  217. var cardNumber = person.CreditCardNumber;
  218. if (cardNumber == null || cardType == null)
  219. {
  220. e.IsValid = false;
  221. }
  222. else
  223. {
  224. switch (cardType.Value)
  225. {
  226. case QSBusinessLayerCSharp.CardType.Visa:
  227. case QSBusinessLayerCSharp.CardType.MasterCard:
  228. {
  229. e.IsValid = (cardNumber.Length == 16);
  230. e.ErrorMessage = "VISA/MasterCard has to have 16 characters";
  231. break;
  232. }
  233. case QSBusinessLayerCSharp.CardType.Amex:
  234. {
  235. e.IsValid = (cardNumber.Length == 15);
  236. e.ErrorMessage = "AMEX has to have 15 characters.";
  237. break;
  238. }
  239. default:
  240. {
  241. throw new ArgumentException("Invalid CardType");
  242. }
  243. }
  244. }
  245. }
  246. public void Hello([RequiredStringRule]string text)
  247. {
  248. var validate = ParameterValidationManager.Validate(this, MethodBase.GetCurrentMethod().MethodHandle, text);
  249. }
  250. #endregion
  251. }
  252. }