Пример #1
0
        public void ExecuteDynamicQueryObject()
        {
            var source = new List<Product>
                             {
                                 new Product {Name = "NPatterns", Version = 1.2},
                                 new Product {Name = "NPatterns.Messaging.IoC",Version = 1.1},
                                 new Product {Name = "NPatterns.ObjectRelational.EF",Version = 1.0},
                                 new Product {Name = null,Version = 0}
                             };

            var query = new QueryObject(new DynamicQueryObjectExecutor());
            query.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsNotNull });

            var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or };
            criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" });
            criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" });

            query.Add(criteriaGroup);
            query.Add(new SortDescription { Field = "Version", Direction = SortDirection.Ascending });

            var result = query.Execute(source.AsQueryable()).ToList();
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result[0].Version < result[1].Version);

            query.Add(new Criteria { Field = "Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or);
            var result2 = query.Execute(source.AsQueryable()).ToList();
            Assert.AreEqual(3, result2.Count);
        }
Пример #2
0
        public void ExecuteDynamicQueryObject()
        {
            var source = new List <Product>
            {
                new Product {
                    Name = "NPatterns", Version = 1.2
                },
                new Product {
                    Name = "NPatterns.Messaging.IoC", Version = 1.1
                },
                new Product {
                    Name = "NPatterns.ObjectRelational.EF", Version = 1.0
                },
                new Product {
                    Name = null, Version = 0
                }
            };

            var query = new QueryObject(new DynamicQueryObjectExecutor());

            query.Add(new Criteria {
                Field = "Name", Operator = CriteriaOperator.IsNotNull
            });

            var criteriaGroup = new CriteriaGroup {
                Operator = CriteriaGroupOperator.Or
            };

            criteriaGroup.Criterias.Add(new Criteria {
                Field = "Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns"
            });
            criteriaGroup.Criterias.Add(new Criteria {
                Field = "Name", Operator = CriteriaOperator.EndsWith, Value = "ioc"
            });

            query.Add(criteriaGroup);
            query.Add(new SortDescription {
                Field = "Version", Direction = SortDirection.Ascending
            });

            var result = query.Execute(source.AsQueryable()).ToList();

            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result[0].Version < result[1].Version);

            query.Add(new Criteria {
                Field = "Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0
            }, CriteriaGroupOperator.Or);
            var result2 = query.Execute(source.AsQueryable()).ToList();

            Assert.AreEqual(3, result2.Count);
        }
Пример #3
0
        public void ExecuteDynamicQueryObjectWithComplexType()
        {
            var source = new List<ProductFeature>
                             {
                                 new ProductFeature
                                     {
                                         Product = new Product {Name = "NPatterns", Version = 1.2},
                                         Feature = new Feature {Name = "IUnitOfWork"}
                                     },
                                 new ProductFeature
                                     {
                                         Product = new Product {Name = "NPatterns.Messaging.IoC", Version = 1.1},
                                         Feature = new Feature {Name = "MessageBus"}
                                     },
                                 new ProductFeature
                                     {
                                         Product = new Product {Name = "NPatterns.ObjectRelational.EF", Version = 1.0},
                                         Feature = new Feature {Name = "UnitOfWork"}
                                     },
                                 new ProductFeature
                                     {
                                         Product = new Product(),
                                         Feature = new Feature()
                                     }
                             };

            var query = new QueryObject(new DynamicQueryObjectExecutor());
            query.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsNotNull });

            var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or };
            criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" });
            criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" });

            query.Add(criteriaGroup);
            var result = query.Execute(source.AsQueryable()).ToList();
            Assert.AreEqual(2, result.Count);

            query.Add(new Criteria { Field = "Product.Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or);
            var result2 = query.Execute(source.AsQueryable()).ToList();
            Assert.AreEqual(3, result2.Count);
        }
Пример #4
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "TEST";

            var uow = new UnitOfWork();
            var customerRepository = new CustomerRepository(uow);
            var orderRepository    = new OrderRepository(uow);

            Console.WriteLine("SIMPLE CRUD TEST APP");
            //FindAll TEST
            Console.WriteLine("FINDALL TEST");
            Console.WriteLine("List of Customers:");
            var customers = customerRepository.FindAll().ToList <Customer>();

            for (var index = 0; index < customers.Count; index++)
            {
                Console.WriteLine(string.Format("{0}\t{1}", index + 1, customers[index].FullName));
            }
            PressTo("CONTINUE");

            //Update TEST
            Console.WriteLine("UPDATE TEST");
            var customerEntity = customers[0];

            Console.WriteLine(string.Format("Customer current FirstName: {0}", customerEntity.FirstName));
            Console.WriteLine("ENTER Customer NEW FirstName: ");
            var newName = Console.ReadLine();
            var oldName = customerEntity.FirstName;

            customerEntity.FirstName = newName;
            Console.WriteLine("Update data? (Y/N): ");

            var keyPressed = Console.ReadKey(true).Key;

            while (keyPressed != ConsoleKey.Y && keyPressed != ConsoleKey.N)
            {
                keyPressed = Console.ReadKey(true).Key;
            }
            if (keyPressed == ConsoleKey.Y)
            {
                customerRepository.Update(customerEntity);
                uow.Commit();
                Console.WriteLine("Data updated!");
            }
            else
            {
                customerEntity.FirstName = oldName;
                Console.WriteLine("Change reverted!");
            }

            Console.WriteLine(string.Format("Customer Full Name: {0}", customerEntity.FullName));
            PressTo("CONTINUE");

            //Insert TEST
            Console.WriteLine("INSERT TEST");

            Console.WriteLine("ENTER NEW Customer FirstName: ");
            var newFirstName = Console.ReadLine();

            Console.WriteLine("ENTER NEW Customer LastName: ");
            var newLastName = Console.ReadLine();

            customerEntity = Customer.Create(newFirstName, newLastName);
            customerRepository.Add(customerEntity);
            uow.Commit();
            Console.WriteLine("NEW Customer CREATED!");
            Console.WriteLine("");
            Console.WriteLine("List of Customers:");
            customers = customerRepository.FindAll().ToList <Customer>();
            for (var index = 0; index < customers.Count; index++)
            {
                Console.WriteLine(string.Format("{0}\t{1}", index + 1, customers[index].FullName));
            }
            PressTo("CONTINUE");

            //Delete TEST
            Console.WriteLine("DELETE TEST");

            Console.WriteLine("ENTER Customer's number to DELETE: ");
            int deleteIndex;

            while (!int.TryParse(Console.ReadLine(), out deleteIndex) || (deleteIndex < 0 || deleteIndex > customers.Count))
            {
                Console.WriteLine("ENTER a valid Customer's number to DELETE: ");
            }
            customerEntity = customers[deleteIndex - 1];
            customerRepository.Remove(customerEntity);
            uow.Commit();
            Console.WriteLine(string.Format("Customer '{0}' was DELETED!", customerEntity.FullName));
            Console.WriteLine("");
            Console.WriteLine("List of Customers:");
            customers = customerRepository.FindAll().ToList <Customer>();
            for (var index = 0; index < customers.Count; index++)
            {
                Console.WriteLine(string.Format("{0}\t{1}", index + 1, customers[index].FullName));
            }
            PressTo("CONTINUE");

            //FindAll TEST [Orders]
            Console.WriteLine("FINDALL TEST [Orders]");
            Console.WriteLine("Enter Customer's number to display the list of Orders:");
            int customerIndex;

            while (!int.TryParse(Console.ReadLine(), out customerIndex) || (customerIndex < 0 || customerIndex > customers.Count))
            {
                Console.WriteLine("ENTER a valid Customer's number: ");
            }
            customerEntity = customers[customerIndex - 1];
            Console.WriteLine(string.Format("{0}'s ORDERS LIST:", customerEntity.FirstName));
            var orders = orderRepository.FindAllBy(customerEntity.Id).ToList <Order>();

            for (var index = 0; index < orders.Count; index++)
            {
                Console.WriteLine(string.Format("{0}\t{1}", index + 1, orders[index].OrderDate));
            }
            PressTo("CONTINUE");

            //FindAll TEST [Orders] with QueryObject
            Console.WriteLine("FINDALL TEST [Orders] with QueryObject");
            Console.WriteLine("Enter Customer's FirstName to display the list of Orders:");
            var customerFirstName = Console.ReadLine();

            var query = new QueryObject(typeof(Customer));

            query.AddCriteria(Criteria.Matches("FirstName", customerFirstName));
            var customerByQueryObject = query.Execute(uow);

            query = new QueryObject(typeof(Order));
            query.AddCriteria(Criteria.Matches("CustomerId", customerByQueryObject[0].Id.ToString()));
            var ordersByQueryObject = query.Execute(uow);

            Console.WriteLine(string.Format("{0}'s ORDERS LIST:", ((Customer)customerByQueryObject[0]).FirstName));
            for (var index = 0; index < ordersByQueryObject.Count; index++)
            {
                Console.WriteLine(string.Format("{0}\t{1}", index + 1, ((Order)ordersByQueryObject[index]).OrderDate));
            }
            PressTo("CONTINUE");

            PressTo("EXIT");
        }
Пример #5
0
        public void ExecuteDynamicQueryObjectWithComplexType()
        {
            var source = new List <ProductFeature>
            {
                new ProductFeature
                {
                    Product = new Product {
                        Name = "NPatterns", Version = 1.2
                    },
                    Feature = new Feature {
                        Name = "IUnitOfWork"
                    }
                },
                new ProductFeature
                {
                    Product = new Product {
                        Name = "NPatterns.Messaging.IoC", Version = 1.1
                    },
                    Feature = new Feature {
                        Name = "MessageBus"
                    }
                },
                new ProductFeature
                {
                    Product = new Product {
                        Name = "NPatterns.ObjectRelational.EF", Version = 1.0
                    },
                    Feature = new Feature {
                        Name = "UnitOfWork"
                    }
                },
                new ProductFeature
                {
                    Product = new Product(),
                    Feature = new Feature()
                }
            };

            var query = new QueryObject(new DynamicQueryObjectExecutor());

            query.Add(new Criteria {
                Field = "Product.Name", Operator = CriteriaOperator.IsNotNull
            });

            var criteriaGroup = new CriteriaGroup {
                Operator = CriteriaGroupOperator.Or
            };

            criteriaGroup.Criterias.Add(new Criteria {
                Field = "Product.Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns"
            });
            criteriaGroup.Criterias.Add(new Criteria {
                Field = "Product.Name", Operator = CriteriaOperator.EndsWith, Value = "ioc"
            });

            query.Add(criteriaGroup);
            var result = query.Execute(source.AsQueryable()).ToList();

            Assert.AreEqual(2, result.Count);

            query.Add(new Criteria {
                Field = "Product.Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0
            }, CriteriaGroupOperator.Or);
            var result2 = query.Execute(source.AsQueryable()).ToList();

            Assert.AreEqual(3, result2.Count);
        }