Query Object Mode

Source: Internet
Author: User


Complex data query is an important part of web systems. If the data access code is constructed in a DaO object or repository object, it is also feasible. If you extract complex data queries for separate modeling,

If the DaO object and repository object are left with only basic data access, isn't it feasible to achieve higher separation of responsibilities. (The following describes the code included in this article)

The query object mode can be used to solve this problem. For complex queries, We need to model the query. The purpose of the query is to execute a piece of code to return results.

We can define such an interface:

Namespace queryobject. Core. Data. Query
{
///
<Summary>
/// Description of IQUERY.
/// </Summary>
Public
Interface IQUERY
{
}
Public interface IQUERY <tresult>:
IQUERY
{
Tresult execute ();
}
}

There is only one method in the interface. Any object implementing this interface is a query object. For example, we want to provide a complex query for a product. Obviously, we only need to construct an object that implements the IQUERY <tresult> interface,

The productsearch class can also directly implement the query interface. However, to implement API-oriented programming or provide a contract, we must first define an iproductsearch interface to implement IQUERY <tresult>, you can use productsearch to implement the iproductsearch interface:

Namespace queryobject. Core. Data. Query
{
///
<Summary>
/// Description of iproductsearch.
///
</Summary>
Public interface iproductsearch:
IQUERY <ienumerable <product>
{
String name {Get;
Set ;}
String description {Get; set ;}
Decimal? Minimumprice {Get;
Set ;}
Decimal? Maximumprice {Get; set ;}
Productsearchsort sort {
Get; set ;}
}
Public
Enum productsearchsort
{
Priceasc,
Pricedesc,
Name
}
}

The Code provides two symbolic methods to implement this interface. One is the productsearch implemented by the query string, and its inheritance system is sqlbase,
Sqlquerybase, sql1_querybase, productsearch

Another is procproductsearch, which is intended to be implemented using stored procedures. Its Inheritance System is sqlbase, sqlquerybase,
Sqlprocquerybase, procproductsearch

Of course, it is more convenient to use the ORM tool for implementation.

You can create query objects for each query and query objects by PAGE based on various Query Needs.

Dao object or repository object to use the query object to obtain the query object, for convenience,

Let's define another interface for the query object factory. the implementers of this interface can easily return the desired query object:

Namespace queryobject. Core. Data. Query
{
///
<Summary>
/// Description of iqueryfactory
///
</Summary>
Public interface iqueryfactory
{
Tquery
Createquery <tquery> () Where tquery: IQUERY;
}
}

The Code provides a simple implementation of queryfactory in the queryobject. Core. Data. IMP namespace.

So far, we have a query object and a query object factory that is convenient for locating the query object. The next step is to create a repository class to use the query object. Define a basic interface:

Irepository <t>,
In the code, irepository <t> inherits the iqueryfactory interface in order to make each repository class that implements this interface provide external complex queries.

Namespace queryobject. Core. Data
{
///
<Summary>
/// Description of irepository.
///
</Summary>
Public interface irepository <t>:
Iqueryfactory where T: Entity
{
// Basic operation
}
}

The current result is that each repository is also a queryfactory object. In fact, the repository object holds an object that independently implements the iqueryfactory interface.
Reference, the repository object can only wrap the following

Call the createquery <tquery> () method.

The Code has an implementation of the irepository interface productrepository. Its Inheritance System is sqlbase, sqlrepository,
Productrepository.

By now, the basic code structure has been built. So how to call it? There is always a piece of test code,

Idbconnection conn = new
Sqlconnection (@ "Server =. \ sqlexpress; database = test;
Trusted_connection = sspi ");
Iqueryfactory queryfactory = new
Queryfactory (conn );
Productrepository target = new
Productrepository (Conn, queryfactory );
VaR query =
Target. createquery <productsearch> ();
Query. Name
= "Apple ";
VaR result = query. Execute ();

I am not sure whether it is easy to understand or not. But how can I check the source code? haha, let's get the code.

Source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.