The second bullet in the quick development framework: Getting ORM wings for quick development (1)

Source: Internet
Author: User

I want to write general class libraries and general controls. A general library is a collection of some open-source projects based on the common methods accumulated by you. Open-source projects are listed as follows: Discuze, CommonLibrary. NET ,. NET Extensions ....., in addition, Log4Net is integrated with several open-source components. okay. Now let's start this article.

Select an ORM

I am a fan of ORM and have used llblgen, Nber, iBatis, SubSonic, Hxj. data, and finally chose MySoft for the following reasons: I do not like the database access method configured in XML, and I do not like the lazy loading method for objects like drag, you do not like to query the database before each update. You do not like to add characters to the strong ORM syntax. Okay, except for MySoft. _. I like it most, regardless of the syntax or usage. if you are interested, read the blog of Mao Ge, author of MySoft. this article does not discuss usage, but just proposes an idea. It doesn't matter if you like other ORM. I emphasize my personal preferences again!

Encapsulation and Encapsulation
// Obtain an object Products entity = BaseDao <Products>. get_SingleEntity_byWhere (Products. _. productID = parID); // update an object Products entity = Fill_Entity (); // The method BaseDao <Products> is omitted for assigning values to an object. update_Entity (entity); // delete all data BaseDao <Products>. delete_Entity (Products. _. productID. in (strIDsCollection. split (','); // The paging with conditional sorting of a single table is discussed later in the article about multi-Table var entityList = BaseDao <Products>. get_Entity_byPage (currentPageIndex, pageSize, where, orderby );

Only a few operations are listed above. As all single table operations are done by BaseDao, transactions and batch operations are also encapsulated in it. Because BaseDao is a generic class, the Where condition in the following method is used, the OrderBy condition, the fields to be queried are all active, so that we can get the best effort when writing a single table. The BaseDao code is as follows:

Public class BaseDao <T> where T: entity {# region "query" /// <summary> /// obtain the object through the condition /// </summary> /// <param name = "where"> </ param> // <returns> if your condition is to obtain a single object, list T [0] is the object </returns> public static List <T> Get_Entity_byWhere (WhereClip where, orderByClip orderby, params Field [] fields) {return DbSession. default. from <T> (). where (where ). select (fields ). orderBy (orderby ). toList ();} /// <summary> /// obtain the able Data Set /// </summary> /// <param name = "where"> </param> /// <param name = "orderby"> </param> // <param name = "fields"> </param> // <returns> </returns> public static DataTable Get_Entity_byWhere_ToTable (whereClip where, orderByClip orderby, params Field [] fields) {return DbSession. default. from <T> (). where (where ). select (fields ). orderBy (orderby ). toTable ();} /// <summary> /// obtain a single object by using conditions /// </summary> /// <param name = "where"> </param> /// <param name = "fields"> </param> // <returns> </returns> public static T Get_SingleEntity_byWhere (WhereClip where, params Field [] fields) {return DbSession. default. from <T> (). where (where ). select (fields ). toSingle ();} /// <summary> /// obtain all the data /// </summary> /// <returns> datatable </returns> public static DataTable Get_AllData_Table () {return DbSession. default. from <T> (). toTable ();} /// <summary> /// obtain all the data /// </summary> /// <returns> List <T> </returns> public static List <T> get_AllData_List () {return DbSession. default. from <T> (). toList ();} /// <summary> /// obtain the top data records /// </summary> /// <param name = "top"> </param> /// <param name = "where"> </param> /// <param name = "orderby"> </param> /// <returns> </returns> public static List <T> Get_Entitys_ByTop (int top, whereClip where, OrderByClip orderby, params Field [] fields) {if (top = 0) throw new Exception ("top value cannot be 0"); return DbSession. default. from <T> (). where (where ). select (fields ). getTop (top ). orderBy (orderby ). toList ();} # endregion # region "Update" /// <summary> /// update a record /// </summary> /// <param name = "entity"> </param >/// <returns> </returns> public static bool Update_Entity (T entity) {entity. attach (); return DbSession. default. save <T> (entity)> 0 ;} /// <summary> /// update an object using multiple conditions /// </summary> /// <param name = "fields"> </param> /// <param name = "values"> </param> /// <param name = "where"> </param> /// <returns> </returns> public static bool update_Entity_byWhere (Field [] fields, object [] values, WhereClip where) {return DbSession. default. update <T> (fields, values, where)> 0 ;} /// <summary> /// update an object using a single condition /// </summary> /// <param name = "fields"> </param> // <param name = "values"> </param> // <param name = "where"> </param> // <returns> </returns> public static bool Update_Entity_byWhere (Field filed, object value, WhereClip where) {return DbSession. default. update <T> (filed, value, where)> 0 ;} # endregion # region "delete" // <summary> // delete a record from an object /// </summary> /// <param name = "entity"> </ param> // <returns> </returns> public static bool delete_Entity (T entity) {return DbSession. default. delete <T> (entity)> 0 ;} /// <summary> /// delete from the primary key ID set /// </summary> /// <param name = "idList"> </param> // <returns> </returns> public static int Delete_Entitys (List <string> idList) {return Delete_Entitys (idList. toArray ());} /// <summary> /// delete from the primary key ID set /// </summary> /// <param name = "idList"> </param> // <returns> </returns> public static int Delete_Entitys (string [] idList) {return DbSession. default. delete <T> (idList) ;}/// <summary> /// you can specify the where = T condition when deleting objects in batches by using the condition. ID. in (obj []) /// </summary> /// <param name = "where"> </param> /// <returns> </returns> public static bool Delete_Entity (WhereClip where) {return DbSession. default. delete <T> (where)> 0 ;}# endregion # region "added" /// <summary> // add an new record, /// </summary> /// <param name = "entity"> </param> /// <returns> </returns> public static bool Add_Entity (T entity) {return DbSession. default. save <T> (entity)> 0 ;} # endregion # region "pagination" /// <summary> // The total number of records required for displaying data by PAGE /// </summary> /// <param name = "currentPageindex"> current page number </param> /// <param name = "pageSize"> pagesize </param> /// <param name = "where"> WhereClient </param> // /<param name = "orderby"> OrderByClip </param> // <param name = "record"> total number of records </param> // <returns> </ returns> public static List <T> Get_Entity_byPage (int currentPageindex, int pageSize, WhereClip where, OrderByClip orderby, out int record, params Field [] fields) {record = Get_Entity_Record (where); return Get_Entity_byPage (currentPageindex, pageSize, where, orderby, fields );} /// <summary> /// return able by PAGE /// </summary> /// <param name = "currentPageindex"> </param> /// <param name = "pageSize"> </param> // <param name = "where"> </param> // <param name = "orderby"> </param>/ // <param name = "record"> total records </param> /// <param name = "fields"> </param> /// <returns> </returns> public static DataTable Get_Entity_byPage_ToTable (int currentPageindex, int pageSize, WhereClip where, OrderByClip orderby, out int record, params Field [] fields) {record = Get_Entity_Record (where); return partition (currentPageindex, pageSize, where, orderby, fields );} /// <summary> /// obtain the number of records in the table using the conditions /// </summary> /// <param name = "where"> </param> // /<returns> </returns> public static int Get_Entity_Record (WhereClip where) {return DbSession. default. from <T> (). where (where ). count ();} /// <summary> /// the total number of records that do not need to be recorded on the page. // </summary> /// <param name = "currentPageindex"> current page number </param> /// <param name = "pageSize"> pagesize </param> /// <param name = "where"> WhereClient </param> /// <param name = "orderby"> OrderByClip </param> // <returns> </returns> public static List <T> Get_Entity_byPage (int currentPageindex, int pageSize, WhereClip where, OrderByClip orderby, params Field [] fields) {return DbSession. default. from <T> (). where (where ). select (fields ). orderBy (orderby ). getPage (pageSize ). toList (currentPageindex );} /// <summary> /// return able by PAGE /// </summary> /// <param name = "currentPageindex"> </param> /// <param name = "pageSize"> </param> // <param name = "where"> </param> // <param name = "orderby"> </param>/ // <param name = "fields"> </param> // <returns> </returns> public static DataTable Get_Entity_byPage_ToTable (int currentPageindex, int pageSize, WhereClip where, OrderByClip orderby, params Field [] fields) {return DbSession. default. from <T> (). where (where ). select (fields ). orderBy (orderby ). getPage (pageSize ). toTable (currentPageindex );} # endregion # region "batch processing" /// <summary> /// batch add with things /// </summary> /// <param name = "entityList"> </param> // <returns> </returns> public static bool BatchAdd_Entitys (List <T> entityList) {// use a transaction to insert using (DbTrans trans = DbSession. default. beginTrans () {try {DbBatch batch = trans. beginBatch (entityList. count); entityList. forEach (item => {item. detach (); batch. save (item) ;}); batch. process (); trans. commit (); return true;} catch {trans. rollback (); return false ;}}} /// <summary> /// batch update /// </summary> /// <param name = "entityList"> </param> /// <returns> </returns> public static bool BatchUpdate_Entitys (List <T> entityList) {// use a transaction to update using (DbTrans trans = DbSession. default. beginTrans () {try {DbBatch batch = trans. beginBatch (entityList. count); entityList. forEach (item => {item. attach (); batch. save (item) ;}); batch. process (); trans. commit (); return true;} catch {trans. rollback (); return false ;}}# endregion}

We can see that the input object can be passed into this class, including conditions, sorting, and the fields to be obtained. This makes this operation suitable for most operations on a single table.

This encapsulation and Orm flexibility make the project layer very simple. The business layer only needs to focus on complex business logic and multi-Table Association. the solution for multi-Table Association will be described later. other ORM can also be used for this encapsulation, greatly improving work efficiency.

Old saying: performance issues

First of all, I want to talk about how the efficiency of the ORM works without seeing it. I will weigh the improvement of the work efficiency brought by it and the conciseness of the development model, is it suitable for your application scenarios. In addition, we can solve many performance problems in other ways, such as cache solutions, such as cache-based timed and quantitative commit when concurrent data is inserted, and database read/write splitting...

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.