Dapper是一个轻量级的ORM框架,它只是一个IDbConnection的扩展文件。所以我们需要手写很多SQL,但是写CRUD的代码总是很无趣的。所有就有了DapperExtensions。DapperExtensions对Dapper提供了更多的扩展,可以不用写SQL就实现CRUD操作跟简单的查询功能。话不多说还是直接上代码吧。
class ConnectionFactory { public static IDbConnection CreateConnection<T>() where T:IDbConnection,new () { IDbConnection connection = new T(); connection.ConnectionString = ConnectionConfig.ConnectionString; connection.Open(); return connection; } public static IDbConnection CreateSqlConnection() { return CreateConnection<SqlConnection>(); } }
interface IRepository<T> { IEnumerable<T> GetList(); T Get(object id); bool Update(T t); T Insert(T apply); bool Delete(T t); IEnumerable<T> Find(Expression<Func<T, object>> expression, Operator op, object param); }
class SqlRepository<T> : IRepository<T> where T : class, IEntity { public virtual IEnumerable<T> GetList() { using (var conn = ConnectionFactory.CreateSqlConnection()) { return conn.GetList<T>(); } } public virtual T Get(object id) { using (var conn = ConnectionFactory.CreateSqlConnection()) { return conn.Get<T>(id); } } public virtual bool Update(T t) { using (var conn = ConnectionFactory.CreateSqlConnection()) { return conn.Update(t); } } public virtual T Insert(T apply) { using (var conn = ConnectionFactory.CreateSqlConnection()) { conn.Insert(apply); return apply; } } public virtual bool Delete(T t) { using (var conn = ConnectionFactory.CreateSqlConnection()) { return conn.Delete(t); } } public virtual IEnumerable<T> Find(Expression<Func<T,object>> expression,Operator op,object param) { using (var conn = ConnectionFactory.CreateSqlConnection()) { return conn.GetList<T>(Predicates.Field<T>(expression, op, param)); } } }
恩,就是这么简单。
时间: 2024-10-22 07:34:11