Dapper is a lightweight ORM class. The Code is a sqlmapper. CS file, mainly the extension method of idbconnection, a very small DLL of 40 K after compilation. Official site http://code.google.com/p/dapper-dot-net/, can also be installed through nuget
- Dapper is fast. The speed of dapper is close to that of idatareader.
- Dapper supports mainstream databases such as MySQL, sqllite, mssql2000, mssql2005, and Oracle.
- Objects that support multi-table parallel operation. It supports one-to-many relationships and is not invasive.
- The principle is that emit reflects the sequence queue of idatareader to quickly obtain and generate objects.
- Dapper syntax is very simple. Database design is not required
Query () method:
Query () is an extended idbconnection method and is overloaded. It extracts information from the database and fills in our business object model.
VaR counters = new list <tuple <int, performancecounter> ();
Using (VAR conn = new sqlconnection (configurationmanager. connectionstrings ["sqldiagnosticsdb"]. connectionstring ))
{
Conn. open ();
String SQL = string. Format ("select ID, servicename, categoryname, countername, InstanceName from service_counters where machinename = '{0}'", machinename );
Foreach (VAR counter in conn. query <servicecounter> (SQL ))
{
Logger. infoformat (@ "creating performance counter: {0 }\{ 1 }\{ 2 }\{ 3}", counter. machinename ?? ".", Counter. categoryname,
Counter. countername, counter. InstanceName );
VaR perfcounter = new performancecounter (counter. categoryname, counter. countername, counter. InstanceName, counter. machinename ?? ".");
Counters. Add (New tuple <int, performancecounter> (counter. ID, perfcounter ));
// First value doesn' t matter so we shoshould call the counter at least once
Try {perfcounter. nextvalue ();}
Catch {}
}
}
The following is the definition of servicecounter.
Public class servicecounter
{
Public int ID {Get; set ;}
Public String servicename {Get; set ;}
Public String machinename {Get; set ;}
Public String categoryname {Get; set ;}
Public String countername {Get; set ;}
Public String InstanceName {Get; set ;}
Public String displayname {Get; set ;}
Public String displaytype {Get; set ;}
Public override string tostring ()
{
Return string. Format (@ "{0} \ {1} \ {2} \ {3}", machinename ?? ".", Categoryname, countername, InstanceName );
}
}
Dapper can also load nested objects. In this case, a category object is returned considering the category attribute of news.
1. When filling in nested objects, You have to execute the tolist <> method. Otherwise, the executereader must have enabled and available connections. The current status of the connection is closed, and no error is reported for a single object. It is estimated that the connection is closed after using, and the nested object executes executereader during map, the list set must be returned before using ends.
2. There are many parameters for nested objects, mainly the first two parameters. If other parameters are useless, they can be set to null. Note the spliton parameter. This parameter cannot be blank. Otherwise, an error is returned because the object is a reference. [Spliton parameter means to read the split column of the second object and read the second object from which column. If the auto-increment column in the table is ID, you can set this parameter to "ID "].
Execute Method:
Just as the query method retrieves data, the execute method does not retrieve data. It is very similar to the query method, but it returns the total number of results (the number of affected rows) rather than a collection of objects (such: insert update and delete ].
Private void saveservicesnapshots (ienumerable <servicecountersnapshot> snapshots)
{
Using (VAR conn = new sqlconnection (configurationmanager. connectionstrings ["sqldiagnosticsdb"]. connectionstring ))
{
Conn. open ();
Foreach (VAR snapshot in snapshots)
{
// Insert new snapshot to the database
Conn. Execute (
@ "Insert into service_counter_snapshots (servicecounterid, snapshotmachinename, creationtimeutc, servicecountervalue) values (
@ Servicecounterid, @ snapshotmachinename, @ creationtimeutc, @ servicecountervalue) ", snapshot );
}
}
}
Servicecountersnapshot is defined as follows:
Public class servicecountersnapshot
{
Public int ID {Get; set ;}
Public int servicecounterid {Get; set ;}
/// <Summary>
/// Machine on which the snapshot was taken.
/// </Summary>
Public String snapshotmachinename {Get; set ;}
Public datetime creationtimeutc {Get; set ;}
Public float? Servicecountervalue {Get; set ;}
}
A look at dapper. net
Examples of dapper usage and extension