Build your own practical. net architecture (3) "orm-dapper+dapperextensions"
Source: Internet
Author: User
<span id="Label3"></p><p><p>Now mature ORM abound, here only to introduce the use of dapper (at least I use it, has been applied to the project, small partners feedback can also).</p></p><p><p>Advantages:</p></p><p><p>1, Open source, Light weight, small, easy to get Started.</p></p><p><p>2, supporting the database is quite a lot of, <strong>mysql,sqllite,sqlserver,oracle</strong> , such as a series of databases.</p></p><p><p>3, dapper principle through emit reflection IDataReader sequence queue to quickly get and produce objects. It looks like a good performance.</p></p><p><p>Disadvantages:</p></p><p><p>As an ORM is too lightweight, the ability to automatically generate SQL based on objects or blank, you need to expand their own,</p></p><p><p>Of course, This is also the advantage, good voice of the mentors often say that someone is a blank paper ...</p></p><p><p></p></p><p><p>So there are many mature extension projects for Dapper, dapper.rainbow, Dapper.contrib,dapperextensions.</p></p><p><p>We are here to introduce <strong>dapperextensions</strong>.</p></p><p><p></p></p><p><p>Dapper-dot-net source:<span style="color: #888888;">https://github.com/StackExchange/dapper-dot-net <span style="color: #000000;">(update frequency fast, The project contains a variety of except Dapper-extensions Extension Project)</span> </span></p></p><p><p>Dapper-extensions source:<span style="color: #888888;">https://github.com/tmsmith/Dapper-Extensions</span></p></p><p><p><strong>Advantages of Dapper-extensions</strong> :</p></p><p><p>1. Open Source</p></p><p><p>2, for dapper encapsulation of common crud methods, there is a separate query syntax.</p></p><p><p>3, need to map the entity class itself 0 configuration, no need to add features or anything. is handled by a separate mapping class, you can set the class map to the DB alias, the alias of the field, and so On.</p></p><p><p>Disadvantages of Dapper-extensions:</p></p><p><p>1, several years have not been updated</p></p><p><p>2, does not support Oracle (no Oracle dialect, The database syntax is different, has been done)</p></p><p><p>3. Cannot support multiple data at the same time (already done)</p></p><p><p>4, Some of the code some bugs (found all done)</p></p><p><p></p></p><p><p>of course, all of these shortcomings have been taken care of.</p></p><p><p>Here is a <strong>brief</strong> introduction to the basic syntax of Dapper.</p></p><p><p>Dapper A. CS file, which can be compiled directly into the project code or directly referencing the DLL File.</p></p><p><p>Dapper operation on DB relies on connection, in order to support multi-library, we use IDbConnection conn</p></p><pre><pre><span style="color: #0000ff;">using</span> (idbconnection conn =<span style="color: #000000;"> getconnection ()) { </span><span style="color: #0000ff;">const</span><span style="color: #0000ff;">string</span><span style="color: #800000;">"</span><span style="color: #800000;">select * FROM XO ORDER BY id desc</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">RETURN</span> Conn. Query<xoentity> (query,<span style="color: #0000ff;">null</span><span style="color: #000000;">); }</span></pre></pre><p><p>Here is the syntax with parameters</p></p><pre><pre>int xoid=666; Variable PRIMARY key<br><span style="color: #0000ff;">using</span> (idbconnection conn =<span style="color: #000000;"> getconnection ()) { </span><span style="color: #0000ff;">const</span><span style="color: #0000ff;">string</span><span style="color: #800000;">"</span><span style="color: #800000;">select * from XO where [email protected]</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">return</span><span style="color: #0000ff;">new</span> {ID = xoid<span style="color: #000000;">}); }</span></pre></pre><p><p></p></p><p><p>Various methods are overloaded with transactional operations, which are supported by general database Operations. But not once execution all need to pass sql, and every time use using, look uncomfortable ah, this ...</p></p><p><p>ok, Here's A <strong>brief</strong> introduction to the basic syntax for using dapper-extensions (the repository mode is used on the basis of dapper-extensions, the code effect is as follows).</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">entity class</span></span>demoentity entity =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">demoentity (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Delete based on entity primary key</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Delete<demoentity><span style="color: #000000;"><span style="color: #000000;">(entity); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Delete based on primary key ID</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Delete<demoentity> (<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Increase</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Insert<demoentity><span style="color: #000000;"><span style="color: #000000;">(entity); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Update</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">BOOL</span></span>result =<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Update<demoentity><span style="color: #000000;"><span style="color: #000000;">(entity); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return entities based on primary key</span></span>entity =<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Getbyid<demoentity> (<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">number of rows returned</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Count<demoentity> (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>{ID =<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;"> }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Query All</span></span>ienumerable<demoentity> list =<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Getall<demoentity><span style="color: #000000;"><span style="color: #000000;">(); IList</span></span><ISort> sort =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>List<isort><span style="color: #000000;"><span style="color: #000000;">(); Sort. ADD (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Sort {propertyname =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ID</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, ascending =<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;"> }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Conditional Query</span></span>List =<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Getlist<demoentity> (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>{ID =<span style="color: #800080;"><span style="color: #800080;">1</span></span>, Name =<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">123</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">}, sort); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">ORM Stitching Condition Query</span></span>Ilist<ipredicate> predlist =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>List<ipredicate><span style="color: #000000;"><span style="color: #000000;">(); Predlist.add (predicates.field</span></span><DemoEntity> (p = p.name, operator.like,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">do not know%</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)); Predlist.add (predicates.field</span></span><DemoEntity> (p = p.id, operator.eq,<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">)); Ipredicategroup Predgroup</span></span>=<span style="color: #000000;"><span style="color: #000000;">predicates.group (groupoperator.and, Predlist.toarray ()); List</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Getlist<demoentity><span style="color: #000000;"><span style="color: #000000;">(predgroup); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Paging Query</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span>Allrowscount =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Getpagelist<demoentity> (<span style="color: #800080;"><span style="color: #800080;">1</span></span>,<span style="color: #800080;"><span style="color: #800080;">Ten</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span>allrowscount,<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>{ID =<span style="color: #800080;"><span style="color: #800080;">1</span></span>}, sort);</pre><p><p></p></p><p><p>After work, not to be continued.</p></p><p><p></p></p><p><p>Build your own practical. net architecture (3) "orm-dapper+dapperextensions"</p></p></span>
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