public static Specification <EFCustomer> GetCustomerByFilter(CustomerQueryCriteria criteria) { Specification <EFCustomer> specProfile = new TrueSpecification <EFCustomer>(); if (!string.IsNullOrEmpty(criteria.UserName)) { specProfile &= new DirectSpecification <EFCustomer>( p => p.UserName.Contains(criteria.UserName)); } if (!string.IsNullOrEmpty(criteria.StreetName)) { specProfile &= new DirectSpecification <EFCustomer>( p => p.Address.Street.Contains(criteria.StreetName)); } if (!string.IsNullOrEmpty(criteria.Email)) { specProfile &= new DirectSpecification <EFCustomer>( p => p.Email.Contains(criteria.Email)); } if (!string.IsNullOrEmpty(criteria.Phone)) { specProfile &= new DirectSpecification <EFCustomer>( p => p.Email == criteria.Phone); } return(specProfile); }
public void ef_spec_test() { var customer = GetCustomerInfo(); InsertNewCustomer(customer); var custList = new List <EFCustomer>(); var cQuery = new CustomerQueryCriteria { UserName = "******" }; var spec = CustomerSpecification.GetCustomerByFilter(cQuery); using (var repo = new CustomerRepository()) { custList = repo.GetBy(spec).ToList(); } custList.Count.Should().Be(1); custList.First().UserName.Should().Be("jacky.zhou"); custList.First().Id.Should().BeGreaterThan(0); }