Let your Dapper call open source like EF ~!, Dapperef

Source: Internet
Author: User

Let your Dapper call open source like EF ~!, Dapperef
What is Dapper?

Dapper is a lightweight ORM framework! Developed by Google, the efficiency of N orm frameworks is the fastest! Tested data is 12-20 times higher than EF

Stackoverflow, the largest foreign technology website, was developed based on this Orm framework and is very simple.

 

Why Dapper?

 

Body

I made a brief introduction to Dapper. There are a lot of articles on the advantages of Dapper on the Internet. I will not go into detail here! I believe many Yuanyuan companies have used Microsoft's EF framework?

EF has many advantages: "CodeFist code first", "No need to write all required ADO for data access. NET Pipeline Code "," use C # code to write all the data access logic, rather than writing SQL queries and stored procedures "and so on ....

But the biggest drawback is that it is too bloated and its performance is not bad! For this reason! Many companies have abandoned this framework and choose to use their own encapsulated SqlHelper class or other third-party orm frameworks.

However, due to EF's bad habits, the younger brother felt that the other ORM frameworks were not quick and convenient, so he thought that he could not change the most popular Dapper to a call mode similar to EF! So this blog is coming out!

 

Let's take a look at several examples.

Operate data in a data table

-------------------------- Example (1 )------------------------

// First, Dapper native obtains data string query = "SELECT * FROM Book WHERE Id = @ id"; book = conn. query <Book> (query, new {id = id }). singleOrDefault ();

// Var id = 0001 after Dapper modification; // query the data var entity = _ repExample. GetById (Id) based on the id );

-------------------------- Example (2 )----------------------------

// Dapper native obtains the total number of data table items var count = conn. executeScalar <int> ("select count (*) FROM customers Where Type = A"); // obtain the total number of data tables var count01 = _ repExample after the Dapper is modified. getCount (); // var count02 = _ repExample without conditions. getCount (item => item. age> 30 & item. createTime <new DateTime (classification 8, 10); // with conditions


------------------------- Example (3 )-------------------------------------
// Dapper native obtains the first data var entity = _ Conn. QueryFirst <T_Example> ("Select * from T_Example where Name = g ");
// Var entity = _ repExample. GetFist (item => item. Name = "g") after the Dapper is modified; // use lambda


------------------------------- Example (4) --------------------------------------- // Dapper native checks whether the data exists var count = conn. executeScalar <int> ("select count (*) FROM customers Where Type = A"); return count> 0; // determine whether the count value is greater than 0 // After Dapper modification, bool IsExist = _ repExample. exists (item => item. phone = "10086 ");

 

Add, delete, modify, and delete data

--------------------------- Example (1 )-------------------------------------------

// Dapper native Insert Data Book book = new Book (); book. name = "C # essence"; string query = "insert into Book (Name) VALUES (@ name)"; // operate on the object conn. execute (query, book); // assign values directly to conn. execute (query, new {name = "C # essence "});

// After modification by the younger brother Dapper
Var item = _ repExample. Insert (entity); // Insert bool succ = _ repExample. Insert (entityList); // batch Insert built-in transaction operation failure one of the errors is rolled back

-------------------------- Example (2 )-------------------------------------------
// Daper native Update UPDATE data string query = "Update Book SET Name = @ name WHERE id = @ id"; conn. execute (query, book); // After the Dapper is modified

Var entity = new T_Example (); var item = _ repExample. update (entity); // a single model Update bool succ = _ repExample. update (entityList); // batch Update built-in transaction operation failure. One of the errors is rolled back bool succ = _ repExample. update (id, new {Name = "James", Age = 22}); // some fields are updated.
---------------------------- Example (3 )--------------------------------------------
// Dapper native Delete DELETE data string query = "Delete FROM Book WHERE id = @ id"; conn. execute (query, book); conn. execute (query, new {id = id}); // var entity = new T_Example (); var item = _ repExample after Dapper modification. delete (id); // Delete a single bool succ = _ repExample. delete (IEnumerable <object> ids); // batch Delete built-in transaction operations fail. One of the errors is rolled back.

 

Data Query operation instance

// Dapper native query string query = "SELECT * FROM Book Where Name Like % C # %"; // No parameter query, return to the list, the parameter query method is the same as the previous parameter value assignment method. Conn. Query <Book> (query). ToList (); // after modification by younger brother

_ RepExample. getAll (); // get all data in the data table _ repExample. getAll (new {Name = true}); // sort fields to true ascending order to false descending order // obtain data based on conditions _ repExample. getTableData (item => item. age> 20 & item. name. contains ("C # %"); // start with fuzzy query _ repExample. getTableData (item => item. age> 20 & item. name. contains ("% understand C # %"); // include fuzzy query _ repExample. getTableData (item => item. age> 20 & item. name. contains ("% Join Skeet"); // fuzzy query at the end // long pageTotal query by page; // output the total number of items in the current table _ repExample. getPageData (1, 20, out pageTotal); _ repExample. getPageData (1, 20, out pageTotal, item => item. age> 30); // obtain View data based on the conditions by PAGE // _ repExample. queryView <ExampleBusinessModel> ("View _ View name"); _ repExample. querySql <T_Example> ("select Name, Age, Phone from T_Example"); // execute native SQL

 

 

Multi-Table transaction operation instance

// The dapper transaction processing will not post the code. // only paste the code var succ = _ repExample after the Dapper is modified. submit (tran =>{// here perform your specific transaction operations _ repExample. insertTran (new T_Example (), tran); // insert A data repLog in table. deleteTran (id, tran); // Delete the data in Table B again });

 

There are still a lot of encapsulation methods that the younger brother has no time to expand. Now the younger brother has open-source code and you can expand it according to your own needs.

Although the younger brother has only three and a half years of programming experience, he thinks that the Code or the standard source code has comments, so he can easily understand the comments. You are welcome to ask the younger brother QQ407015811.

 

 

I will release the source code after work in the evening!

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.