天下之事常成于困约,而败于奢靡。这篇文章主要讲述NetCore+Dapper WebApi架构搭建:底层封装相关的知识,希望能为你提供帮助。
看下我们上一节搭建的架构,现在开始从事底层的封装
1、首先需要一个实体的接口IEntity
namespace Dinner.Dapper { public interface IEntity< TPrimaryKey> { TPrimaryKey Id { get; set; } } }
2、由于所有的数据库实体都是有主键的,所以这里定义一个BaseModel,这里我用的主键是Guid,你们可以换成int
using System; namespace Dinner.Dapper { public class BaseModel : IEntity< Guid> { public Guid Id { get; set; } } }
3、接下来是封装Dapper的SqlServer配置DataBaseConfig(Redis的配置你们可以忽略),不要忘记添加Dapper的Nuget包
using StackExchange.Redis; using System.Data; using System.Data.SqlClient; namespace Dinner.Dapper { public class DataBaseConfig { #region SqlServer链接配置private static string DefaultSqlConnectionString = @"Data Source=localhost; Initial Catalog=Dinner; User ID=sa; Password=123456; "; private static string DefaultRedisString = "localhost, abortConnect=false"; private static ConnectionMultiplexer redis; public static IDbConnection GetSqlConnection(string sqlConnectionString = null) { if (string.IsNullOrWhiteSpace(sqlConnectionString)) { sqlConnectionString = DefaultSqlConnectionString; } IDbConnection conn = new SqlConnection(sqlConnectionString); conn.Open(); return conn; }#endregion#region Redis链接配置private static ConnectionMultiplexer GetRedis(string redisString = null) { if (string.IsNullOrWhiteSpace(redisString)) { redisString = DefaultRedisString; } if (redis == null || redis.IsConnected) { redis = ConnectionMultiplexer.Connect(redisString); } return redis; }#endregion } }
【NetCore+Dapper WebApi架构搭建(底层封装)】
4、然后是仓储基接口IRepositoryBase,添加了基本的增删改查和一个无参的存储过程
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Dinner.Dapper { public interface IRepositoryBase< T> { Task Insert(T entity, string insertSql); Task Update(T entity, string updateSql); Task Delete(Guid Id, string deleteSql); Task< List< T> > Select(string selectSql); Task< T> Detail(Guid Id, string detailSql); /// < summary> /// 无参存储过程 /// < /summary> /// < param name="SPName"> < /param> /// < param name="args"> < /param> /// < returns> < /returns> Task< List< T> > ExecQuerySP(string SPName); } }
5、仓储基实现类RepositoryBase继承仓储基接口,简单实现增删改查和无参的存储过程
1 using Dapper; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Linq; 6 using System.Threading.Tasks; 7 8 namespace Dinner.Dapper 9 { 10public class RepositoryBase< T> : IRepositoryBase< T> 11{ 12public async Task Delete(Guid Id, string deleteSql) 13{ 14using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 15{ 16await conn.ExecuteAsync(deleteSql, new { Id = Id }); 17} 18} 19 20public async Task< T> Detail(Guid Id, string detailSql) 21{ 22using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 23{ 24//string querySql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users WHERE Id=@Id"; 25return await conn.QueryFirstOrDefaultAsync< T> (detailSql, new { Id = Id }); 26} 27} 28 29/// < summary> 30/// 无参存储过程 31/// < /summary> 32/// < param name="SPName"> < /param> 33/// < returns> < /returns> 34public async Task< List< T> > ExecQuerySP(string SPName) 35{ 36using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 37{ 38return await Task.Run(() => conn.Query< T> (SPName, null, null, true, null, CommandType.StoredProcedure).ToList()); 39} 40} 41 42public async Task Insert(T entity, string insertSql) 43{ 44using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 45{ 46await conn.ExecuteAsync(insertSql, entity); 47} 48} 49 50public async Task< List< T> > Select(string selectSql) 51{ 52using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 53{ 54//string selectSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users"; 55return await Task.Run(()=> conn.Query< T> (selectSql).ToList()); 56} 57} 58 59public async Task Update(T entity, string updateSql) 60{ 61using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 62{ 63await conn.ExecuteAsync(updateSql, entity); 64} 65} 66} 67 }
上面这些就是底层基本的封装,下一节我们来添加实体和仓储
源码地址: https://github.com/wangyulong0505/Dinner
来源:https://www.cnblogs.com/wangyulong/p/8961061.html
推荐阅读
- 2020最新统计,年薪50万+的Android开发者所必备的知识体系架构,你都会吗((超多图哦!))
- Android Handler 机制(Hander 机制深入探究问题梳理)
- Android Activity销毁
- Android Intent 隐式
- Appium自动化测试教程-自学网-monkey参数
- Appium自动化测试教程-自学网-Package与Activity
- Appium自动化测试教程-自学网-monkey事件
- Linux部署Django(报错 nohup: ignoring input and appending output to ‘nohup.out’)
- Ubuntu下安卓模拟器的选择