NetCore+Dapper WbpApi架构搭建(底层封装)

智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述NetCore+Dapper WbpApi架构搭建:底层封装相关的知识,希望能为你提供帮助。
【NetCore+Dapper WbpApi架构搭建(底层封装)】看下我们上一节搭建的架构,现在开始从事底层的封装
1、首先需要一个实体的接口IEntity

1 namespace Dinner.Dapper 2 { 3public interface IEntity< TPrimaryKey> 4{ 5TPrimaryKey Id { get; set; } 6} 7 }

2、由于所有的数据库实体都是有主键的,所以这里定义一个BaseModel,这里我用的主键是Guid,你们可以换成int
1 using System; 2 3 namespace Dinner.Dapper 4 { 5public class BaseModel : IEntity< Guid> 6{ 7public Guid Id { get; set; } 8} 9 }

3、接下来是封装Dapper的SqlServer配置DataBaseConfig(Redis的配置你们可以忽略),不要忘记添加Dapper的Nuget包
1 using StackExchange.Redis; 2 using System.Data; 3 using System.Data.SqlClient; 4 5 namespace Dinner.Dapper 6 { 7public class DataBaseConfig 8{ 9#region SqlServer链接配置 10 11private static string DefaultSqlConnectionString = @"Data Source=localhost; Initial Catalog=Dinner; User ID=sa; Password=123456; "; 12private static string DefaultRedisString = "localhost, abortConnect=false"; 13private static ConnectionMultiplexer redis; 14 15public static IDbConnection GetSqlConnection(string sqlConnectionString = null) 16{ 17if (string.IsNullOrWhiteSpace(sqlConnectionString)) 18{ 19sqlConnectionString = DefaultSqlConnectionString; 20} 21IDbConnection conn = new SqlConnection(sqlConnectionString); 22conn.Open(); 23return conn; 24} 25 26#endregion 27 28#region Redis链接配置 29 30private static ConnectionMultiplexer GetRedis(string redisString = null) 31{ 32if (string.IsNullOrWhiteSpace(redisString)) 33{ 34redisString = DefaultRedisString; 35} 36if (redis == null || redis.IsConnected) 37{ 38redis = ConnectionMultiplexer.Connect(redisString); 39} 40return redis; 41} 42 43#endregion 44} 45 }

4、然后是仓储基接口IRepositoryBase,添加了基本的增删改查和一个无参的存储过程
NetCore+Dapper WbpApi架构搭建(底层封装)

文章图片
NetCore+Dapper WbpApi架构搭建(底层封装)

文章图片
1 using System; 2 using System.Collections.Generic; 3 using System.Threading.Tasks; 4 5 namespace Dinner.Dapper 6 { 7public interface IRepositoryBase< T> 8{ 9Task Insert(T entity, string insertSql); 10 11Task Update(T entity, string updateSql); 12 13Task Delete(Guid Id, string deleteSql); 14 15Task< List< T> > Select(string selectSql); 16 17Task< T> Detail(Guid Id, string detailSql); 18 19/// < summary> 20/// 无参存储过程 21/// < /summary> 22/// < param name="SPName"> < /param> 23/// < param name="args"> < /param> 24/// < returns> < /returns> 25Task< List< T> > ExecQuerySP(string SPName); 26} 27 }

View Code5、仓储基实现类RepositoryBase继承仓储基接口,简单实现增删改查和无参的存储过程
NetCore+Dapper WbpApi架构搭建(底层封装)

文章图片
NetCore+Dapper WbpApi架构搭建(底层封装)

文章图片
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 [email  protected]"; 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 }

View Code上面这些就是底层基本的封装,下一节我们来添加实体和仓储

    推荐阅读