1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using DemoApp.Properties;
namespace DemoApp.Model
{
/// <summary>
/// Represents a customer of a company. This class
/// has built-in validation logic. It is wrapped
/// by the CustomerViewModel class, which enables it to
/// be easily displayed and edited by a WPF user interface.
/// </summary>
public class Customer : IDataErrorInfo
{
public static Customer CreateNewCustomer()
{
return new Customer();
}
public static Customer CreateCustomer(
double totalSales,
string firstName,
string lastName,
bool isCompany,
string email)
{
return new Customer
{
TotalSales = totalSales,
FirstName = firstName,
LastName = lastName,
IsCompany = isCompany,
Email = email
};
}
protected Customer()
{
}
public string Email { get; set; }
public string FirstName { get; set; }
public bool IsCompany { get; set; }
public string LastName { get; set; }
public double TotalSales { get; private set; }
string IDataErrorInfo.Error { get { return null; } }
string IDataErrorInfo.this[string propertyName]
{
get { return this.GetValidationError(propertyName); }
}
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
if (GetValidationError(property) != null)
return false;
return true;
}
}
static readonly string[] ValidatedProperties =
{
"Email",
"FirstName",
"LastName",
};
string GetValidationError(string propertyName)
{
if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
return null;
string error = null;
switch (propertyName)
{
case "Email":
error = this.ValidateEmail();
break;
case "FirstName":
error = this.ValidateFirstName();
break;
case "LastName":
error = this.ValidateLastName();
break;
default:
Debug.Fail("Unexpected property being validated on Customer: " + propertyName);
break;
}
return error;
}
string ValidateEmail()
{
if (IsStringMissing(this.Email))
{
return Strings.Customer_Error_MissingEmail;
}
else if (!IsValidEmailAddress(this.Email))
{
return Strings.Customer_Error_InvalidEmail;
}
return null;
}
string ValidateFirstName()
{
if (IsStringMissing(this.FirstName))
{
return Strings.Customer_Error_MissingFirstName;
}
return null;
}
string ValidateLastName()
{
if (this.IsCompany)
{
if (!IsStringMissing(this.LastName))
return Strings.Customer_Error_CompanyHasNoLastName;
}
else
{
if (IsStringMissing(this.LastName))
return Strings.Customer_Error_MissingLastName;
}
return null;
}
static bool IsStringMissing(string value)
{
return
String.IsNullOrEmpty(value) ||
value.Trim() == String.Empty;
}
static bool IsValidEmailAddress(string email)
{
if (IsStringMissing(email))
return false;
// This regex pattern came from: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
return Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase);
}
}
} |
Partager