Use dapperextensions for simple warehousing

Source: Internet
Author: User

Dapper is a lightweight ORM framework that is just a idbconnection extension file. So we need to write a lot of SQL, but the code that writes Crud is always boring. All of them have dapperextensions. Dapperextensions provides more extensions to dapper, which enables CRUD operations and simple query functions without writing SQL. Don't say much or go straight to the code.

  classConnectionFactory { Public StaticIDbConnection createconnection<t> ()whereT:idbconnection,New() {IDbConnection connection=NewT (); Connection. ConnectionString=connectionconfig.connectionstring; Connection.            Open (); returnconnection; }         Public Staticidbconnection createsqlconnection () {returnCreateconnection<sqlconnection>(); }    }
Interface Irepository<t>    {        IEnumerable<T> GetList ();        T Get (object  ID);         BOOL Update (t t);        T Insert (t apply);         BOOL Delete (t t);        IEnumerableObjectobject  param);    }
classSqlrepository<t>: irepository<t>whereT:class, ientity { Public VirtualIenumerable<t>GetList () {using(varconn =connectionfactory.createsqlconnection ()) {                returnConn. Getlist<t>(); }        }         Public VirtualT Get (ObjectID) {using(varconn =connectionfactory.createsqlconnection ()) {                returnConn. Get<t>(ID); }        }         Public Virtual BOOLUpdate (T t) {using(varconn =connectionfactory.createsqlconnection ()) {                returnConn.            Update (t); }        }         Public Virtualt Insert (t apply) {using(varconn =connectionfactory.createsqlconnection ()) {Conn.                Insert (apply); returnapply; }        }         Public Virtual BOOLDelete (T t) {using(varconn =connectionfactory.createsqlconnection ()) {                returnConn.            Delete (t); }        }         Public VirtualIenumerable<t> Find (Expression<func<t,Object>> expression,operator op,Objectparam) {            using(varconn =connectionfactory.createsqlconnection ()) {                returnConn. Getlist<t> (predicates.field<t>(expression, op, param)); }        }    }

Well, that's so simple.

Use dapperextensions for simple warehousing

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.