Getting Started with Default Compiler for Object

Fidely Framework contains the default search query compiler to make it easy to begin using it. In this section describes basic usage of the Fidely default search query compiler.

The following is the sample Product data that is used in this section.

Id Name ListPrice SellStartDate
1 Pencil A 100 2006/4/1
2 Pencil B 150 2006/7/1
3 Notebook A 200 2007/1/10
4 Pen A 120 2007/2/1
5 Book A 300 2007/3/15
6 Pen B 500 2008/10/31
7 Notebook B 250 2008/12/15
8 Notebook C 180 2009/4/1
9 Book B 210 2009/8/12
10 Pen C 400 2010/1/1

Work Through

1. Create the Console Application project.

2. Add assembly reference to Fidely.Framework.dll and Fidely.Framework.Compilation.Object.dll

3. Create Product.cs.

public class Product
{
    [Alias("id")]
    public int ProductID { get; set; }

    public string Name { get; set; }

    [Alias("price")]
    public decimal ListPrice { get; set; }

    [Alias("start")]
    public DateTime SellStartDate { get; set; }
}

4. Implement Main method as follows.

public static void Main(string[] args)
{
    SearchQueryCompiler<Product> compiler =
        SearchQueryCompilerBuilder.Instance.BuildUpDefaultCompilerForObject<Product>();
    IEnumerable<Product> products = GetProducts();

    while (true)
    {
        Console.Write("$ ");
        string query = Console.ReadLine();
        Expression<Func<Product, bool>> filter = compiler.Compile(query);
        IEnumerable<Product> result = products.AsQueryable().Where(filter);
        PrintProducts(result);
    }
}

5. Press F5 to debug.

Note that this work through abbreviates code of GetProducts and PrintProducts method because these methods aren't related to Fidely Framework. You can get the complete source code and the executable file from here.

Try To Search

First, let's press Enter without entering any characters. It means "Search without criteria".
console_demo001.png
The Fidely search query compiler supports the search query format like "PropertyName Operator Value". For example, let's type "ProductID = 1". Then the product data that its Product ID is equal to 1 will be shown.
console_demo002.png
The default search query compiler supports the comparative operators =, !=, <, <=, >, >=, : (partial matching), =: (prefix search) and := (suffix search). For example, Name : book finds products that its product name contains "book".
console_demo003.png
The logical operators (AND and OR) are supported. You can find products that its list price between 200 to 400 by entering 200 <= ListPrice AND ListPrice < 400.
console_demo004.png
You can also use abbreviation format like 200<= ListPrice < 400.
console_demo005.png
If the property has the AliasAttribute, you can use the alias name instead of the property name. Let's try to type 200 <= price < 400 and press Enter key. It returns same result set as 200<= ListPrice < 400.
console_demo006.png
The four arithmetic operations (+, -, * and /) are also supported, so you can use a search query like 600 / 3 <= price < (10 + 30) * 10.
console_demo007.png
If you want to use the value of DateTime type, you have to enclose the value in double quotes. Because the default search query compiler recognizes / as the divide operator.
console_demo008.png
Note that if you enclose the value in single quotes, it's treated as String value.
console_demo009.png
Finally, let's type 3 and press Enter. If the search query doesn't contain any comparative operators, the default search query compiler generates the expression tree to perform partial match on all properties.
console_demo010.png

Last edited Jun 5, 2011 at 1:27 PM by sasaminn, version 9

Comments

No comments yet.