public static void Uppercase(this IIAddress a) { if (a == null) { return; } if (a.Address1 != null) { a.Address1 = a.Address1.ToUpper(); } if (a.City != null) { a.City = a.City.ToUpper(); } if (a.Country != null) { a.Country = a.Country.ToUpper(); } if (a.PostalCode != null) { a.PostalCode = a.PostalCode.ToUpper(); } if (a.Province != null) { a.Province = a.Province.ToUpper(); } }
//public static void CopyTo(this IMayHaveAddressId from, IMayHaveAddressId to, bool allowDefaultValues = true) //{ // if (from == null || to == null) // return; // if (allowDefaultValues || (from.AddressId.HasValue && from.AddressId != default(int))) // to.AddressId = from.AddressId; //} public static void CopyTo(this IIAddress from, IIAddress to, bool allowDefaultValues = true) { if (from == null || to == null) { return; } if (allowDefaultValues || !string.IsNullOrWhiteSpace(from.Address1)) { to.Address1 = from.Address1; } if (allowDefaultValues || !string.IsNullOrWhiteSpace(from.City)) { to.City = from.City; } if (allowDefaultValues || !string.IsNullOrWhiteSpace(from.Country)) { to.Country = from.Country; } if (allowDefaultValues || !string.IsNullOrWhiteSpace(from.PostalCode)) { to.PostalCode = from.PostalCode; } if (allowDefaultValues || !string.IsNullOrWhiteSpace(from.Province)) { to.Province = from.Province; } }
public static bool IsEmpty(this IIAddress address) { return(string.IsNullOrWhiteSpace(address.Address1) && string.IsNullOrWhiteSpace(address.PostalCode) && string.IsNullOrWhiteSpace(address.City) && string.IsNullOrWhiteSpace(address.Province) && string.IsNullOrWhiteSpace(address.Country)); }
public static ICollection <ValidationResult> Validate(this IIAddress address, bool strict = true) { if (strict) { return(ValidateDataAnnotations(address)); } else { return(ValidateMinimalNeeded(address)); } }
public static void Trim(this IIAddress address) { if (address.City != null) { address.City = address.City.Trim(); } if (address.Country != null) { address.Country = address.Country.Trim(); } if (address.Address1 != null) { address.Address1 = address.Address1.Trim(); } if (address.PostalCode != null) { address.PostalCode = Regex.Replace(address.PostalCode, @"\s*", ""); } if (address.Province != null) { address.Province = address.Province.Trim(); } }
public static ICollection <ValidationResult> ValidateDataAnnotations(this IIAddress address) { var currentThread = Thread.CurrentThread; var currentUiCulture = currentThread.CurrentUICulture; try { string addressCulture = CountryCodeToCulture(address.Country); if (addressCulture != null && currentUiCulture.IetfLanguageTag != addressCulture) { currentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(addressCulture); } AddressImplementor a; if (typeof(AddressImplementor).IsInstanceOfType(address)) { a = (AddressImplementor)address; } else { a = new AddressImplementor(); address.CopyTo(a); } var ret = new List <ValidationResult>(); var vc = new ValidationContext(a, null, null); Validator.TryValidateObject(a, vc, ret, true); return(ret); } finally { currentThread.CurrentUICulture = currentUiCulture; } }
public static ICollection <ValidationResult> ValidateMinimalNeeded(this IIAddress address) { var currentThread = Thread.CurrentThread; var currentUiCulture = currentThread.CurrentUICulture; try { string addressCulture = CountryCodeToCulture(address.Country); if (addressCulture != null && currentUiCulture.IetfLanguageTag != addressCulture) { currentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(addressCulture); } AddressImplementor a; if (typeof(AddressImplementor).IsInstanceOfType(address)) { a = (AddressImplementor)address; } else { a = new AddressImplementor(); address.CopyTo(a); } //Get flags indicating if we need to validate specific fields XDocument countries = XDocument.Load( EmbeddedResourceProvider.GetResourceStream(EmbeddedResourceProvider.ResourceTypeOptions.CountryProvinceCityXML) ); bool require_postal_code = true; bool require_city = true; var countryNode = countries.Root.Elements("country").FirstOrDefault(c => c.Attribute("code").Value == address.Country); if (countryNode != null) { require_postal_code = countryNode.Attribute("require_postal_code") == null ? require_postal_code : Convert.ToBoolean(countryNode.Attribute("require_postal_code").Value); require_city = countryNode.Attribute("require_city") == null ? require_city : Convert.ToBoolean(countryNode.Attribute("require_city").Value); } var ret = new List <ValidationResult>(); if (require_postal_code && string.IsNullOrWhiteSpace(address.PostalCode)) { if (require_city && string.IsNullOrWhiteSpace(address.City)) { ret.Add(new ValidationResult("Expected City or Postal Code", new string[] { "PostalCode", "City" })); } if (typeof(IAddress).IsInstanceOfType(address)) { var address2 = (IAddress)address; if (string.IsNullOrWhiteSpace(address2.Province) && string.IsNullOrWhiteSpace(address2.ProvinceName)) { ret.Add(new ValidationResult("Expected Province Code, Province Name or Postal Code", new string[] { "PostalCode", "Province" })); } } else { if (string.IsNullOrWhiteSpace(address.Province)) { ret.Add(new ValidationResult("Expected Province or Postal Code", new string[] { "PostalCode", "Province" })); } } } var vc = new ValidationContext(a, null, null); vc.MemberName = "Country"; Validator.TryValidateProperty(address.Country, vc, ret); if (!string.IsNullOrWhiteSpace(address.Address1)) { vc.MemberName = "Address1"; Validator.TryValidateProperty(address.Address1, vc, ret); } if (require_postal_code && !string.IsNullOrWhiteSpace(address.PostalCode)) { vc.MemberName = "PostalCode"; Validator.TryValidateProperty(address.PostalCode, vc, ret); } if (require_city && !string.IsNullOrWhiteSpace(address.City)) { vc.MemberName = "City"; Validator.TryValidateProperty(address.City, vc, ret); } if (!string.IsNullOrWhiteSpace(address.Province)) { vc.MemberName = "Province"; Validator.TryValidateProperty(address.Province, vc, ret); } return(ret); } finally { currentThread.CurrentUICulture = currentUiCulture; } }
public static Expression <Func <IAddress, bool> > ToLambda(this IIAddress filter, byte validityLevel = 0) { if (filter == null) { return(x => false); } if (filter.ValidateDataAnnotations().Any(v => !string.IsNullOrEmpty(v.ErrorMessage))) { return(x => false); } IAddress trimmedAddress = new AddressImplementor(); filter.CopyTo(trimmedAddress, true); if (trimmedAddress.Address1 != null) { trimmedAddress.Address1 = trimmedAddress.Address1.Trim().ToUpper(); } if (trimmedAddress.City != null) { trimmedAddress.City = trimmedAddress.City.Trim().ToUpper(); } if (trimmedAddress.Country != null) { trimmedAddress.Country = trimmedAddress.Country.Trim().ToUpper(); } if (trimmedAddress.PostalCode != null) { trimmedAddress.PostalCode = trimmedAddress.PostalCode.Replace(" ", "").Trim().ToUpper(); } if (trimmedAddress.Province != null) { trimmedAddress.Province = trimmedAddress.Province.Trim().ToUpper(); } if (trimmedAddress.ProvinceName != null) { trimmedAddress.ProvinceName = trimmedAddress.ProvinceName.Trim().ToUpper(); } trimmedAddress.ValidityLevel = validityLevel; Expression res; Expression <Func <IAddress, bool> > ret = x => true; var expParam = ret.Parameters.First(); res = ret.Body; //???: Do we need to serve IsResidential ? if (trimmedAddress.AddressId != default(int)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("AddressId"), Expression.Constant(trimmedAddress.AddressId)), res); } if (!string.IsNullOrEmpty(trimmedAddress.Address1)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("Address1"), Expression.Constant(trimmedAddress.Address1)), res); } if (!string.IsNullOrEmpty(trimmedAddress.City)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("City"), Expression.Constant(trimmedAddress.City)), res); } if (!string.IsNullOrEmpty(trimmedAddress.Country)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("Country"), Expression.Constant(trimmedAddress.Country)), res); } if (!string.IsNullOrEmpty(trimmedAddress.PostalCode)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("PostalCode"), Expression.Constant(trimmedAddress.PostalCode)), res); } if (!string.IsNullOrEmpty(trimmedAddress.Province)) { res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("Province"), Expression.Constant(trimmedAddress.Province)), res); } //if (!string.IsNullOrEmpty(trimmedAddress.ProvinceName)) // res = Expression.AndAlso(Expression.Equal(expParam.GetFieldAccessExpression("ProvinceName"), Expression.Constant(trimmedAddress.ProvinceName)), res); if (trimmedAddress.ValidityLevel != default(byte)) { res = Expression.AndAlso(Expression.GreaterThanOrEqual(expParam.GetFieldAccessExpression("ValidityLevel"), Expression.Constant(trimmedAddress.ValidityLevel)), res); } return(Expression.Lambda <Func <IAddress, bool> >(res, expParam)); }