.Net WebApi Swagger AutoFac JWT的实现(一)

开发环境:VS2017、.Net 4.6.2、SQLServer数据库

首先,先将项目的基础框架搭建起来,项目使用的是 EF DBFirst机制,DDD模式

本章实现一下项目框架的基础搭建

1、使用 VS2017创建一个 .net  的webapi项目,项目创建完成后,在项目中添加相关的业务类库

Net.App:数据业务层

Net.IApp:数据业务接口层

Net.Infrastructure:通用方法

Net.Repository:Model实体以及数据库

Net.WebApi:webapi 接口

2、NetCore.Repository的搭建

Nuget添加 EntityFramework、Z.EntityFramework.Plus.EF6两个包的引用

Common:通用实体类

Domain:数据库表实体类

RequestEntity:WebApi请求参数实体类

ResponseEntity:WebApi返回数据实体类

NetCoreDbContext:数据库上下文

BaseRepository:数据库操作通用方法

IBaseRepository:数据库操作通用方法

UnitWork:数据库事务操作方法

IUnitWork:数据库事务操作方法接口

NetDbContext代码

using System;
using System.Data.Entity;
using Net.Repository.Domain;

namespace Net.Repository
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class NetDbContext:DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public NetDbContext():base("name=MyConnection")
        {

        }
        #region dbSet
        public virtual DbSet<UserInfo> UserInfos { get; set; }
        #endregion
    }
}

BaseRepository代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Net.Repository.Domain;
using System.Data.Entity;
using Z.EntityFramework.Plus;
using Net.Infrastructure;

namespace Net.Repository
{
    /// <summary>
    /// 数据操作通用方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T>:IBaseRepository<T> where T : BaseModel
    {
        protected NetDbContext netDbContext;

        public BaseRepository(NetDbContext _netDbContext)
        {
            netDbContext = _netDbContext;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Add(T entity)
        {
            try
            {
                //netDbContext.Set<T>().Add(entity);
                //netDbContext.SaveChanges();
                //netDbContext.Entry(entity).State = EntityState.Detached;
                netDbContext.Entry(entity).State = EntityState.Added;
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public bool BatchAdd(T[] entities)
        {
            try
            {
                netDbContext.Set<T>().AddRange(entities);
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected == entities.Length ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            try
            {
                netDbContext.Set<T>().Attach(entity);
                netDbContext.Entry(entity).State = EntityState.Modified;
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件更新数据
        /// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
        /// </summary>
        /// <param name="where">The where.</param>
        /// <param name="entity">The entity.</param>
        public bool Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity)
        {
            try
            {
                netDbContext.Set<T>().Where(where).Update(entity);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据ID获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetEntiyByID(Guid id)
        {
            try
            {
                Expression<Func<T, bool>> where = x => x.ID.Equals(id);
                var modelList = netDbContext.Set<T>().Where(where).AsQueryable().ToList();
                return modelList.Any() ? modelList.FirstOrDefault() : null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 根据查询条件获取单个数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T GetEntityByWhere(Expression<Func<T,bool>> expression)
        {
            return netDbContext.Set<T>().FirstOrDefault(expression);
        }
        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(Guid id)
        {
            var model = GetEntiyByID(id);
            if (model != null)
            {
                try
                {
                    netDbContext.Set<T>().Attach(model);
                    netDbContext.Entry(model).State = EntityState.Deleted;
                    int rowsAffected = netDbContext.SaveChanges();
                    return rowsAffected > 0 ? true : false;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            try
            {
                netDbContext.Set<T>().Remove(entity);
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="expression"></param>
        public bool Delete(Expression<Func<T, bool>> expression)
        {
            try
            {
                netDbContext.Set<T>().Where(expression).Delete();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool IsExist(Expression<Func<T,bool>> expression)
        {
            return netDbContext.Set<T>().Any(expression);
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="expression">查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns></returns>
        public List<T> GetList(string orderColumn, Expression<Func<T,bool>> expression = null,string orderBy="desc")
        {
            try
            {
                IQueryable<T> quary;
                if (expression == null)
                {
                    quary= netDbContext.Set<T>().AsNoTracking().AsQueryable();
                }
                else
                {
                    quary= netDbContext.Set<T>().AsNoTracking().AsQueryable().Where(expression);
                }
                return orderBy == "desc" ? quary.OrderByDescending(orderColumn).ToList() : quary.OrderBy(orderColumn).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="orderBy">排序方式</param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<T> GetPageList(Expression<Func<T, bool>> expression,int pageSize,int pageIndex,out int totalCount, string orderColumn, string orderBy = "desc")
        {
            try
            {
                if (pageIndex < 1)
                {
                    pageIndex = 1;
                }
                int skipCount = (pageIndex - 1) * pageSize;
                totalCount = netDbContext.Set<T>().Where(expression).Count();
                IQueryable<T> quary = netDbContext.Set<T>().AsNoTracking().AsQueryable().Where(expression);
                return orderBy == "desc" ? quary.OrderByDescending(orderColumn).Skip(skipCount).Take(pageSize).ToList() : quary.OrderBy(orderColumn).Skip(skipCount).Take(pageSize).ToList();
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return null;
            }
        }
    }
}

IBaseRepository代码

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Net.Repository
{
    /// <summary>
    /// 数据基本操作接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IBaseRepository<T> where T:class
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Add(T entity);
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        bool BatchAdd(T[] entities);
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Update(T entity);
        /// <summary>
        /// 根据条件更新
        /// </summary>
        /// <param name="where"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity);
        /// <summary>
        /// 根据ID获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T GetEntiyByID(Guid id);
        /// <summary>
        /// 根据查询条件获取单个数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        T GetEntityByWhere(Expression<Func<T, bool>> expression);
        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        bool Delete(Guid id);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Delete(T entity);
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="expression"></param>
        bool Delete(Expression<Func<T, bool>> expression);
        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        bool IsExist(Expression<Func<T, bool>> expression);
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="expression">查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns></returns>
        List<T> GetList(string orderColumn, Expression<Func<T, bool>> expression = null, string orderBy = "desc");
        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="orderBy">排序方式</param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        List<T> GetPageList(Expression<Func<T, bool>> expression, int pageSize, int pageIndex, out int totalCount, string orderColumn, string orderBy = "desc");
    }
}

 

UnitWrok代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Net.Repository.Domain;
using System.Data.Entity;
using Z.EntityFramework.Plus;
using Net.Infrastructure;

namespace Net.Repository
{
    /// <summary>
    /// 数据库事务操作方法
    /// </summary>
    public class UnitWork:IUnitWork
    {
        protected NetDbContext netDbContext;

        public UnitWork(NetDbContext _netDbContext)
        {
            netDbContext = _netDbContext;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add<T>(T entity) where T:BaseModel
        {
            try
            {
                //netCoreDbContext.Set<T>().Add(entity);
                //netCoreDbContext.SaveChanges();
                //netCoreDbContext.Entry(entity).State = EntityState.Detached;
                netDbContext.Entry(entity).State = EntityState.Added;
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public bool BatchAdd<T>(T[] entities) where T:BaseModel
        {
            try
            {
                netDbContext.Set<T>().AddRange(entities);
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected == entities.Length ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update<T>(T entity) where T:class
        {
            try
            {
                netDbContext.Set<T>().Attach(entity);
                netDbContext.Entry(entity).State = EntityState.Modified;
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件更新数据
        /// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
        /// </summary>
        /// <param name="where">The where.</param>
        /// <param name="entity">The entity.</param>
        public bool Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity)where T:class
        {
            try
            {
                netDbContext.Set<T>().Where(where).Update(entity);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据ID获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetEntiyByID<T>(Guid id) where T:BaseModel
        {
            try
            {
                Expression<Func<T, bool>> where = x => x.ID.Equals(id);
                var modelList = netDbContext.Set<T>().Where(where).AsQueryable().ToList();
                return modelList.Any() ? modelList.FirstOrDefault() : null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 根据查询条件获取单个数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T GetEntityByWhere<T>(Expression<Func<T, bool>> expression) where T:class
        {
            return netDbContext.Set<T>().FirstOrDefault(expression);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete<T>(T entity) where T:class
        {
            try
            {
                netDbContext.Set<T>().Remove(entity);
                int rowsAffected = netDbContext.SaveChanges();
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="expression"></param>
        public bool Delete<T>(Expression<Func<T, bool>> expression) where T:class
        {
            try
            {
                netDbContext.Set<T>().Where(expression).Delete();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool IsExist<T>(Expression<Func<T, bool>> expression) where T:class
        {
            return netDbContext.Set<T>().Any(expression);
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="expression">查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns></returns>
        public List<T> GetList<T>(string orderColumn, Expression<Func<T, bool>> expression = null,string orderBy="desc") where T:class
        {
            try
            {
                IQueryable<T> quary;
                if (expression == null)
                {
                    quary = netDbContext.Set<T>().AsNoTracking().AsQueryable();
                }
                else
                {
                    quary= netDbContext.Set<T>().AsNoTracking().AsQueryable().Where(expression);
                }
                return orderBy == "desc"?quary.OrderByDescending(orderColumn).ToList():quary.OrderBy(orderColumn).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="orderBy">排序方式</param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<T> GetPageList<T>(Expression<Func<T, bool>> expression, int pageSize, int pageIndex, out int totalCount, string orderColumn, string orderBy = "desc") where T:class
        {
            try
            {
                if (pageIndex < 1)
                {
                    pageIndex = 1;
                }
                int skipCount = (pageIndex - 1) * pageSize;
                totalCount = netDbContext.Set<T>().Where(expression).Count();
                IQueryable<T> quary = netDbContext.Set<T>().AsNoTracking().AsQueryable().Where(expression);
                return orderBy == "desc" ? quary.OrderByDescending(orderColumn).Skip(skipCount).Take(pageSize).ToList() : quary.OrderBy(orderColumn).Skip(skipCount).Take(pageSize).ToList();
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return null;
            }
        }
    }
}

IUnitWork代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Net.Repository.Domain;

namespace Net.Repository
{
    /// <summary>
    /// 数据库事务操作方法
    /// </summary>
    public interface IUnitWork
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Add<T>(T entity) where T : BaseModel;
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        bool BatchAdd<T>(T[] entities) where T : BaseModel;
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Update<T>(T entity) where T : class;
        /// <summary>
        /// 根据条件更新数据
        /// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
        /// </summary>
        /// <param name="where">The where.</param>
        /// <param name="entity">The entity.</param>
        bool Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T : class;
        /// <summary>
        /// 根据ID获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T GetEntiyByID<T>(Guid id) where T : BaseModel;
        /// <summary>
        /// 根据查询条件获取单个数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        T GetEntityByWhere<T>(Expression<Func<T, bool>> expression) where T : class;
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Delete<T>(T entity) where T : class;
        /// <summary>
        /// 根据条件删除数据
        /// </summary>
        /// <param name="expression"></param>
        bool Delete<T>(Expression<Func<T, bool>> expression) where T : class;
        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        bool IsExist<T>(Expression<Func<T, bool>> expression) where T : class;
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="expression">查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns></returns>
        List<T> GetList<T>(string orderColumn, Expression<Func<T, bool>> expression = null, string orderBy = "desc") where T : class;
        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="orderColumn">排序字段</param>
        /// <param name="orderBy">排序方式</param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        List<T> GetPageList<T>(Expression<Func<T, bool>> expression, int pageSize, int pageIndex, out int totalCount, string orderColumn, string orderBy = "desc") where T : class;
    }
}

在Domain下添加BaseModel和UserInfo实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;

namespace Net.Repository.Domain
{
    /// <summary>
    /// 用户信息
    /// </summary>
    [Table("UserInfo")]
    public class UserInfo:BaseModel
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginAccount { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string LoginPassword { get; set; }
    }
}
using System;
using System.ComponentModel.DataAnnotations;

namespace Net.Repository.Domain
{
    /// <summary>
    /// 通用字段实体类
    /// </summary>
    public abstract class BaseModel
    {
        /// <summary>
        /// 主键
        /// </summary>
        [Key]
        public string ID { get; set; } = Guid.NewGuid().ToString();
        /// <summary>
        /// 创建人
        /// </summary>
        [Required, StringLength(50)]
        public string CreateUser { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyUser { get; set; }
        /// <summary>
        /// 修改日期
        /// </summary>
        public DateTime? ModifyTime { get; set; }
    }
}

3、Net.App的搭建

项目添加对Net.Repository及Net.Infrastructure以及Net.IApp三个项目的引用

在项目中添加BaseApp、UserInfoApp两个业务操作类

using System;
using System.Collections.Generic;
using System.Text;
using Net.Repository;
using Net.Repository.Domain;

namespace Net.App
{
    public class BaseApp<T> where T:BaseModel
    {
        protected IUnitWork unitWork;
        protected IBaseRepository<T> baseRepository;
        public BaseApp(IUnitWork _unitWork, IBaseRepository<T> _baseRepository)
        {
            unitWork = _unitWork;
            baseRepository = _baseRepository;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
using Net.Repository;
using Net.Repository.Domain;
using Net.Repository.RequestEntity;
using Net.Infrastructure;
using Net.IApp;

namespace Net.App
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class UserInfoApp:BaseApp<UserInfo>,IUserInfoApp
    {
        public UserInfoApp(IUnitWork unitWork,IBaseRepository<UserInfo> baseRepository):base(unitWork,baseRepository)
        {
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool Add(UserInfo userInfo)
        {
            return baseRepository.Add(userInfo);
        }
        /// <summary>
        /// 判断账户是否已存在
        /// </summary>
        /// <param name="loginAccount"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExist(string loginAccount,string id)
        {
            return baseRepository.IsExist(x => x.LoginAccount.Equals(loginAccount) && !x.ID.Equals(id));
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginAccount">登录名</param>
        /// <param name="password">登录密码</param>
        /// <returns></returns>
        public UserInfo Login(string loginAccount,string password)
        {
            return baseRepository.GetEntityByWhere(x=>x.LoginAccount.Equals(loginAccount)&&x.LoginPassword.Equals(password));
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userInfoRequest"></param>
        /// <returns></returns>
        public List<UserInfo> GetList( UserInfoRequest userInfoRequest)
        {
            Expression<Func<UserInfo,bool>> predicte=x=>true;//查询条件
            if (!string.IsNullOrEmpty(userInfoRequest.UserName))
            {
                predicte = predicte.And(x=>x.UserName.Contains(userInfoRequest.UserName));
            }
            return baseRepository.GetList("CreateTime", predicte);
        }
    }
}

IUserInfoApp代码

using Net.Repository.Domain;
using Net.Repository.RequestEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net.IApp
{
    public interface IUserInfoApp
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        bool Add(UserInfo userInfo);
        /// <summary>
        /// 判断账户是否已存在
        /// </summary>
        /// <param name="loginAccount"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        bool IsExist(string loginAccount, string id);

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginAccount">登录名</param>
        /// <param name="password">登录密码</param>
        /// <returns></returns>
        UserInfo Login(string loginAccount, string password);
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userInfoRequest"></param>
        /// <returns></returns>
        List<UserInfo> GetList(UserInfoRequest userInfoRequest);
    }
}

RequestEntity添加用户查询条件的实体

using System;
using System.Collections.Generic;
using System.Text;

namespace Net.Repository.RequestEntity
{
    /// <summary>
    /// 用户查询信息
    /// </summary>
    public class UserInfoRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginAccount { get; set; }
        /// <summary>
        /// 登录密码
        /// </summary>
        public string LoginPassword { get; set; }
    }
}

Net.WebApi  Web.config配置文件中添加数据库连接

<connectionStrings>
    <add name="MyConnection" connectionString="server=.;database=NetCoreWebApi;uid=sa;pwd=123456; multipleactiveresultsets=True;Connection Timeout=500;" providerName="System.Data.SqlClient" />
  </connectionStrings>

完整代码下载地址:https://download.csdn.net/download/liwan09/11720168

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值