Extended dbutility (1)

Source: Internet
Author: User

This article original path: https://www.zybuluo.com/Ivony/note/14074

Objective

Dbutility V3 is an open-source lightweight database access framework that is published through the Apache protocol and can be used for commercial purposes. The latest version can be downloaded through NuGet, project and source code:

Https://github.com/Ivony/DbUtility

Dbutility first version published seven years ago, about the history of dbutility and the use of dbutility, please refer to the following blog:

Http://www.cnblogs.com/Ivony/p/3659746.html
https://www.zybuluo.com/Ivony/note/8277

As an open source project, Dbutility is not only dedicated to helping you simplify database access, but also welcomes and looks forward to your participation, whether it's submitting bugs or unit tests, or new feature requirements, and adding more interesting features to dbutility yourself. Is the way to participate in open source projects. This article is intended for all programmers interested in adding custom functionality to dbutility, introducing Dbutility's extended architecture.

Basic structure

The Dbutility v3 is the most significant difference from previous versions, and Dbutility V3 's disdain for other lightweight database access frameworks is its excellent extension architecture. As described in the previous introduction, dbutility divides a database query into three parts:

    • Query Executor
    • Query Builder
    • Result Builder

Each of the three sections has a key interface, namely:

    • Idbexecutor where T:idbquery a query executor that defines a type of query.
    • Idbquery defining a specific database query
    • Idbexecutecontext defines the database query execution context, which includes the DataReader object.

For example, the sqldbutility type is a query executor implementation type that targets a SQL Server database and is declared as:

 Public class sqldbutility:  Iasyncdbexecutor<ParameterizedQuery>,  iasyncdbexecutor<StoredProcedureQuery>  ,  Idbtransactionprovider<SqlDbUtility>

IAsyncDbExecutoris an IDbExecutor asynchronous version that indicates that a query of the specified type can be executed asynchronously. As you can see, sqldbutility implements two query executor interfaces, respectively, IAsyncDbExecutor<ParameterizedQuery> and IAsyncDbExecutor<StoredProcedureQuery> This means that objects of this type can execute queries of type ( ParameterizedQuery parameterized query) as well as StoredProcedureQuery types (stored procedures).

Extending the Query Builder

Next we try to extend a query-building approach, just like that T( "SELECT * FROM Users" ) .

Suppose we often encounter a scenario where we need to get all the data from a table, and if we use T to build a parameterized query, there are a lot of SQL statements to write: What SELECT * FROM TableName do we want to simplify as DT( "TableName" ) a form of?

First we want to determine whether we need to build a new type of query, because building a new query type requires modifying the query executor at the same time, which in this case does not involve such in-depth issues. We assume that the parameterized query object is borrowed for the moment, just to simplify its generated code.

So the first thing we need to know is that all of the query building methods are actually extension methods, which are extended to a particular query executor. If we are generating a parameterized query, then we need a parameterized query executor to execute, so we are going to expand on the parameterized query executor. First, create a new static type for the extension method:

 Public Static class myextensions{}

It is important to note that the type that defines the extension method must be a static type.

Then we add an extension method, because the type of query we ultimately generate is a parameterized query, so we need to extend the object that can execute the parameterized query, like this:

 Public Static void  This string tableName) {  thrownew  notimplementedexception ();}

Please note that the return value type of my side has not been filled in, because there is another problem, the simple query object can not be executed, in order to achieve db.DT( "Users" ).ExecuteDataTable() this effect, we need to bind the query object and the query executor, the type of the bundled object IDbExecutableQuery (meaning an executable query), So we need to set the return value to IDbExecutableQuery type:

 Public Static  This string tableName) {  thrownew  notimplementedexception ();}

Dbutility has provided us with a ready-made IDbExecutableQuery implementation, that is, the DbExecutableQuery<T> type, we just need to build a query object, and then together with the query executor call this type of constructor:

 Public Static  This string tableName) {  null;   return New Dbexecutablequery<parameterizedquery>(executor, query);}

Finally, we need to build a parameterized query object, the parameterized query object as the basic query object, there are many ways to build, the most common is to use the template to build, if the db.T method is called directly, in fact, the construction of the DbExecutableQuery<ParameterizedQuery> type of object, fortunately, the system provides a static type Dbto provide support for these common tasks:

 Public Static  This string tableName) {  "" + tableName);   return New Dbexecutablequery<parameterizedquery>(executor, query);}

At this point, our first extension is complete, and try it out right away:

  var " Database "  );   var " Users " ). Executedatatable ();

End

In fact, you know, db.T this method, its internal implementation, is like this.
The following is an excerpt from dbutility V3 source code:

 Public StaticDbexecutablequery<parameterizedquery> Template ( ThisIdbexecutor<parameterizedquery> Executor,stringTemplateparams Object[] Parameters) {  return NewDbexecutablequery<parameterizedquery>(executor, templateparser.parsetemplate (template, parameters));} Public StaticDbexecutablequery<parameterizedquery> T ( ThisIdbexecutor<parameterizedquery> Executor,stringTemplateparams Object[] Parameters) {  returntemplate (executor, template, parameters);}

Next, we will extend this method further, we want the user to call this method, no longer return an executable query object, but directly execute the query, and return the DataTable is good, I believe that smart you should soon be able to think of how to write:

   Public Static classMyExtensions { Public StaticDataTable DT ( ThisIdbexecutor<parameterizedquery> Executor,stringtableName) {parameterizedquery Query= DB.T ("SELECT * from"+tableName); return NewDbexecutablequery<parameterizedquery>(executor, query).    Executedatatable (); }  }

Yes, with an excellent extensible architecture, dbutility can be arbitrarily transformed into any form of API you like, and this inverse extensibility is deeply embedded in the design of the dbutility overall architecture model. constitutes an unparalleled experience for dbutility.

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.