.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() == " ") 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前端框架的时候会详细说明。
标签:--------,Core,return,string,int,propertyName,基础架构,Expression,public 来源: https://www.cnblogs.com/zpy1993-09/p/15500321.html