Samiksha Jaiswal (Editor)

FluentQueryBuilder

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

The FluentQueryBuilder is a propriety .NET class developed in order to help with the construction and initialization of a Query instance.

Contents

It uses a fluent interface in order to implement the Builder Pattern.

History

Before the introduction of the FluentQueryBuilder, the now outdated QueryBuilder was used for this purpose. However, it suffered from the verbosity associated with a classic interface, failed to help its clients deal with casting issues and type conversion issues commonly associated with Filter and Entity Attributes creation. The FluentQueryBuilder was created to address these issues.

Look and Feel

Following is a sample of a query created using the FluentQueryBuilder together with a discussion of how the shortcomings of QueryBuilder have been addressed

Query q = new FluentQueryBuilder() .AddRange(someAttributes) .Add(ADtoClass.Attributes.AnAttribute) .FilterGroup .Add(ADtoClass.Filter.AFilter, Operators.Equals, 1) .BuildQuery();

Fluent Interface

Due to its fluent interface, user can create a query without the need to assign the instance to a variable and continuously reference it in order to add attributes and filters. Using QueryBuilder, one would have to perform the following:

var qb = new QueryBuilder(); qb.EntityAttributes = ...; qb.FilterGroup = ... var query = qb.Build();

Type Conversion Help

EntityAttributes and Filters are stored as integers inside a query, but are initially available as enumeration values. Using QueryBuilder, client would have to continuously cast the enum values to integers. FluentQueryBuilder on the other hand uses generics to allow the user to pass any value for a filter, attribute, operator or value, and the values are cast or converted internally appropriately. For example, previously user would have had to write the following:

Filter f = new Filter(); f.Column = (int)ADtoClass.Filter.AFilter; f.Operator = (int)Operators.Equals; f.Value = 1.ToString(); qb.FilterGroup.Add(f);

Filter Creation Help

Previously, Client had to create an instance of a Filter, and add it to the Filters of a FilterGroup. Then add the FilterGroup to the QueryBuilder. FluentQueryBuilder create the FilterGroup when the FilterGroup Property is called, and a filter can be added to this by specifying property values of filter: i.e. ".FilterGroup.Add(ADtoClass.Filter.AFilter, Opperators.Equals, 1)" as seen above.

References

FluentQueryBuilder Wikipedia


Similar Topics