Prideorm Framework Design (I)-data access layer design

Source: Internet
Author: User

Before introducing the ORM framework, we will introduce the design of the data access layer. The data access layer is the basis of the ORM framework. Orm only encapsulates data access operations. I divide the database into three types based on the database access objects provided by. Net:

 Code  
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> ///
/// Database Type
///
Public Enum databasetype
{< br> sqlserver,
Oracle,
oledb
}

Except SQL Server and Oracle, other databases are classified as oledb. The data access layer uses the factory mode to create the corresponding data category based on parameters.CodeAs follows:

 

 Dbhelperfactory 
Public Class Dbhelperfactory
{
Public Static String Connectionstring { Get ; Set ;}
Public Static Databasetype dbtype { Get ; Set ;}

Static Dbhelperfactory ()
{
Dbtype = Databasetype. sqlserver;
}

/// <Summary>
/// Generate database category based on database connection string name
/// </Summary>
/// <Param name = "connectionname"> </param>
/// <Returns> </returns>
Public Static Idbhelper createdbhelper ( String Connectionname)
{
/// Obtain the database connection string from the configuration file
Connectionstring = Configurationmanager. connectionstrings [connectionname]. connectionstring;

Switch (Dbtype)
{
Case Databasetype. sqlserver:
Return New Sqldbhelper (connectionstring );
Case Databasetype. Oracle:
Return New Oracledbhelper (connectionstring );
Case Databasetype. oledb:
Return New Oledbhelper (connectionstring );
Default :
Return Null ;
}
}
}

 

The data access object is created based on the set database type and the input database connection string name. That is to say, multiple different types of databases can be used in a project, the SQL Server database is used by default. Each data category inherits a unified interface:

Idbhelper 
Public Interface Idbhelper
{
/// <Summary>
/// SQL statement
/// </Summary>
String Commandtext { Get ; Set ;}
/// <Summary>
/// Return multiple data records
/// </Summary>
/// <Returns> </returns>
Datatable executedatatable ();
/// <Summary>
/// Returns a single data record.
/// </Summary>
/// <Returns> </returns>
Datarow executedatarow ();
Int Executenonquery ();
Object Executescalar ();
/// <Summary>
/// Add Parameters
/// </Summary>
/// <Param name = "key"> Key </Param>
/// <Param name = "value"> Value </Param>
Void Addparameter ( String Key, Object Value );
}

 

The code is very simple, so there are not many explanations. You can refer to the source code, and there are basically comments. Now let's take a look at the usage of the data access layer:

 

 Code 
Public Datatable getdatatable ( String Name)
{
/// Set the Database Type
Dbhelperfactory. dbtype = Databasetype. sqlserver;
/// Create data category
Idbhelper dbhelper = Dbhelperfactory. createdbhelper ( " Connectionstring " );
String SQL = " Select * from products where name = @ name " ;
Dbhelper. commandtext = SQL;
/// Add Parameters
Dbhelper. addparameter ( " @ Name " , Name );
Return Dbhelper. executedatatable ();
}

It is so simple and intuitive to use.

The ORM framework has a lot of content and requires network disconnection. It is really inconvenient. It uses the switch next door, and others are going to bed. I am working on a project recently. I am busy. I have to work overtime tomorrow night. Now we will attach the source code of the data access layer and introduce the design of the orm framework next time. I like originality. Although it is not as good as others' frameworks, so flexible and powerful, I am happy to use it.

Dbaccesser.rarData access layer

 

 

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.