sunzxh 2020-05-10
看下我们上一节搭建的架构,现在开始从事底层的封装
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 } }
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继承仓储基接口,简单实现增删改查和无参的存储过程
using Dapper; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; namespace Dinner.Dapper { public class RepositoryBase<T> : IRepositoryBase<T> { public async Task Delete(Guid Id, string deleteSql) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { await conn.ExecuteAsync(deleteSql, new { Id = Id }); } } public async Task<T> Detail(Guid Id, string detailSql) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { //string querySql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users WHERE "; return await conn.QueryFirstOrDefaultAsync<T>(detailSql, new { Id = Id }); } } /// <summary> /// 无参存储过程 /// </summary> /// <param name="SPName"></param> /// <returns></returns> public async Task<List<T>> ExecQuerySP(string SPName) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { return await Task.Run(() => conn.Query<T>(SPName, null, null, true, null, CommandType.StoredProcedure).ToList()); } } public async Task Insert(T entity, string insertSql) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { await conn.ExecuteAsync(insertSql, entity); } } public async Task<List<T>> Select(string selectSql) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { //string selectSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users"; return await Task.Run(()=>conn.Query<T>(selectSql).ToList()); } } public async Task Update(T entity, string updateSql) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { await conn.ExecuteAsync(updateSql, entity); } } } }
上面这些就是底层基本的封装,下一节我们来添加实体和仓储
源码地址: https://github.com/wangyulong0505/Dinner