static void Main() { var mobiles = new List <Mobile> { new Mobile(BrandName.Samsung, Type.Smart, 700), new Mobile(BrandName.Apple, Type.Smart), new Mobile(BrandName.Htc, Type.Basic), new Mobile(BrandName.Samsung, Type.Basic) }; ISpecification <Mobile> samsungExpSpec = new ExpressionSpecification <Mobile>(o => o.BrandName == BrandName.Samsung); ISpecification <Mobile> htcExpSpec = new ExpressionSpecification <Mobile>(o => o.BrandName == BrandName.Htc); ISpecification <Mobile> SamsungHtcExpSpec = samsungExpSpec.Or(htcExpSpec); ISpecification <Mobile> NoSamsungExpSpec = new ExpressionSpecification <Mobile>(o => o.BrandName != BrandName.Samsung); var samsungMobiles = mobiles.FindAll(samsungExpSpec.IsSatisfiedBy); var htcMobiles = mobiles.FindAll(htcExpSpec.IsSatisfiedBy); var samsungHtcMobiles = mobiles.FindAll(SamsungHtcExpSpec.IsSatisfiedBy); var noSamsungMobiles = mobiles.FindAll(NoSamsungExpSpec.IsSatisfiedBy); //samsungHtcMobiles.ForEach(o => Console.WriteLine(o.ToString())); //samsungMobiles.ForEach(o => Console.WriteLine(o.ToString())); //htcMobiles.ForEach(o => Console.WriteLine(o.ToString())); noSamsungMobiles.ForEach(o => Console.WriteLine(o.ToString())); }
public void OrTestCase3() { var left = new ExpressionSpecification<String>( x => false ); var target = left.Or( x => false ); var actual = target.IsSatisfiedBy( String.Empty ); Assert.IsFalse( actual ); }
public void OrTestCase4() { var left = new ExpressionSpecification<String>( x => true ); var target = left.Or( x => true ); var actual = target.IsSatisfiedByWithMessages( String.Empty ); Assert.AreEqual( 0, actual.Count() ); }
public void MoreThan10_Or_LessThan5() { ISpecification <int> moreThan10 = ExpressionSpecification.For <int>(i => i > 10); ISpecification <int> lessThan5 = new ExpressionSpecification <int>(i => i < 5); ISpecification <int> subject = lessThan5.Or(moreThan10); Assert.That(subject, Must.Not.Be.SatisfiedBy(7)); Assert.That(subject, Must.Be.SatisfiedBy(3).And(13)); }
public void OrTest8() { var left = new ExpressionSpecification <String>(x => true, "msgLeft"); var target = left.Or(x => true, "msgRight"); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Equal(0, actual.Count()); }
public void OrTestCase10() { var left = new ExpressionSpecification<String>( x => false, "msgLeft" ); var target = left.Or( x => true, "msgRight" ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 0, actual.Count() ); }
public void OrTest4() { var left = new ExpressionSpecification <String>(x => true); var target = left.Or(x => true); var actual = target.IsSatisfiedByWithMessages(String.Empty); Assert.Empty(actual); }
public void OrTestNullCheck1() { var left = new ExpressionSpecification <String>(x => true); Func <String, Boolean> expression = null; // ReSharper disable once AssignNullToNotNullAttribute Action test = () => left.Or(expression); test.ShouldThrow <ArgumentNullException>(); }
public void OrTest3() { var left = new ExpressionSpecification <String>(x => false); var target = left.Or(x => false); var actual = target.IsSatisfiedBy(String.Empty); Assert.False(actual); }
public void OrTest3() { var target = new ExpressionSpecification <String>(x => false); var other = new ExpressionSpecification <String>(x => false); var actual = target.Or(other); var result = actual.IsSatisfiedBy(String.Empty); Assert.False(result); }
public void OrTest5() { var left = new ExpressionSpecification <String>(x => true); var target = left.Or(x => false); var actual = target.IsSatisfiedByWithMessages(String.Empty) .ToList(); Assert.Equal(0, actual.Count); }
public void OrTest10() { var left = new ExpressionSpecification <String>(x => false, "msgLeft"); var target = left.Or(x => true, "msgRight"); var actual = target.IsSatisfiedByWithMessages(String.Empty) .ToList(); Assert.Empty(actual); }
public void OrTestNullCheck() { var target = new ExpressionSpecification <String>(x => false); ExpressionSpecification <String> other = null; // ReSharper disable once AssignNullToNotNullAttribute Action test = () => target.Or(other); test.ShouldThrow <ArgumentNullException>(); }
public void Test_And_Specification() { ISpecification<int> rule1 = new ExpressionSpecification<int>(x => x == 1, "rule1 failed"); ISpecification<int> rule2 = new ExpressionSpecification<int>(x => x == 2, "rule2 failed"); ISpecification<int> rule3 = new ExpressionSpecification<int>(x => x == 3, "rule3 failed"); ISpecification<int> rule4 = rule1.Or(rule2).Or(rule3); var result = rule4.ValidateWithMessages(4); Assert.IsTrue(result.Count > 0); }
public void Test_And_Specification() { ISpecification <int> rule1 = new ExpressionSpecification <int>(x => x == 1, "rule1 failed"); ISpecification <int> rule2 = new ExpressionSpecification <int>(x => x == 2, "rule2 failed"); ISpecification <int> rule3 = new ExpressionSpecification <int>(x => x == 3, "rule3 failed"); ISpecification <int> rule4 = rule1.Or(rule2).Or(rule3); var result = rule4.ValidateWithMessages(4); Assert.IsTrue(result.Count > 0); }
public void OrTest11() { var left = new ExpressionSpecification <String>(x => false, "msgLeft"); var target = left.Or(x => false, "msgRight"); var actual = target.IsSatisfiedByWithMessages(String.Empty) .ToList(); Assert.Equal(2, actual.Count); Assert.Equal(1, actual.Count(x => x == "msgLeft")); Assert.Equal(1, actual.Count(x => x == "msgRight")); }
public static ExpressionSpecification <Product> GetProductsById([Annotations.NotNull] params long[] idCollection) { var temp = new ExpressionSpecification <Product>(p => p.Id == idCollection[0]); for (int i = 1; i < idCollection.Length; i++) { temp = new ExpressionSpecification <Product>(temp .Or(new ExpressionSpecification <Product>(p => p.Id == idCollection[i])) .IsSatisfiedBy()); } return(temp); }
public static void Main() { var someBookstore = new Bookstore("Shakespeare and Company"); var witchesAbroad = new Book("Witches abroad", Genre.Fantasy, 301); someBookstore.Add(witchesAbroad); var hatFullOfSky = new Book("Hat full of sky", Genre.Fantasy, 310); someBookstore.Add(hatFullOfSky); var gameOfThrones = new Book("Game of thrones", Genre.Fantasy, 900); someBookstore.Add(gameOfThrones); var deathOnTheNile = new Book("Death on the Nile", Genre.Crime, 400); someBookstore.Add(deathOnTheNile); var historyOfTheBalkans = new Book("History of the Balkans", Genre.History, 1120); someBookstore.Add(historyOfTheBalkans); Console.WriteLine(someBookstore); var ruleOnlyFantasy = new ExpressionSpecification<Book>(b => b.Genre == Genre.Fantasy); var ruleLargeBooks = new ExpressionSpecification<Book>(b => b.Pages > 1000); var rulePagesBelow500 = new ExpressionSpecification<Book>(b => b.Pages < 500); var ruleFantasyOrLargeBooks = ruleOnlyFantasy.Or(ruleLargeBooks); var allBooks = someBookstore.All(); var largeBooks = allBooks.FindAll(b => ruleLargeBooks.IsSatisfiedBy(b)); Console.WriteLine("Large books:"); foreach (var book in largeBooks) { Console.WriteLine(book); } Console.WriteLine(); var fantasyAndLargeBooks = allBooks.FindAll(b => ruleFantasyOrLargeBooks.IsSatisfiedBy(b)); Console.WriteLine("Fantasy and large books:"); foreach (var book in fantasyAndLargeBooks) { Console.WriteLine(book); } Console.WriteLine(); }
static void Main(string[] args) { try { PersonDataMapper personDataMapper = new PersonDataMapper(ConfigurationManager.ConnectionStrings["connectionStringSchool"].ConnectionString); /* * personDataMapper.Insert(new Person * { * LastName = "Munguia", * FirstName = "Isack", * HireDate = DateTime.Now, * EnrollmentDate = DateTime.Now, * Discriminator = "Instructor" * }); */ List <DataMapper.Models.Person> personas = personDataMapper.GetFiltered("AND FirstName = 'Isack'", "LastName", 10, 1).ToList(); //List<Person> personasConApellidoPerez = personDataMapper.GetFiltered("LastName = 'Perez'", "", int.MaxValue, 1).ToList(); //personas.ForEach(Console.WriteLine); //ISpecification<DataMapper.Models.Person> personasQueSeLlamanIsack = new ExpressionSpecification<DataMapper.Models.Person>(persona => persona.FirstName == "Isack"); //ISpecification<DataMapper.Models.Person> personasConApellidoSuarez = new ExpressionSpecification<DataMapper.Models.Person>(persona => persona.LastName == "Suarez"); //ISpecification<DataMapper.Models.Person> personasQueNoSonEstudiantes = new ExpressionSpecification<DataMapper.Models.Person>(persona => persona.Discriminator != "Student"); //ISpecification<DataMapper.Models.Person> todasLasEspecificaciones = personasQueSeLlamanIsack.Or(personasConApellidoSuarez).Or(personasQueNoSonEstudiantes); //personas.FindAll(()=>personasQueSeLlamanIsack.IsSatisfiedBy).ForEach(Console.WriteLine); //var dataFiltrada = personas.FindAll(persona => todasLasEspecificaciones.IsSatisfiedBy()); //personas.FindAll(persona => new PropertyEqualsSpecification<DataMapper.Models.Person>("Discriminator", "Student").IsSatisfiedBy(persona)).ForEach(Console.WriteLine); var p1 = new Person { LastName = "Munguia", FirstName = "Isack", HireDate = DateTime.Now, EnrollmentDate = DateTime.Now, Discriminator = "Instructor" }; var p2 = new Person { LastName = "Herrera", FirstName = "Gysela", HireDate = null, EnrollmentDate = DateTime.Now, Discriminator = "Instructor" }; ISpecification <Person> specification0 = new PersonDiscriminatorSpecification("Instructor"); ISpecification <Person> specification1 = new PersonFirstNameSpecification("Isack"); ISpecification <Person> specification2 = new ExpressionSpecification <Person>(p => p.FirstName.FirstOrDefault().Equals('A')); ISpecification <Person> specification3 = new ExpressionSpecification <Person>(p => p.LastName.FirstOrDefault().Equals('A')); var todasLasEspecificaciones = specification3.Or(specification2); List <Person> persons = new List <Person>(); using (new ContextCreation()) { var repository = PersonRepository.Instance; //var itemToCreate = repository.Create(pp); //var itemToDelete = repository.Delete(specification1); //var itemToDelete = repository.Delete(p2); //repository.SaveChanges(); persons = repository.Filter(todasLasEspecificaciones).ToList(); //var updateResult = repository.Update(p2); } Console.WriteLine($"{nameof(persons)}: {persons.Count}"); persons.ToList().ForEach(Console.WriteLine); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadKey(); } }
public void OrTestCase7() { var left = new ExpressionSpecification<String>( x => false ); var target = left.Or( x => false ); var actual = target.IsSatisfiedByWithMessages( String.Empty ) .ToList(); Assert.AreEqual( 2, actual.Count() ); Assert.IsNull( actual[0] ); Assert.IsNull( actual[1] ); }
static void Main(string[] args) { // Person list var people = new List <Person> { new Person(Guid.NewGuid(), "Douglas 1", new Email("*****@*****.**"), new Category(2, "Partner")), new Person(Guid.NewGuid(), "Douglas 2", new Email("*****@*****.**"), new Category(1, "Customer")), new Person(Guid.NewGuid(), "Douglas 3", new Email("*****@*****.**"), new Category(2, "Partner")), new Person(Guid.NewGuid(), "Douglas 4", new Email("douglas4_gmail.com"), new Category(1, "Customer")), new Person(Guid.NewGuid(), "Douglas 5", new Email("*****@*****.**"), new Category(1, "Customer")), new Person(Guid.NewGuid(), "Douglas 6", new Email("*****@*****.**"), new Category(1, "Customer")), new Person(Guid.NewGuid(), "Douglas 7", new Email("*****@*****.**"), null) }; Console.WriteLine(":: ALL PEOPLE ::"); foreach (var item in people) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item?.Email?.Address + " | " + item.Category?.Description); } // Specifications usages Console.WriteLine(""); Console.WriteLine(":: CUSTOMER ::"); ISpecification <Person> personCustomersSpecification = new PersonCustomerSpecification <Person>(); var customer = people.FindAll(x => personCustomersSpecification.IsSatisfiedBy(x)); foreach (var item in customer) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.WriteLine(""); Console.WriteLine(":: PARTNER ::"); ISpecification <Person> partnerSpecification = new ExpressionSpecification <Person>(x => x.Category?.CategoryId == 2); var partners = people.FindAll(x => partnerSpecification.IsSatisfiedBy(x)); foreach (var item in partners) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.WriteLine(""); Console.WriteLine(":: WITHOUT CATEGORY ::"); ISpecification <Person> nullSpecification = new ExpressionSpecification <Person>(x => x.Category == null); var nullCategory = people.FindAll(x => nullSpecification.IsSatisfiedBy(x)); foreach (var item in nullCategory) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.WriteLine(""); Console.WriteLine(":: WITH AND WITHOUT CATEGORY ::"); ISpecification <Person> customersSpecification = new ExpressionSpecification <Person>(x => x.Category?.CategoryId == 1); var allWithCategorySpecification = customersSpecification.Or(partnerSpecification); var includeNull = allWithCategorySpecification.Or(nullSpecification); var allAndNullCategory = people.FindAll(x => includeNull.IsSatisfiedBy(x)); foreach (var item in allAndNullCategory) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(""); Console.WriteLine(":: ALL VALID ::"); ISpecification <Person> validSpecification = new PersonValidSpecification <Person>(); var validPeople = people.Where(x => validSpecification.IsSatisfiedBy(x)); foreach (var item in validPeople) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(""); Console.WriteLine(":: VALID CUSTOMERS ::"); var validCustomers = people.Where(x => validSpecification.IsSatisfiedBy(x) && customersSpecification.IsSatisfiedBy(x)); foreach (var item in validCustomers) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); Console.WriteLine(":: VALID PARTNERS ::"); var validPartners = people.Where(x => validSpecification.IsSatisfiedBy(x) && partnerSpecification.IsSatisfiedBy(x)); foreach (var item in validPartners) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(""); Console.WriteLine(":: INVALID ::"); var invalidPeople = people.Where(x => !validSpecification.IsSatisfiedBy(x)); foreach (var item in invalidPeople) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(""); Console.WriteLine(":: ISVALID ::"); var isvalidPeople = people.Where(x => x.IsValid()); foreach (var item in isvalidPeople) { Console.WriteLine(item.PersonId + " | " + item.Name + " | " + item.Email.Address + " | " + item.Category?.Description); } Console.ReadKey(); }
public void OrTestCase3() { var target = new ExpressionSpecification<String>( x => false ); var other = new ExpressionSpecification<String>( x => false ); var actual = target.Or( other ); var result = actual.IsSatisfiedBy( String.Empty ); Assert.IsFalse( result ); }
public void OrTestCaseNullCheck() { var target = new ExpressionSpecification<String>( x => false ); ExpressionSpecification<String> other = null; Action test = () => target.Or( other ); test.ShouldThrow<ArgumentNullException>(); }
public void OrTestCaseNullCheck1() { var left = new ExpressionSpecification<String>( x => true ); Func<String, Boolean> expression = null; Action test = () => left.Or( expression ); test.ShouldThrow<ArgumentNullException>(); }