C # lightweight ORM Framework implementation and DEMO source code

Source: Internet
Author: User

A lightweight ORM framework written by myself is released. This framework is designed based on a three-tier architecture at the beginning. Therefore, from the perspective of naming, it is easy to understand the three-tier architecture.

The purpose of designing this framework is to add, delete, modify, and query data without repeated writes, and focus on function implementation.

Reasons for publishing and changing the framework: I hope to give a reference to beginners, give good suggestions, and give myself a presentation opportunity.

Before I start, let me explain that I have almost no idea about the concept of software engineering. The highest level of education is the second day.

Start with my orm design bottom layer

The bottom layer is a DalBase, Which is abstract and implements the basic operations of addition, deletion, modification, and query.

Since it is abstract, it should not have any specific members.

Its core objects include: database access objects, generating abstract definitions of SQL objects, creating SQL parameter abstraction methods, and where parameterized query object abstraction definitions.

/// <Summary >/// DBHelper object for data access /// </summary> public abstract DbHelperBase DBHelper {get ;} // subclass implementation /// <summary> /// obtain the object that generates the SQL text /// </summary> protected internal abstract BuildSQL BuildSQLTextObj {get ;} /// <summary> /// obtain the data parameter object /// </summary> protected internal abstract DbParameter GetDbParam (string paramName, object paramValue ); /// <summary> /// create a WhereHelper object // </summary> internal abstract WhereHelper CreateWhereHelper ();

To expand the supported database types, you only need to implement specific code for these abstract objects.

The following code adds, deletes, and modifies a query (the query is relatively complex and is released separately)

/// <Summary> /// Number of affected rows returned by the SQL statement // </summary> public virtual int ExecuteBySQL (string sqlText, Dictionary <string, object> dbParams) {// The operations for executing SQL statements are implemented by DbParameter [] parameters = GetDbParam (dbParams); int rows = DBHelper. execNonQuery (sqlText, parameters); return rows ;} /// <summary> /// Add one or more items /// </summary> public virtual int Add <TModel> (params TModel [] models) where TModel: modelBase, new () {ThrowModelIsNullException (models); string sqlText; Dictionary <string, object> dbParams;
// In fact, the internal implementation accesses the InsertSQLTExtAndParam of BuildSQLObj. It generates SQL statements and SQL parameter objects and writes them as a method so that the subclass can rewrite them, you may have thought too much about this. You can rewrite Add directly. generateInsertSQLTextAndParam (out sqlText, out dbParams, models); int rows = ExecuteBySQL (sqlText, dbParams); return rows ;} /// <summary> /// update one or more records. Specify the update field based on the SQL condition. (If sqlWhere is empty, update by primary key) /// </summary> public virtual int Update <TModel> (string [] fields, string sqlWhere, Dictionary <stri Ng, object> dbParams, params TModel [] models) where TModel: ModelBase, new () {ThrowModelIsNullException (models); string sqlText; GenerateUpdateSQLTextAndParam (out sqlText, ref dbParams, sqlWhere, fields, models); int rows = ExecuteBySQL (sqlText, dbParams); return rows;} // <summary> // update one or more records based on SQL conditions, ignore the updated field // </summary> public virtual int UpdateByIgnoreField <TModel> (string [] ignoreFie Lds, string sqlWhere, Dictionary <string, object> dbParams, params TModel [] models) where TModel: ModelBase, new () {string [] allFields = BuildSQLTextObj. getAllFields <TModel> (); string [] updateFields = BuildSQLTextObj. removeFields (allFields, ignoreFields); return Update (updateFields, sqlWhere, dbParams, models );} /// <summary> /// Delete a record based on the primary key /// </summary> public virtual int Delete <TModel> (params o Bject [] pkValues) where TModel: ModelBase, new () {string sqlText; Dictionary <string, object> dbParams; if (pkValues = null | pkValues. length <0) {throw new DbDataException ("the primary key is blank during the delete operation! ");} GenerateDeleteSQLTextAndParam <TModel> (out sqlText, out dbParams, pkValues); int rows = ExecuteBySQL (sqlText, dbParams); return rows ;}

The method provided by the query is "3". It returns DataSet, List, DataReader, and paging.

Public virtual DataTable GetDataTable <TModel> (string sqlWhere, Dictionary <string, object> dbParams, string [] orderFields, bool isDesc, params string [] selectFields) where TModel: ModelBase, new ()
{
String sqlText;
GenerateSearchSQLTextAndParam <TModel> (sqlWhere, dbParams, orderFields, isDesc, out sqlText, selectFields );
Return GetDataTableBySQL (sqlText, dbParams );
}

Public virtual DbDataReader GetDataReader <TModel> (string sqlWhere, Dictionary <string, object> dbParams, string [] orderFields, bool isDesc, params string [] selectFields) where TModel: ModelBase, new ()
{
String sqlText;
GenerateSearchSQLTextAndParam <TModel> (sqlWhere, dbParams, orderFields, isDesc, out sqlText, selectFields );
Return GetDataReaderBySQL (sqlText, dbParams );
}

Public virtual List <TModel> GetList <TModel> (string sqlWhere, Dictionary <string, object> dbParams, string [] orderFields, bool isDesc, params string [] selectFields) where TModel: modelBase, new ()
{
DbDataReader dbReader = GetDataReader <TModel> (sqlWhere, dbParams, orderFields, isDesc, selectFields );
List <TModel> list = GetListByDataReader <TModel> (dbReader );
Return list;
}

Why is there a DataReader method? It is useful to return it. 1 is to convert it to List, 2 is because the DataReader method is also called internally for obtaining DataSet. (This can be seen through decompilation)

Because DataReader converts a List more efficiently than DataTable to List;

 

 

 

Related Article

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.