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 IAddress filter) { if (filter == null) { return(x => false); } if (filter.ValidateDataAnnotations().Any(v => !string.IsNullOrEmpty(v.ErrorMessage))) { return(x => false); } AddressImplementor 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(); } 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)); }