.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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章