Exemplo n.º 1
0
        public void ScratchPadTest()
        {
            //repository.Get(keyboard, o=>o.InAscOrderOf(p=>p.AverageRatings));

            //var keyboard = new ProductCategorySpecification(ProductCategory.KeyBoard);

            //1. Following example is achieving the same result as above one but using DynamicSpecification.

            //var keyboard = new DynamicSpecification<Product>("Category", OperationType.EqualTo, "Keyboard");
            //var popular = new DynamicSpecification<Product>("AverageRatings",OperationType.GreaterThanEqualTo,  "4.5");

            //var popularKeyboard = popular.And(keyboard);
            //var popularKeyboards = repository.Get(popularKeyboard);

            //2. Order following example we're combinding DynamicSpecification with GenericSpecification.

            //var keyboard = new GenericSpecification<Product>(p=>p.Category==ProductCategory.KeyBoard);
            //var popular = new DynamicSpecification<Product>("AverageRatings", OperationType.GreaterThanEqualTo, "4.5");

            //var popularKeyboard = popular.And(keyboard);
            //var popularKeyboards = repository.Get(popularKeyboard);

            //var popular = new PopularProductSpecification();
            //var keyboard = new CategorySpecification(ProductCategory.KeyBoard);
            //var popularKeyboard = popular.And(keyboard);

            //var popularKeyboards = _repository.Get(popularKeyboard);

            string userSuppliedProperty = "AverageRatings";
            OperationType userSuppliedOperationType = OperationType.GreaterThan;
            var userSuppliedValue = 4.5;

            var userFilter = new DynamicSpecification<Product>(userSuppliedProperty, userSuppliedOperationType, userSuppliedValue);
            var filteredProducts = _repository.Get(userFilter);

            //and dynamic ordering
            string userSuppliedOrderingProperty = "Category";
            OrderType userSuppliedOrderType = OrderType.Ascending;
            var sortedFilteredProduct = _repository.Get(userFilter, o => o.InOrderOf(userSuppliedOrderingProperty, userSuppliedOrderType));
        }
        public void Throws_exception_when_dynamic_property_is_not_found_in_field_and_properties_of_candidate_object()
        {
            var specification = new DynamicSpecification<TestClass>("Age", OperationType.GreaterThan, 30);

            TestDelegate matchingOfSpecification = ()=> specification.IsSatisfiedBy(_candidateObject);

            Assert.That(matchingOfSpecification,Throws.TypeOf<ArgumentException>());
        }