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