我是靠谱客的博主 温婉香烟,最近开发中收集的这篇文章主要介绍android web api 封装,NetCore+Dapper WebApi架构搭建(二):底层封装(示例代码),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

看下我们上一节搭建的架构,现在开始从事底层的封装

1、首先需要一个实体的接口IEntity

fz.gif

1 namespace Dinner.Dapper

2 {

3 public interface IEntity

4 {

5 TPrimaryKey Id { get; set; }

6 }

7 }

fz.gif

2、由于所有的数据库实体都是有主键的,所以这里定义一个BaseModel,这里我用的主键是Guid,你们可以换成int

fz.gif

1 using System;

2

3 namespace Dinner.Dapper

4 {

5 public class BaseModel : IEntity

6 {

7 public Guid Id { get; set; }

8 }

9 }

fz.gif

3、接下来是封装Dapper的SqlServer配置DataBaseConfig(Redis的配置你们可以忽略),不要忘记添加Dapper的Nuget包

fz.gif

1 using StackExchange.Redis;

2 using System.Data;

3 using System.Data.SqlClient;

4

5 namespace Dinner.Dapper

6 {

7 public class DataBaseConfig

8 {

9 #region SqlServer链接配置

10

11 private static string DefaultSqlConnectionString = @"Data Source=localhost;Initial Catalog=Dinner;User ID=sa;Password=123456;";

12 private static string DefaultRedisString = "localhost, abortConnect=false";

13 private static ConnectionMultiplexer redis;

14

15 public static IDbConnection GetSqlConnection(string sqlConnectionString = null)

16 {

17 if (string.IsNullOrWhiteSpace(sqlConnectionString))

18 {

19 sqlConnectionString = DefaultSqlConnectionString;

20 }

21 IDbConnection conn = new SqlConnection(sqlConnectionString);

22 conn.Open();

23 return conn;

24 }

25

26 #endregion

27

28 #region Redis链接配置

29

30 private static ConnectionMultiplexer GetRedis(string redisString = null)

31 {

32 if (string.IsNullOrWhiteSpace(redisString))

33 {

34 redisString = DefaultRedisString;

35 }

36 if (redis == null || redis.IsConnected)

37 {

38 redis = ConnectionMultiplexer.Connect(redisString);

39 }

40 return redis;

41 }

42

43 #endregion

44 }

45 }

fz.gif

4、然后是仓储基接口IRepositoryBase,添加了基本的增删改查和一个无参的存储过程

jian.gif

fz.gif

1 using System;

2 using System.Collections.Generic;

3 using System.Threading.Tasks;

4

5 namespace Dinner.Dapper

6 {

7 public interface IRepositoryBase

8 {

9 Task Insert(T entity, string insertSql);

10

11 Task Update(T entity, string updateSql);

12

13 Task Delete(Guid Id, string deleteSql);

14

15 Task> Select(string selectSql);

16

17 Task Detail(Guid Id, string detailSql);

18

19 ///

20 /// 无参存储过程

21 ///

22 ///

23 ///

24 ///

25 Task> ExecQuerySP(string SPName);

26 }

27 }

fz.gif

5、仓储基实现类RepositoryBase继承仓储基接口,简单实现增删改查和无参的存储过程

jian.gif

fz.gif

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 {

10 public class RepositoryBase : IRepositoryBase

11 {

12 public async Task Delete(Guid Id, string deleteSql)

13 {

14 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

15 {

16 await conn.ExecuteAsync(deleteSql, new { Id = Id });

17 }

18 }

19

20 public async Task Detail(Guid Id, string detailSql)

21 {

22 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

23 {

24 //string querySql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users WHERE Id=@Id";

25 return await conn.QueryFirstOrDefaultAsync(detailSql, new { Id = Id });

26 }

27 }

28

29 ///

30 /// 无参存储过程

31 ///

32 ///

33 ///

34 public async Task> ExecQuerySP(string SPName)

35 {

36 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

37 {

38 return await Task.Run(() => conn.Query(SPName, null, null, true, null, CommandType.StoredProcedure).ToList());

39 }

40 }

41

42 public async Task Insert(T entity, string insertSql)

43 {

44 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

45 {

46 await conn.ExecuteAsync(insertSql, entity);

47 }

48 }

49

50 public async Task> Select(string selectSql)

51 {

52 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

53 {

54 //string selectSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM dbo.Users";

55 return await Task.Run(()=>conn.Query(selectSql).ToList());

56 }

57 }

58

59 public async Task Update(T entity, string updateSql)

60 {

61 using (IDbConnection conn = DataBaseConfig.GetSqlConnection())

62 {

63 await conn.ExecuteAsync(updateSql, entity);

64 }

65 }

66 }

67 }

fz.gif

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

源码地址: https://github.com/wangyulong0505/Dinner

最后

以上就是温婉香烟为你收集整理的android web api 封装,NetCore+Dapper WebApi架构搭建(二):底层封装(示例代码)的全部内容,希望文章能够帮你解决android web api 封装,NetCore+Dapper WebApi架构搭建(二):底层封装(示例代码)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部