码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-04-27 13:48:24      阅读:814      评论:0      收藏:0      [点我收藏+]

标签:users   F12   stack   一个   host   lex   first   using   app   

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

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

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

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

1 using System;
2 
3 namespace Dinner.Dapper
4 {
5     public class BaseModel : IEntity<Guid>
6     {
7         public 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 {
 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 }

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

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Threading.Tasks;
 4 
 5 namespace Dinner.Dapper
 6 {
 7     public interface IRepositoryBase<T>
 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<List<T>> Select(string selectSql);
16 
17         Task<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>
25         Task<List<T>> ExecQuerySP(string SPName);
26     }
27 }
View Code

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 {
10     public class RepositoryBase<T> : IRepositoryBase<T>
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<T> 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<T>(detailSql, new { Id = Id });
26             }
27         }
28 
29         /// <summary>
30         /// 无参存储过程
31         /// </summary>
32         /// <param name="SPName"></param>
33         /// <returns></returns>
34         public async Task<List<T>> ExecQuerySP(string SPName)
35         {
36             using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
37             {
38                 return await Task.Run(() => conn.Query<T>(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<List<T>> 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<T>(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 }
View Code

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

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

标签:users   F12   stack   一个   host   lex   first   using   app   

原文地址:https://www.cnblogs.com/wangyulong/p/8961061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!