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


     從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;
        }
    }
}

 對了,這里要加上一個分頁類,不然上面Base基類會用到,不加報錯的。

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

namespace Core.Net.Common.Core.Net.Core
{
    /// <summary>
    /// 通用分頁對象
    /// </summary>
    public class PageModel
    {
        /// <summary>
        /// 每頁記錄數
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 當前頁
        /// </summary>

        private int _pageIndex = 1;
        /// <summary>
        /// 當前頁索引,即第幾頁,從1開始
        /// </summary>
        public int PageIndex
        {
            get
            {
                if (_pageIndex <= 0)
                    _pageIndex = 1;
                return _pageIndex;
            }
            set { _pageIndex = value; }
        }

        /// <summary>
        /// 總記錄數
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 總頁數
        /// </summary>
        public int TotalPage
        {
            get
            {
                if (RecordCount > 0)
                {
                    return RecordCount % this.PageSize == 0 ? RecordCount / this.PageSize : RecordCount / this.PageSize + 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }

    /// <summary>
    /// 通用分頁對象單例,初始化參數配置計划存放在config中
    /// </summary>
    public class PageModelInstance
    {
        /// <summary>
        /// 分頁按實例生成默認值
        /// </summary>
        private static PageModel pageInstance;
        /// <summary>
        /// 獲取一個初始化的分頁對象實例
        /// </summary>
        /// <returns></returns>
        public static PageModel GetInstance()
        {
            if (pageInstance == null)
            {
                pageInstance = new PageModel();
                pageInstance.PageSize = 20;
                pageInstance.PageIndex = 1;
                pageInstance.RecordCount = 0;
            }

            return pageInstance;
        }
        /// <summary>
        /// 獲取一個初始化的分頁對象實例
        /// </summary>
        /// <returns></returns>
        public static PageModel GetInstance(int pageSize, int pageIndex)
        {
            if (pageInstance == null)
            {
                pageInstance = new PageModel();
                pageInstance.RecordCount = 0;
            }
            pageInstance.PageSize = pageSize;
            pageInstance.PageIndex = pageIndex;
            return pageInstance;
        }
        public static int GetPageTotal(int PageSize, int RecordCount)
        {
            if (RecordCount > 0)
            {
                return RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
            }
            else
            {
                return 0;
            }
        }
    }

}

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM