NetCore+Dapper WebApi架构搭建(三):添加实体和仓储
上一节讲了类库添加一些底层的基本封装,下面来添加实体和仓储
1、Entities文件夹添加一个实体类Users,继承BaseModel,即拥有BaseModel的主键
1 using System; 2 3 namespace Dinner.Dapper.Entities 4 { 5 public class Users : BaseModel 6 { 7 /// <summary> 8 /// 用户名 9 /// </summary> 10 public string UserName { get; set; } 11 12 /// <summary> 13 /// 密码 14 /// </summary> 15 public string Password { get; set; } 16 17 /// <summary> 18 /// 性别(0女,1男) 19 /// </summary> 20 public int Gender { get; set; } 21 22 /// <summary> 23 /// 出生年月日 24 /// </summary> 25 public DateTime Birthday { get; set; } 26 27 /// <summary> 28 /// 创建日期 29 /// </summary> 30 public DateTime CreateDate { get; set; } 31 32 /// <summary> 33 /// 是否删除(0正常,1删除) 34 /// </summary> 35 public int IsDelete { get; set; } 36 } 37 }
2、IRepository中添加一个IUsersRepository仓储接口,注意仓储接口的写法规范 I+实体名+Repository,对应的仓储业写法贵方 实体名+Repository,这么写不仅是日常规范更是为后面的依赖注入做铺垫
继承自IRepositoryBase<Users> 这个泛型接口,如果你还要定义其他操作,应该自定义接口的新操作方法,然后在实现类中实现自定义的方法
1 using Dinner.Dapper.Entities; 2 using System; 3 using System.Collections.Generic; 4 using System.Threading.Tasks; 5 6 namespace Dinner.Dapper.IRepository 7 { 8 public interface IUserRepository : IRepositoryBase<Users> 9 { 10 #region 扩展的dapper操作 11 12 //加一个带参数的存储过程 13 string ExecExecQueryParamSP(string spName, string name, int Id); 14 15 Task<List<Users>> GetUsers(); 16 17 Task PostUser(Users entity); 18 19 Task PutUser(Users entity); 20 21 Task DeleteUser(Guid Id); 22 23 Task<Users> GetUserDetail(Guid Id); 24 25 #endregion 26 } 27 }
3、Repository中添加一个UserRepository接口实现类继承自RepositoryBase<Users>, IUserRepository
1 using Dapper; 2 using Dinner.Dapper.Entities; 3 using Dinner.Dapper.IRepository; 4 using System; 5 using System.Collections.Generic; 6 using System.Data; 7 using System.Threading.Tasks; 8 9 namespace Dinner.Dapper.Repository 10 { 11 public class UserRepository : RepositoryBase<Users>, IUserRepository 12 { 13 public async Task DeleteUser(Guid Id) 14 { 15 string deleteSql = "DELETE FROM [dbo].[Users] WHERE Id=@Id"; 16 await Delete(Id, deleteSql); 17 } 18 19 public string ExecExecQueryParamSP(string spName, string name, int Id) 20 { 21 using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) 22 { 23 DynamicParameters parameters = new DynamicParameters(); 24 parameters.Add("@UserName", name, DbType.String, ParameterDirection.Output, 100); 25 parameters.Add("@Id", Id, DbType.String, ParameterDirection.Input); 26 conn.Execute(spName, parameters, null, null, CommandType.StoredProcedure); 27 string strUserName = parameters.Get<string>("@UserName"); 28 return strUserName; 29 } 30 } 31 32 public async Task<Users> GetUserDetail(Guid Id) 33 { 34 string detailSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM [dbo].[Users] WHERE Id=@Id"; 35 return await Detail(Id, detailSql); 36 } 37 38 public async Task<List<Users>> GetUsers() 39 { 40 string selectSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM [dbo].[Users]"; 41 return await Select(selectSql); 42 } 43 44 public async Task PostUser(Users entity) 45 { 46 string insertSql = @"INSERT INTO [dbo].[Users](Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete) VALUES(@Id, @UserName, @Password, @Gender, @Birthday, @CreateDate, @IsDelete)"; 47 await Insert(entity, insertSql); 48 } 49 50 public async Task PutUser(Users entity) 51 { 52 string updateSql = "UPDATE [dbo].[Users] SET UserName=@UserName, Password=@Password, Gender=@Gender, Birthday=@Birthday, CreateDate=@CreateDate, IsDelete=@IsDelete WHERE Id=@Id"; 53 await Update(entity, updateSql); 54 } 55 } 56 }
Dapper就是写纯Sql语句的,这很考验Sql功底,不像EF那样,写几个Linq就解决了,不过视图,函数,存储过程都是通用的,Dapper可以,EF也可以
这个Dapper的数据库和表需要你自己创建,不比EF可以自动生成
上面建的实体,仓储接口和仓储实现都是为了演示而已,你们若想添加其他实体,仓储就自定义添加,不过一定要遵循命名规范。
下一节我们转到Web Api层,来实现仓储的依赖注入
源码地址: https://github.com/wangyulong0505/Dinner