• NetCore+Dapper WebApi架构搭建(三):添加实体和仓储


    上一节讲了类库添加一些底层的基本封装,下面来添加实体和仓储

    1、Entities文件夹添加一个实体类Users,继承BaseModel,即拥有BaseModel的主键

    using System;
    
    namespace Dinner.Dapper.Entities
    {
        public class Users : BaseModel
        {
            /// <summary>
            /// 用户名
            /// </summary>
            public string UserName { get; set; }
    
            /// <summary>
            /// 密码
            /// </summary>
            public string Password { get; set; }
    
            /// <summary>
            /// 性别(0女,1男)
            /// </summary>
            public int Gender { get; set; }
    
            /// <summary>
            /// 出生年月日
            /// </summary>
            public DateTime Birthday { get; set; }
    
            /// <summary>
            /// 创建日期
            /// </summary>
            public DateTime CreateDate { get; set; }
    
            /// <summary>
            /// 是否删除(0正常,1删除)
            /// </summary>
            public int IsDelete { get; set; }
        }
    }

    2、IRepository中添加一个IUsersRepository仓储接口,注意仓储接口的写法规范 I+实体名+Repository,对应的仓储业写法贵方 实体名+Repository,这么写不仅是日常规范更是为后面的依赖注入做铺垫

    继承自IRepositoryBase<Users> 这个泛型接口,如果你还要定义其他操作,应该自定义接口的新操作方法,然后在实现类中实现自定义的方法

    using Dinner.Dapper.Entities;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace Dinner.Dapper.IRepository
    {
        public interface IUserRepository : IRepositoryBase<Users>
        {
            #region 扩展的dapper操作
    
            //加一个带参数的存储过程
            string ExecExecQueryParamSP(string spName, string name, int Id);
    
            Task<List<Users>> GetUsers();
    
            Task PostUser(Users entity);
    
            Task PutUser(Users entity);
    
            Task DeleteUser(Guid Id);
    
            Task<Users> GetUserDetail(Guid Id);
    
            #endregion
        }
    }

    3、Repository中添加一个UserRepository接口实现类继承自RepositoryBase<Users>, IUserRepository

    using Dapper;
    using Dinner.Dapper.Entities;
    using Dinner.Dapper.IRepository;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Threading.Tasks;
    
    namespace Dinner.Dapper.Repository
    {
        public class UserRepository : RepositoryBase<Users>, IUserRepository
        {
            public async Task DeleteUser(Guid Id)
            {
                string deleteSql = "DELETE FROM [dbo].[Users] WHERE Id=@Id";
                await Delete(Id, deleteSql);
            }
    
            public string ExecExecQueryParamSP(string spName, string name, int Id)
            {
                using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
                {
                    DynamicParameters parameters = new DynamicParameters();
                    parameters.Add("@UserName", name, DbType.String, ParameterDirection.Output, 100);
                    parameters.Add("@Id", Id, DbType.String, ParameterDirection.Input);
                    conn.Execute(spName, parameters, null, null, CommandType.StoredProcedure);
                    string strUserName = parameters.Get<string>("@UserName");
                    return strUserName;
                }
            }
    
            public async Task<Users> GetUserDetail(Guid Id)
            {
                string detailSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM [dbo].[Users] WHERE Id=@Id";
                return await Detail(Id, detailSql);
            }
    
            public async Task<List<Users>> GetUsers()
            {
                string selectSql = @"SELECT Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete FROM [dbo].[Users]";
                return await Select(selectSql);
            }
    
            public async Task PostUser(Users entity)
            {
                string insertSql = @"INSERT INTO [dbo].[Users](Id, UserName, Password, Gender, Birthday, CreateDate, IsDelete) VALUES(@Id, @UserName, @Password, @Gender, @Birthday, @CreateDate, @IsDelete)";
                await Insert(entity, insertSql);
            }
    
            public async Task PutUser(Users entity)
            {
                string updateSql = "UPDATE [dbo].[Users] SET UserName=@UserName, Password=@Password, Gender=@Gender, Birthday=@Birthday, CreateDate=@CreateDate, IsDelete=@IsDelete WHERE Id=@Id";
                await Update(entity, updateSql);
            }
        }
    }

    Dapper就是写纯Sql语句的,这很考验Sql功底,不像EF那样,写几个Linq就解决了,不过视图,函数,存储过程都是通用的,Dapper可以,EF也可以

    这个Dapper的数据库和表需要你自己创建,不比EF可以自动生成

    上面建的实体,仓储接口和仓储实现都是为了演示而已,你们若想添加其他实体,仓储就自定义添加,不过一定要遵循命名规范。

    下一节我们转到Web Api层,来实现仓储的依赖注入

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

    来源:https://www.cnblogs.com/wangyulong/p/8961162.html

  • 相关阅读:
    UICollectionView基础教学
    iOS之多线程
    即时通信之XMPP环境的搭建
    Xcode8+和iOS10+使用Masonry自动计算行高
    视频直播框架集成
    iOS 开发的几种手势
    Swift和Objective-C混编注意事项
    如何在TableView上添加悬浮按钮
    iOS中UITextView键盘回收
    iOS应用内跳转系统设置相关界面的方法
  • 原文地址:https://www.cnblogs.com/frank0812/p/12864525.html
Copyright © 2020-2023  润新知