We all know that ORM is called object Relationship Mapper, which is the ability to map our db with object, and there are many ORM frameworks on the market, including a framework
Called Dapper, and is known as the King of the ORM.
One: Why Choose Dapper
1. Superior Performance:
In fact, on the major sites, we will probably see such a contrast, in more than 500 times the performance of the process of Poco serialization, we found that Dapper is second,
Of course, the first person who can not surpass, the more the bottom of course long faster, but also the more trouble. Like who can exceed the "01 code"???
2. Support Multi-database
The nature of supporting multiple databases is because Dapper is a way to extend the IDbConnection interface, such as the SqlMapper.cs you see, and once you do that, we know that
Sqlconnection,mysqlconnection,oracleconnection are inherited from the DbConnection, and DbConnection is the realization of the IDbConnection interface, right ...
Two: Install dapper
There are usually two ways to install dapper:
1. Installing through NuGet
If you don't know how to install with NuGet, or if you don't know what install-package is, you can look it up on browser, like this:
Then we copy to the package console and try it out.
2. Get the source code on GitHub.
Why to get the source code, is because with Ilspy debug dapper source code too laborious, after all, is now asynchronous programming, from Ilspy see are anonymous method many can not penetrate, nonsense not much
Say, we just pull out the dapper folder and copy it to our solution, like this:
Three: Fast curd operation
In fact, the operation of the database is curd, before the operation we have a users table.
1. Configure the Users table
CREATE TABLE [dbo]. [Users] ( [UserID] [int] IDENTITY () not NULL, [UserName] [varchar] () NULL, [Email] [varchar] (+) NULL, [ Address] [varchar] (+) NULL, CONSTRAINT [pk_users] PRIMARY KEY CLUSTERED ( [UserID] ASC) with (pad_index = OFF, ST Atistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [PRIMARY]) On [PRIMARY]
2. Insert operation
Generally, there are two kinds of insert operations:
<1> Single insert operation
This is a simple parametric insert, but also can be plugged into the anonymous type, right, compared with the original SqlParameter, is not much simpler???
static void Main (string[] args) { IDbConnection connection = new SqlConnection ("Data source=.;i Nitial catalog=datamip;integrated security=true; Multipleactiveresultsets=true "); var result = connection. Execute ("Insert into Users values (@UserName, @Email, @Address)", new {UserName = "Jack", email = "[Email protected]" , Address = "Shanghai"});
<2> Insertbulk operation
Since it is the bulk operation, it is definitely a bulk INSERT, we have to do is the above "anonymous object" into "anonymous object collection" on it ... For ease of operation, this defines
A users class, such as the following ...
static void Main (string[] args) { IDbConnection connection = new SqlConnection ("Data source=.;i Nitial catalog=datamip;integrated security=true; Multipleactiveresultsets=true "); var result = connection. Execute ("Insert into Users values (@UserName, @Email, @Address)", // new {UserName = "Jack", email = "[Email prot Ected] ", Address =" Shanghai "}); var userslist = enumerable.range (0, 10). Select (i + = new Users () { Email = i + "qq.com", Address = "Anhui", UserName = i + "Jack" }); var result = connection. Execute ("Insert into Users values (@UserName, @Email, @Address)", userslist); }
2. Query operation
In fact, there are too many articles in dapper on the query. In this article we will follow the simplest parametric query just fine ... For example, I want to find Username=jack records, as follows:
1 static void Main (string[] args) 2 {3 idbconnection connection = new SqlConnection ("Data source=.;i Nitial catalog=datamip;integrated security=true; Multipleactiveresultsets=true "); 4 5 var query = connection. Query<users> ("select * from Users where [email protected]", new {UserName = "Jack"}); 6 7 }
The bright spot on the chart is the ability to automate mapper to our object, which is something we DataReader can't do, right?
3.update operation
This mode of operation, we still use the Execute method to implement, and insert is a form of OH.
4. Delete operation
Here I still use the parameterized form to delete the userid=10 this record, the way is as follows:
The final SQL table is shown below, and you can see that the userid=11 record has been correctly modified, and the records of userid=10 have been deleted .... Of course, there are plenty of interesting places to dapper.
Orm-dapper Quick Learning