Query Application Block 4.1

The Query Application Block takes the Data Access layer to the next level providing a consistent interface to data storage no matter how or where it is stored. Where the DAAB gives developers a provider model to RDBMS data storage giving independence from the type of RDBMS in use, the Query Application Block gives developers a provider model for queries to data storage from a variety of different sources and source types using the features of the Enterprise Library 4.1. The Query Application Block interfaces to the DAAB for data stored in relational databases but can also be configured to query data from XML Files and WCF Services. The QAB is designed for two-way data storage, reading and writing to all supported data storage types. All configuration of such queries naturally uses the Enterprise Library Configuration Console.

In essence the QAB is similar to the Entity Framework and NHibernate, bringing such features to the Enterprise Library. One difference is that the QAB uses DataSets as the Data Transfer Object type (DTO) and has no mapping facility to convert between DataSets and a business Domain Model. The next stage to providing full Object Relational Mapping (ORM) between a business Domain Model and a data store is to combine with the Mapping Application Block that interfaces to the QAB providing that conversion through configuration. Thus the QAB and the MAB combine together, along with the Configuration Console, to form a full ORM tool.

Note: There was much debate over my use of the DataSet rather than the slimmer DataTable for the Data Transfer Object type, especially as multiple table handling is quite rare. However, I felt the additional flexibility provided within a DataSet, especially for use with custom commands, outweighed the overhead.

The developer is not limited to the providers out-of-the-box but can extend the block by adding providers of their own in the normal Enterprise Library extensible fashion.

Main Features

The following are the main features associated with the Query Application Block:

Key Uses

If you need convincing as to why you would use this block, have a look at the following reasons:

A Quick Code Sample

The following code sample shows the basic function of the block, how to get data with a simple query that takes no parameters:

[C#]
// execute a query with a configured name of "Read Data" that takes no parameters and reads data from a data store returning the results as a DataSet
DataSet ds = QueryFactory.CreateQuery("Read Data").ExecuteForRead();


This next code sample shows how to execute a read query that takes a single parameter:

[C#]
// execute a query with a configured name of "Read Data By Id" that takes a single integer parameter with the name of "Id" and value of 27 and reads data from a data store returning the results as a DataSet
IDictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("Id", 27);
DataSet ds = QueryFactory.CreateQuery("Read Data By Id").ExecuteForRead(parameters);


This next code sample shows how to execute a write query that takes 3 parameters:

[C#]
// execute a query with a configured name of "Write Data" that takes 3 parameters and writes the data away to a data store
IDictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("Id", id);
parameters.Add("Name", name);
parameters.Add("Description", description);
QueryFactory.CreateQuery("Write Data").ExecuteForWrite(parameters);


This next code sample shows how to execute a write query that takes its parameters from a DataSet:

[C#]
// execute a query with a configured name of "Write Data from DataSet" that takes a DataSet and writes the data away to a data store
DataSet dataSet = GetDataSetFromGridView();
QueryFactory.CreateQuery("Write Data from DataSet").ExecuteForWrite(dataSet);

Installing the Assemblies

When you build the Query Application Block the assemblies are compiled without a strong name key. If you want to put these assemblies into the GAC then you will need to assign a strong name key to each assembly and recompile (this is not neccessary for any design.dll assembly). If you use the BuildLibraryAndCopyAssemblies.bat console script then the assemblies are copied to the EntLibContrib bin folder. You can reference the assemblies from here for your application however; to use the Configuration console you will need to copy all of the following assemblies to your installation of the Enterprise Library Configuration Console which is usually in C:\Program Files\Microsoft Enterprise Library 4.1 - October 2008\Bin:

The Query Application Block 4.1 consists of the following assemblies:

What Next?

Acknowledgements

The QAB is inspired by the ideas in Martin Fowler's book Patterns of Enterprise Application Architecture. The Query Application Block implements the DTO, Query Object, Gateway and Plugin patterns as well as the "finder" and "command" objects. I have now complemented the QAB with a Mapping Application Block (MAB) which completes the implementation of PoEAA by adding configurable Object Relational Mapping to a Domain Model.