ASP.NET Core:使用Dapper和SwaggerUI来丰富你的系统框架
Posted 王杰光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET Core:使用Dapper和SwaggerUI来丰富你的系统框架相关的知识,希望对你有一定的参考价值。
一、概述
1、用VS2017创建如下图的几个.NET Standard类库,默认版本为1.4,你可以通过项目属性进行修改,最高支持到1.6,大概五月份左右会更新至2.0,API会翻倍,很期待!
2、当然了,你还可以通过.NET Core Tool的cli命令来创建,前提是你的电脑上安装了.NET Core SDK。
dotnet new classlib -n Light.Repository //创建一个名字为Light.Repository的.NET Standard类库
更多创建类型请键入如下命令进行查看
dotnet new -all
红框内自上而下依次表示:
- 控制台应用程序
- 类库
- 微软自带的单元测试项目
- 引入XUnit框架的单元测试项目
- 空的ASP.NET Core
- ASP.NET Core MVC项目
- ASP.NET Core WebAPI项目
- 空的解决方案
二、引入Dapper
1、准备创建用户表的SQL脚本
1 USE [Light] 2 GO 3 4 /****** Object: Table [dbo].[User] Script Date: 2017/3/27 22:40:08 ******/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 CREATE TABLE [dbo].[User]( 12 [Id] [int] IDENTITY(10000,1) NOT NULL, 13 [UserName] [nvarchar](50) NOT NULL, 14 [Password] [nvarchar](50) NOT NULL, 15 [Gender] [bit] NOT NULL, 16 [Birthday] [datetime] NOT NULL, 17 [CreateUserId] [int] NOT NULL, 18 [CreateDate] [datetime] NOT NULL, 19 [UpdateUserId] [int] NOT NULL, 20 [UpdateDate] [datetime] NOT NULL, 21 [IsDeleted] [bit] NOT NULL, 22 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 23 ( 24 [Id] ASC 25 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 26 ) ON [PRIMARY] 27 28 GO 29 30 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_UserName] DEFAULT (\'\') FOR [UserName] 31 GO 32 33 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_Password] DEFAULT (\'\') FOR [Password] 34 GO 35 36 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_Gender] DEFAULT ((0)) FOR [Gender] 37 GO 38 39 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_Birthday] DEFAULT (getdate()) FOR [Birthday] 40 GO 41 42 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_CreateUserId] DEFAULT ((0)) FOR [CreateUserId] 43 GO 44 45 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_CreateDate] DEFAULT (getdate()) FOR [CreateDate] 46 GO 47 48 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_UpdateUserId] DEFAULT ((0)) FOR [UpdateUserId] 49 GO 50 51 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_UpdateDate] DEFAULT (getdate()) FOR [UpdateDate] 52 GO 53 54 ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_User_IsDeleted] DEFAULT ((0)) FOR [IsDeleted] 55 GO 56 57 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'主键Id\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'Id\' 58 GO 59 60 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'用户名\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'UserName\' 61 GO 62 63 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'密码\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'Password\' 64 GO 65 66 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'性别(0女,1男)\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'Gender\' 67 GO 68 69 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'出生年月日\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'Birthday\' 70 GO 71 72 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'创建人\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'CreateUserId\' 73 GO 74 75 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'创建时间\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'CreateDate\' 76 GO 77 78 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'更新人\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'UpdateUserId\' 79 GO 80 81 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'更新时间\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'UpdateDate\' 82 GO 83 84 EXEC sys.sp_addextendedproperty @name=N\'MS_Description\', @value=N\'标志是否删除(0未删除,1已删除)\' , @level0type=N\'SCHEMA\',@level0name=N\'dbo\', @level1type=N\'TABLE\',@level1name=N\'User\', @level2type=N\'COLUMN\',@level2name=N\'IsDeleted\' 85 GO
2、添加对应的User表实体
1 /// <summary> 2 /// 用户实体 3 /// </summary> 4 public class User : BaseModel 5 { 6 /// <summary> 7 /// 用户名 8 /// </summary> 9 public string UserName { get; set; } 10 11 /// <summary> 12 /// 密码 13 /// </summary> 14 public string Password { get; set; } 15 16 /// <summary> 17 /// 性别(0女,1男) 18 /// </summary> 19 public bool Gender { get; set; } 20 21 /// <summary> 22 /// 出生年月日 23 /// </summary> 24 public DateTime Birthday { get; set; } 25 }
3、使用泛型针对每一个表定义基本的CRUD接口,然后IUserRepository继承它,如果需要其他的接口,后续往里边定义即可
1 /// <summary> 2 /// 基类业务接口定义 3 /// </summary> 4 public interface IBaseBusiness<T> where T : class 5 { 6 /// <summary> 7 /// 添加一个实体 8 /// </summary> 9 /// <param name="entity">要创建的实体</param> 10 /// <param name="connectionString">链接字符串</param> 11 /// <returns></returns> 12 bool CreateEntity(T entity, string connectionString = null); 13 14 /// <summary> 15 /// 根据主键Id获取一个实体 16 /// </summary> 17 /// <param name="id">主键Id</param> 18 /// <param name="connectionString">链接字符串</param> 19 /// <returns></returns> 20 T RetriveOneEntityById(int id, string connectionString = null); 21 22 /// <summary> 23 /// 获取所有实体 24 /// </summary> 25 /// <param name="connectionString">链接字符串</param> 26 /// <returns></returns> 27 IEnumerable<T> RetriveAllEntity(string connectionString = null); 28 29 /// <summary> 30 /// 修改一个实体 31 /// </summary> 32 /// <param name="entity">要修改的实体</param> 33 /// <param name="connectionString">链接字符串</param> 34 /// <returns></returns> 35 bool UpdateEntity(T entity, string connectionString = null); 36 37 /// <summary> 38 /// 根据主键Id删除一个实体 39 /// </summary> 40 /// <param name="id">主键Id</param> 41 /// <param name="connectionString">链接字符串</param> 42 /// <returns></returns> 43 bool DeleteEntityById(int id, string connectionString = null); 44 }
4、主角进场。通过Nuget引入Dapper到Repository中,目前的版本为1.50.2
Install-Package Dapper