1. 程式人生 > 其它 >.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------基礎架構

.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------基礎架構

從18年畢業到現在幹開發轉眼都三四年了,當然17年沒畢業我就幹了差不多一年了,不過做的不是.net 開發,而是Unity3D遊戲開發。這幾年來一邊開發一邊自學,一直忙忙碌碌,是該靜下心來好好總結一下了。

我從開始.net 開發到現在,由最開始的MVC4,MVC5,到.net core2.1,.net core2.2 ,到現在我打算用.net core3.1對以前學的東西做個總結。

架構還是典型的三層架構,只不過為了低耦合層與層之間依賴介面而已。首先介紹整體結構:

接下來我們就要一點一點的把架構搭建起來了:

在appsettings.json配置資料庫連線物件,這裡用sqlserver2019資料庫,注意:.net core3.1以後選用sqlserver資料庫一定要用sqlserver2012以上的版本,不然能連線,但是EF模型對映的時候取資料會異

常。資料庫我用的是本地資料庫

{
  "ConnectionStrings": {
    "SmartNetConnStr": "Server=LAPTOP-FOJA6HBQ\\SQLEXPRESS;uid=sa;pwd=123;Database=CoreNet;MultipleActiveResultSets=true;"
  },
//rides後面會說
"RedisCaching": { "Enabled": true, "RedisConnectionString": "127.0.0.1:6379" }, "Logging": { "LogLevel": {
"Default": "Warning" } }, "AllowedHosts": "*" }

接下來就是資料庫的上下文:

using Core.Net.Model;
using Core.Net.Model.System;
using Microsoft.EntityFrameworkCore;

namespace Core.Net.Common.Core.Net.Data
{
  public  class CoreNetDBContext : DbContext
    {
        /// <summary>
        /// 設定EF資料庫連結字串
        //
/ </summary> public static string DbConnStr { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } /// <summary> /// DbContext 初始化時呼叫 /// </summary> /// <param name="optionsBuilder"></param> [System.Obsolete] protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;database=NotifyBird;Trusted_Connection=True;"); //配置資料鏈接 optionsBuilder.UseSqlServer(DbConnStr); } }

然後在startup中註冊:

 //1、EF設定字串初始化模式
  CoreNetDBContext.DbConnStr = Configuration.GetConnectionString("SmartNetConnStr");

到這裡資料庫配置就完成了。

接下來我們定義每一層的基類:

IBaseRepository:

using Core.Net.Common.Core.Net.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Core.Net.IRepository
{
   public  interface IBaseRepository<T> where T : class, new()
    {
     
        int Insert(T entity);
        int Insert(List<T> entitys);
        int Update(T entity);
        int Delete(string propertyName, object propertyValue);
        int DeleteList(string propertyName, object propertyValue);
        int Delete(T entity);
        T FindEntity(string propertyName, object propertyValue);
        IQueryable<T> IQueryable();
        List<T> FindList(PageModel page, string propertyName, bool orderbyDesc);
        List<T> FindList(string propertyName, bool orderbyDesc);
        List<T> FindList(string propertyName, object propertyValue);
        IQueryable<T> IQueryable(string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        T GetEntityOne(Expression<Func<T, bool>> where);
        IQueryable<T> GetEntityList(Expression<Func<T, bool>> where);
        IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyNames, object propertyValues, string orderByName, bool orderbyDesc);
        IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyName1, object propertyValue1, string propertyName2, object propertyValue2, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, int>> orderbyLambda, bool orderbyDesc);
        List<T> FindList(string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, object>> orderbyLambda, bool orderbyDesc, Expression<Func<T, object>> thenbyLambda, bool thenbyDesc);
        List<T> GetTEntityList(Expression<Func<T, bool>> where);
        IQueryable<T> GetEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true);
        List<T> GetTEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true);
        IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        int SaveChanges();
    }
}

BaseRespository:

using Core.Net.Common.Core.Net.Core;
using Core.Net.Common.Core.Net.Data;
using Core.Net.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Core.Net.Repository
{
     public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        CoreNetDBContext db = new CoreNetDBContext();

        public int Insert(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges();
        }
        public int Insert(List<T> entitys)
        {
            foreach (var entity in entitys)
            {
                db.Entry<T>(entity).State = EntityState.Added;
            }
            return db.SaveChanges();
        }
        public int Update(T entity)
        {
            db.Set<T>().Attach(entity);
            PropertyInfo[] props = entity.GetType().GetProperties();
            bool NoPrimaryKey = true; // 未找到主鍵
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        db.Entry(entity).Property(prop.Name).CurrentValue = null;
                    //主鍵沒找到就一直判斷
                    if (NoPrimaryKey)
                    {
                        IList<CustomAttributeData> listAttr = prop.GetCustomAttributesData();
                        if (listAttr.Count > 0 && listAttr[0].AttributeType.Name == "KeyAttribute")
                        {
                            db.Entry(entity).Property(prop.Name).IsModified = false;
                            NoPrimaryKey = false;
                        }
                    }
                    else
                    {
                        db.Entry(entity).Property(prop.Name).IsModified = true;
                    }

                }
            }

            return db.SaveChanges();
        }
       
        public void Update(T entity, params string[] propertyNames)
        {
            EntityEntry entry = db.Entry<T>(entity);
            entry.State = EntityState.Unchanged;
            foreach (var item in propertyNames)
            {
                entry.Property(item).IsModified = true;
            }
            SaveChanges();
        }
        /// <summary>
        /// 刪除操作:指定主鍵和值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        public int Delete(string propertyName, object propertyValue)
        {
            var TEntity = db.Set<T>().FirstOrDefault(Expression_Equal(propertyName, propertyValue));
            if (TEntity == null)
            {
                return 0;
            }
            else
            {
                return Delete(TEntity);
            }
        }
        /// <summary>
        /// 刪除操作:刪除所有符合條件的記錄
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        public int DeleteList(string propertyName, object propertyValue)
        {
            db.Set<T>().RemoveRange(db.Set<T>().Where(Expression_Equal(propertyName, propertyValue)));
            return db.SaveChanges();
        }
        public int Delete(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Deleted;
            return db.SaveChanges();
        }
        #region 單個物件查詢
        /// <summary>
        /// 按列名及列值查詢單個物件
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        public T FindEntity(string propertyName, object propertyValue)
        {
            return db.Set<T>().FirstOrDefault(Expression_Equal(propertyName, propertyValue));
        }

        #endregion
        /// <summary>
        /// 查詢全部記錄並返回IQueryable
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> IQueryable()
        {
            return db.Set<T>();
        }
        /// <summary>
        /// 無篩選有排序及分頁獲取資料列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="propertyName">單個排序欄位</param>
        /// <param name="orderbyDesc">排序方向</param>
        /// <returns></returns>
        public List<T> FindList(PageModel page, string propertyName, bool orderbyDesc)
        {
            Expression<Func<T, int>> orderbyLambda = GetExpression(propertyName);
            //計算總數量
            page.RecordCount = db.Set<T>().Count();
            if (orderbyDesc)
            {
                var query = db.Set<T>()
                             .OrderByDescending<T, int>(orderbyLambda)
                             .Skip(page.PageSize * (page.PageIndex - 1))
                             .Take(page.PageSize);
                return query.ToList<T>();
            }
            else
            {
                var query = db.Set<T>()
                    .OrderBy<T, int>(orderbyLambda)
                    .Skip(page.PageSize * (page.PageIndex - 1))
                    .Take(page.PageSize);
                return query.ToList<T>();
            }
        }
        /// <summary>
        /// 無篩選所有列表,僅排序
        /// </summary>
        /// <param name="propertyName">單個排序欄位</param>
        /// <param name="orderbyDesc">排序方向</param>
        /// <returns></returns>
        public List<T> FindList(string propertyName, bool orderbyDesc)
        {
            Expression<Func<T, int>> orderbyLambda = GetExpression(propertyName);
            if (orderbyDesc)
            {
                var query = db.Set<T>().OrderByDescending<T, int>(orderbyLambda);
                return query.ToList<T>();
            }
            else
            {
                var query = db.Set<T>().OrderBy<T, int>(orderbyLambda);
                return query.ToList<T>();
            }
        }
        /// <summary>
        /// 按單條件查詢列表 lambda 單個等於查詢條件,返回List
        /// <param name="propertyName">條件列名</param>
        /// <param name="propertyValue">條件檢索值</param>
        /// <returns></returns>
        public List<T> FindList(string propertyName, object propertyValue)
        {
            return db.Set<T>().Where(Expression_Equal(propertyName, propertyValue)).ToList<T>();
        }

        /// <summary>
        /// 單條件查詢所有記錄並排序
        /// </summary>
        /// <param name="propertyName">條件列名</param>
        /// <param name="propertyValue">條件檢索值</param>
        /// <param name="orderByName">排序列名</param>
        /// <param name="propertyValue">排序對應正序逆序</param>
        /// <returns>返回列表</returns>
        public IQueryable<T> IQueryable(string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            var query = db.Set<T>().Where(Expression_Equal(propertyName, propertyValue));
            if (orderbyDesc)
            {
                return query.OrderByDescending<T, int>(GetExpression(orderByName));
            }
            else
            {
                return query.OrderBy<T, int>(GetExpression(orderByName));
            }
        }
        /// <summary>
        /// 兩個條件查詢所有記錄並排序
        /// </summary>
        public IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyNames, object propertyValues, string orderByName, bool orderbyDesc)
        {
            var query = db.Set<T>().Where(Expression_Equal(propertyName, propertyValue));
            query = query.Where(Expression_Equal(propertyNames, propertyValues));
            if (orderbyDesc)
            {
                return query.OrderByDescending<T, int>(GetExpression(orderByName));
            }
            else
            {
                return query.OrderBy<T, int>(GetExpression(orderByName));
            }
        }
        /// <summary>
        /// 三個條件查詢所有記錄並排序
        /// </summary>
        public IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyName1, object propertyValue1, string propertyName2, object propertyValue2, string orderByName, bool orderbyDesc)
        {
            var query = db.Set<T>().Where(Expression_Equal(propertyName, propertyValue));
            query = query.Where(Expression_Equal(propertyName1, propertyValue1));
            query = query.Where(Expression_Equal(propertyName2, propertyValue2));
            if (orderbyDesc)
            {
                return query.OrderByDescending<T, int>(GetExpression(orderByName));
            }
            else
            {
                return query.OrderBy<T, int>(GetExpression(orderByName));
            }
        }
        /// <summary>
        /// 單條件查詢所有記錄並排序分頁,propertyValue 為空時自動忽略此查詢條件
        /// </summary>
        /// <param name="propertyName">列名/屬性</param>
        /// <param name="value">對應值</param>
        /// <returns></returns>
        public List<T> FindList(PageModel page, string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            if (string.IsNullOrEmpty(propertyValue.ToString()))
            {
                return FindList(page, orderByName, orderbyDesc);
            }
            else
            {
                return FindList(page, Expression_Equal(propertyName, propertyValue), GetExpression(orderByName), orderbyDesc);
            }
        }
        /// <summary>
        /// 通用單欄位降序排列分頁查詢
        /// </summary>
        /// <param name="page">分頁物件</param>
        /// <param name="whereLambda">篩選表示式</param>
        /// <param name="orderbyLambda">排序表示式,預設降序排列</param>
        /// <param name="orderbyDesc">true 按降序排列 false 降序</param>
        public List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, int>> orderbyLambda, bool orderbyDesc)
        {
            //計算總數量
            page.RecordCount = db.Set<T>().Where(whereLambda).Count();
            if (orderbyDesc)
            {
                var query = db.Set<T>().Where(whereLambda)
                             .OrderByDescending<T, int>(orderbyLambda)
                             .Skip(page.PageSize * (page.PageIndex - 1))
                             .Take(page.PageSize);
                return query.ToList<T>();
            }
            else
            {
                var query = db.Set<T>().Where(whereLambda)
                    .OrderBy<T, int>(orderbyLambda)
                    .Skip(page.PageSize * (page.PageIndex - 1))
                    .Take(page.PageSize);
                return query.ToList<T>();
            }
        }
        /// <summary>
        /// 單條件查詢所有記錄並排序不分頁
        /// </summary>
        /// <param name="propertyName">列名/屬性</param>
        /// <param name="value">對應值</param>
        /// <returns></returns>
        public List<T> FindList(string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            Expression<Func<T, bool>> whereLambda = Expression_Equal(propertyName, propertyValue);
            Expression<Func<T, int>> orderbyLambda = GetExpression(orderByName);
            if (orderbyDesc)
            {
                var query = db.Set<T>().Where(whereLambda)
                             .OrderByDescending<T, int>(orderbyLambda);
                return query.ToList<T>();
            }
            else
            {
                var query = db.Set<T>().Where(whereLambda)
                    .OrderBy<T, int>(orderbyLambda);
                return query.ToList<T>();
            }
        }
        /// <summary>
        /// 通用兩欄位降序排列分頁查詢
        /// </summary>
        /// <param name="page">分頁物件</param>
        /// <param name="whereLambda">條件篩選表示式,不能為null</param>
        /// <param name="orderbyLambda">排序表示式,預設降序排列,不能為null</param>
        /// <param name="orderbyDesc">true 按降序排列 false 降序</param>
        /// <param name="thenbyLambda">排序表示式,預設降序排列</param>
        /// <param name="thenbyDesc">true 按降序排列 false 降序</param>
        public List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, object>> orderbyLambda, bool orderbyDesc, Expression<Func<T, object>> thenbyLambda, bool thenbyDesc)
        {
            //計算總數量
            page.RecordCount = db.Set<T>().Where(whereLambda).Count();
            if (orderbyDesc)
            {
                if (thenbyDesc)
                {
                    var query = db.Set<T>().Where(whereLambda)
                                 .OrderByDescending<T, object>(orderbyLambda)
                                 .ThenByDescending<T, object>(thenbyLambda)
                                 .Skip(page.PageSize * (page.PageIndex - 1))
                                 .Take(page.PageSize);
                    return query.ToList<T>();
                }
                else
                {
                    var query = db.Set<T>().Where(whereLambda)
                               .OrderByDescending<T, object>(orderbyLambda)
                               .ThenBy<T, object>(thenbyLambda)
                               .Skip(page.PageSize * (page.PageIndex - 1))
                               .Take(page.PageSize);
                    return query.ToList<T>();

                }
            }
            else
            {
                if (thenbyDesc)
                {
                    var query = db.Set<T>().Where(whereLambda)
                                 .OrderBy<T, object>(orderbyLambda)
                                 .ThenByDescending<T, object>(thenbyLambda)
                                 .Skip(page.PageSize * (page.PageIndex - 1))
                                 .Take(page.PageSize);
                    return query.ToList<T>();
                }
                else
                {
                    var query = db.Set<T>().Where(whereLambda)
                               .OrderBy<T, object>(orderbyLambda)
                               .ThenBy<T, object>(thenbyLambda)
                               .Skip(page.PageSize * (page.PageIndex - 1))
                               .Take(page.PageSize);
                    return query.ToList<T>();

                }
            }
        }

        /// <summary>
        /// 傳入值為表示式返回一個實體
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public T GetEntityOne(Expression<Func<T, bool>> where)
        {
            return db.Set<T>().Where(where)?.FirstOrDefault();
        }
        /// <summary>
        /// 傳入值為表示式,返回一個IQueryable
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> GetEntityList(Expression<Func<T, bool>> where)
        {
            return db.Set<T>().Where(where);
        }
        /// <summary>
        /// 傳入值為表示式,返回一個List陣列
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<T> GetTEntityList(Expression<Func<T, bool>> where)
        {
            return db.Set<T>().Where(where).ToList();
        }
        public IQueryable<T> GetEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>().Where(where);
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            return qlist;
        }
        public List<T> GetTEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>().Where(where);
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            return qlist.ToList();
        }
        public IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>();
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            rowscount = qlist.ToList().Count;
            int skipcount = (pageIndex - 1) * pageSize;
            return qlist.Skip(skipcount).Take(pageSize);
        }
        public List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>();
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            rowscount = qlist.ToList().Count;
            int skipcount = (pageIndex - 1) * pageSize;
            return qlist.Skip(skipcount).Take(pageSize).ToList();
        }
        public IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>().Where(where);
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            rowscount = qlist.ToList().Count;
            int skipcount = (pageIndex - 1) * pageSize;
            return qlist.Skip(skipcount).Take(pageSize);
        }
         public List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            IQueryable<T> qlist = db.Set<T>().Where(where);
            if (asc)
            {
                qlist = qlist.OrderBy(orderbykey).AsQueryable<T>();
            }
            else
            {
                qlist = qlist.OrderByDescending(orderbykey).AsQueryable<T>();
            }
            rowscount = qlist.ToList().Count;
            int skipcount = (pageIndex - 1) * pageSize;
            return qlist.Skip(skipcount).Take(pageSize).ToList();
        }
        public int SaveChanges()
        {
           return db.SaveChanges();
        }
        /// <summary>
        /// 建立等於運算lambda表示式
        /// </summary>
        /// <typeparam name="T">物件型別</typeparam>
        /// <param name="propertyName">屬性/列名</param>
        /// <param name="value">篩選值</param>
        public static Expression<Func<T, bool>> Expression_Equal(string propertyName, object propertyValue)
        {
            ParameterExpression paramTable = Expression.Parameter(typeof(T), "t");    //1、表物件
            MemberExpression memName = Expression.Property(paramTable, propertyName); //2、列物件
            ConstantExpression conName = Expression.Constant(propertyValue);                  //3、值物件
            var queryCase = Expression.Equal(memName, conName);                       //4、動作物件

            return Expression.Lambda<Func<T, bool>>(queryCase, paramTable);
        }
        /// <summary>
        /// 封裝一個基本的 p ==> p.xx 物件,主要用來構造OrderBy
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static Expression<Func<T, int>> GetExpression(string propertyName)
        {
            var param = Expression.Parameter(typeof(T), "x");
            Expression conversion = Expression.Convert(Expression.Property(param, propertyName), typeof(int));   //important to use the Expression.Convert   
            return Expression.Lambda<Func<T, int>>(conversion, param);
        }

    }
}

IBaseServervices:

using Core.Net.Common.Core.Net.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Core.Net.IServices
{
   public   interface IBaseServices<T> where T : class, new()
    {
       
        int Insert(T entity);
        int Insert(List<T> entitys);
        int Update(T entity);

        int Delete(string propertyName, object propertyValue);
        int DeleteList(string propertyName, object propertyValue);
        int Delete(T entity);
        T FindEntity(string propertyName, object propertyValue);
        IQueryable<T> IQueryable();
        List<T> FindList(PageModel page, string propertyName, bool orderbyDesc);
        List<T> FindList(string propertyName, bool orderbyDesc);
        List<T> FindList(string propertyName, object propertyValue);
        IQueryable<T> IQueryable(string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyNames, object propertyValues, string orderByName, bool orderbyDesc);
        IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyName1, object propertyValue1, string propertyName2, object propertyValue2, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, int>> orderbyLambda, bool orderbyDesc);
        List<T> FindList(string propertyName, object propertyValue, string orderByName, bool orderbyDesc);
        List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, object>> orderbyLambda, bool orderbyDesc, Expression<Func<T, object>> thenbyLambda, bool thenbyDesc);
     
        T GetEntityOne(Expression<Func<T, bool>> where);
     
        IQueryable<T> GetEntityList(Expression<Func<T, bool>> where);
        List<T> GetTEntityList(Expression<Func<T, bool>> where);
        IQueryable<T> GetEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true);
        List<T> GetTEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true);
        IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true);
        int SaveChanges();
    }
}

BaseServervices:

using Core.Net.Common.Core.Net.Core;
using Core.Net.IRepository;
using Core.Net.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Core.Net.Services
{
     public class BaseServices<T> : IBaseServices<T> where T : class, new()
    {
       public IBaseRepository<T> dal; 
       public int Insert(T entity)
        {
            return dal.Insert(entity);
        }
        public int Insert(List<T> entitys)
        {
            return dal.Insert(entitys);
        }
        public int Update(T entity)
        {
          return  dal.Update(entity);
        }
        public int Delete(string propertyName, object propertyValue)
        {
            return dal.Delete(propertyName, propertyValue);
        }

        public int DeleteList(string propertyName, object propertyValue)
        {
            return dal.Delete(propertyName, propertyValue);
        }

        public int Delete(T entity)
        {
            return dal.Delete(entity);
        }
        public T FindEntity(string propertyName, object propertyValue)
        {
            return dal.FindEntity(propertyName, propertyValue);
        }
        public IQueryable<T> IQueryable()
        {
            return IQueryable();
        }
        public  List<T> FindList(PageModel page, string propertyName, bool orderbyDesc)
        {
            return dal.FindList(page, propertyName, true);
        }
       public  List<T> FindList(string propertyName, bool orderbyDesc)
        {
            return dal.FindList(propertyName, orderbyDesc);
        }
        public List<T> FindList(string propertyName, object propertyValue)
        {
            return dal.FindList(propertyName, propertyValue);
        }
       public  IQueryable<T> IQueryable(string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            return dal.IQueryable(propertyName, propertyValue, orderByName, orderbyDesc);
        }
        public IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyNames, object propertyValues, string orderByName, bool orderbyDesc)
        {
            return dal.IQueryableDouble(propertyName, propertyValue, propertyNames, propertyValues, orderByName, orderbyDesc);
        }
        public IQueryable<T> IQueryableDouble(string propertyName, object propertyValue, string propertyName1, object propertyValue1, string propertyName2, object propertyValue2, string orderByName, bool orderbyDesc)
        {
            return dal.IQueryableDouble(propertyName, propertyValue, propertyName1, propertyValue1, propertyName2, propertyValue2, orderByName, orderbyDesc);
        }
        public List<T> FindList(PageModel page, string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            return dal.FindList(page, propertyName, propertyValue, orderByName, orderbyDesc);
        }
        public List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, int>> orderbyLambda, bool orderbyDesc)
        {
            return dal.FindList(page, whereLambda, orderbyLambda, orderbyDesc);
        }
        public List<T> FindList(string propertyName, object propertyValue, string orderByName, bool orderbyDesc)
        {
            return dal.FindList(propertyName, propertyValue, orderByName, orderbyDesc);
        }
        public List<T> FindList(PageModel page, Expression<Func<T, bool>> whereLambda, Expression<Func<T, object>> orderbyLambda, bool orderbyDesc, Expression<Func<T, object>> thenbyLambda, bool thenbyDesc)
        {
            return dal.FindList(page, whereLambda, orderbyLambda, orderbyDesc, thenbyLambda, thenbyDesc);
        }


        public IQueryable<T> GetEntityList(Expression<Func<T, bool>> where)
        {
            return dal.GetEntityList(where);
        }
        public List<T> GetTEntityList(Expression<Func<T, bool>> where)
        {
            return dal.GetTEntityList(where);
        }
        public IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            return dal.GetEntityPaginationListOrder(pageSize, pageIndex, orderbykey, out rowscount, true);
        }
        public List<T> GetTEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true)
        {
            return dal.GetTEntityListOrder<orderkey>(where, orderbykey, asc);
        }
        public IQueryable<T> GetEntityListOrder<orderkey>(Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, bool asc = true)
        {
            return dal.GetEntityListOrder<orderkey>(where, orderbykey, asc);
        }
        public List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            return dal.GetTEntityPaginationListOrder<orderkey>(pageSize, pageIndex, orderbykey, out rowscount, asc);
        }
        public T GetEntityOne(Expression<Func<T, bool>> where)
        {
            return dal.GetEntityOne(where);
        }

        public IQueryable<T> GetEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            return dal.GetEntityPaginationListOrder<orderkey>(pageSize, pageIndex, where, orderbykey, out rowscount, asc);
        }
        public List<T> GetTEntityPaginationListOrder<orderkey>(int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, orderkey>> orderbykey, out int rowscount, bool asc = true)
        {
            return GetTEntityPaginationListOrder<orderkey>(pageSize, pageIndex, where, orderbykey, out rowscount, asc);
        }
        public int SaveChanges()
        {
            return dal.SaveChanges();
        }

      
    }
}

Model:這裡先放一個使用者實體:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Core.Net.Model.System
{
    [Table("CoreNetSystemUser")]
    public class CoreNetSystemUser
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Passward { get; set; }    
        public string RealName { get; set; }
        public int RoleId { get; set; }
        public int DepartId { get; set; }
        public string PushId { get; set; }
        public int Sex { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int IsUse { get; set; }
        public DateTime? AddTime { get; set; }
        public DateTime? UpdateTime { get; set; }
        public string UpdateIP { get; set; }
    
    }
}

具體使用,以使用者類為例:

ICoreNetSystemUserRespository:

using Core.Net.Model.System;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Net.IRepository
{
    public interface ICoreNetSystemUserRespository : IBaseRepository<CoreNetSystemUser>
    {
    }
}

CoreNetSystemUserRespository:

using Core.Net.IRepository;
using Core.Net.Model.System;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Net.Repository
{
   public class CoreNetSystemUserRespository : BaseRepository<CoreNetSystemUser>, ICoreNetSystemUserRespository
    {
    }
}

ICoreNetSystemUserServices:

using Core.Net.Model.System;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Net.IServices
{
  public  interface ICoreNetSystemUserServices : IBaseServices<CoreNetSystemUser>
    {
    }
}

CoreNetSystemUserServices:

using Core.Net.IRepository;
using Core.Net.IServices;
using Core.Net.Model.System;
namespace Core.Net.Services
{
   public class CoreNetSystemUserServices : BaseServices<CoreNetSystemUser>, ICoreNetSystemUserServices
    {
        public CoreNetSystemUserServices(ICoreNetSystemUserRespository dal)
        {
            base.dal = dal;
        }
    }
}

到這裡一個簡單的架構已經成型了,至於怎末使用後續在加入Layui前端框架的時候會詳細說明。

.Net Core