首页 > 编程语言 >c#服务基类

c#服务基类

时间:2024-11-06 22:15:23浏览次数:3  
标签:predicate 服务 c# db query 基类 var return null

 

using CQIE.LabAssist.Models;

using Microsoft.EntityFrameworkCore;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Linq.Expressions;

using System.Text;

using System.Threading.Tasks;

namespace brand.cyt369.com.dal.DbContexts

{

/// <summary>

/// 服务基类

/// </summary>

/// <typeparam name="TService"></typeparam>

public abstract class BaseService<TService> where TService : class, new()

{

protected bool AutoCloseContext { get; set; } = true;

protected static readonly TService m_Instance = new TService();

/// <summary>

/// 单实例

/// </summary>

public static TService Instance

{

get { return m_Instance; }

}

/// <summary>

/// DBContext对象

/// </summary>

protected virtual _DbContext Ctx

{

get

{

return new _DbContext();

}

}

/// <summary>

/// 保存对象

/// </summary>

/// <param name="course"></param>

/// <returns></returns>

protected virtual bool Save<T>(T entity, Expression<Func<T, bool>> predicate = null) where T : class

{

if (entity == null)

{

throw new ArgumentNullException("entity");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

if (predicate == null)

{

db.Set<T>().Add(entity);

return db.SaveChanges() > 0;

}

else

{

var item = db.Set<T>().SingleOrDefault(predicate);

if (item == null)

{

db.Set<T>().Add(entity);

return db.SaveChanges() > 0;

}

else

{

var propts = entity.GetType().GetProperties();

foreach (var p in propts)

{

p.SetValue(item, p.GetValue(entity, null), null);

}

db.SaveChanges();

}

return true;

}

}

}

else

{

var db = this.Ctx;

{

if (predicate == null)

{

db.Set<T>().Add(entity);

return db.SaveChanges() > 0;

}

else

{

var item = db.Set<T>().SingleOrDefault(predicate);

if (item == null)

{

db.Set<T>().Add(entity);

return db.SaveChanges() > 0;

}

else

{

var propts = entity.GetType().GetProperties();

foreach (var p in propts)

{

p.SetValue(item, p.GetValue(entity, null), null);

}

db.SaveChanges();

}

return true;

}

}

}

}

/// <summary>

/// 更新对象

/// </summary>

/// <param name="course"></param>

/// <returns></returns>

protected virtual bool Update<T>(T entity, Expression<Func<T, bool>> predicate) where T : class

{

if (entity == null)

{

throw new ArgumentNullException("entity");

}

if (predicate == null)

{

throw new ArgumentNullException("selector");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var item = db.Set<T>().SingleOrDefault(predicate);

if (item != null)

{

var propts = entity.GetType().GetProperties();

foreach (var p in propts)

{

p.SetValue(item, p.GetValue(entity, null), null);

}

db.SaveChanges();

return true;

}

return false;

}

}

else

{

var db = this.Ctx;

{

var item = db.Set<T>().SingleOrDefault(predicate);

if (item != null)

{

var propts = entity.GetType().GetProperties();

foreach (var p in propts)

{

p.SetValue(item, p.GetValue(entity, null), null);

}

db.SaveChanges();

return true;

}

return false;

}

}

}

/// <summary>

/// 删除对象

/// </summary>

/// <param name="courseId"></param>

/// <returns></returns>

protected virtual bool Delete<T>(T entity) where T : class

{

if (entity == null)

{

throw new ArgumentNullException("entity");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

db.Set<T>().Remove(entity);

db.SaveChanges();

return true;

}

}

else

{

var db = this.Ctx;

{

db.Set<T>().Remove(entity);

db.SaveChanges();

return true;

}

}

}

/// <summary>

/// 按条件删除

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="predicate"></param>

/// <returns></returns>

protected virtual bool Delete<T>(Expression<Func<T, bool>> predicate) where T : class

{

if (predicate == null)

{

throw new ArgumentNullException("selector");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var entitys = db.Set<T>().Where(predicate);

if (entitys != null)

{

db.Set<T>().RemoveRange(entitys);

db.SaveChanges();

return true;

}

}

}

else

{

var db = this.Ctx;

{

var entitys = db.Set<T>().Where(predicate);

if (entitys != null)

{

db.Set<T>().RemoveRange(entitys);

db.SaveChanges();

return true;

}

}

}

return false;

}

/// <summary>

/// 将对象进行处理后保存数据库

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="predicate"></param>

/// <param name="action"></param>

/// <returns></returns>

protected virtual bool DoAction<T>(Expression<Func<T, bool>> predicate, Action<IQueryable<T>> action) where T : class

{

if (predicate == null)

{

throw new ArgumentNullException("selector");

}

if (action == null)

{

throw new ArgumentNullException("action");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var entitys = db.Set<T>().Where(predicate);

if (entitys != null)

{

action(entitys);

db.SaveChanges();

return true;

}

}

}

else

{

var db = this.Ctx;

{

var entitys = db.Set<T>().Where(predicate);

if (entitys != null)

{

action(entitys);

db.SaveChanges();

return true;

}

}

}

return false;

}

/// <summary>

/// 将对象进行处理后保存数据库

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="predicate"></param>

/// <param name="action"></param>

/// <returns></returns>

protected virtual bool DoAction<T>(Expression<Func<T, bool>> predicate, Action<T> action) where T : class

{

if (predicate == null)

{

throw new ArgumentNullException("selector");

}

if (action == null)

{

throw new ArgumentNullException("action");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var entitys = db.Set<T>().Where(predicate).FirstOrDefault();

if (entitys != null)

{

action(entitys);

db.SaveChanges();

return true;

}

}

}

else

{

var db = this.Ctx;

{

var entitys = db.Set<T>().Where(predicate).FirstOrDefault();

if (entitys != null)

{

action(entitys);

db.SaveChanges();

return true;

}

}

}

return false;

}

/// <summary>

/// 查询列表

/// </summary>

/// <param name="name"></param>

/// <returns></returns>

protected virtual IQueryable<T> Get<T>(Expression<Func<T, bool>> predicate = null) where T : class

{

var db = this.Ctx;

var query = from o in db.Set<T>()

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query;

}

protected virtual IQueryable<TEntity> Get<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> includeExpression, Expression<Func<TEntity, bool>> predicate = null) where TEntity : class

{

if (includeExpression == null)

{

throw new ArgumentNullException("includeExpression");

}

var db = this.Ctx;

var query = from o in db.Set<TEntity>().Include(includeExpression)

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query;

}

/// <summary>

/// 获取一个对象

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="predicate"></param>

/// <returns></returns>

protected virtual T GetOne<T>(Expression<Func<T, bool>> predicate = null) where T : class

{

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var query = from o in db.Set<T>()

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query.FirstOrDefault();

}

}

else

{

var db = this.Ctx;

{

var query = from o in db.Set<T>()

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query.FirstOrDefault();

}

}

}

protected virtual TEntity GetOne<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> includeExpression, Expression<Func<TEntity, bool>> predicate = null) where TEntity : class

{

if (includeExpression == null)

{

throw new ArgumentNullException("includeExpression");

}

if (this.AutoCloseContext)

{

using (var db = this.Ctx)

{

var query = from o in db.Set<TEntity>().Include(includeExpression)

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query.FirstOrDefault();

}

}

else

{

var db = this.Ctx;

{

var query = from o in db.Set<TEntity>().Include(includeExpression)

select o;

if (predicate != null)

{

query = query.Where(predicate);

}

return query.FirstOrDefault();

}

}

}

/// <summary>

/// 分页查询

/// </summary>

/// <typeparam name="TSource"></typeparam>

/// <typeparam name="TKey"></typeparam>

/// <param name="page"></param>

/// <param name="rows"></param>

/// <param name="total"></param>

/// <param name="items"></param>

/// <param name="keySelector"></param>

/// <returns></returns>

protected virtual IQueryable<TSource> Pagination<TSource, TKey>(int page, int rows, ref int total, IQueryable<TSource> items, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TSource, TKey>> thenSelector = null, bool isDesc = true, bool isThenDesc = true)

{

var orderItems = isDesc ? items.OrderByDescending(keySelector) : items.OrderBy(keySelector);

if (thenSelector != null)

{

orderItems = isThenDesc ? orderItems.ThenByDescending(thenSelector) : orderItems.ThenBy(thenSelector);

}

total = items.Count();

if ((page - 1) > 0)

{

items = orderItems.Skip((page - 1) * rows);

items = items.Take(rows);

}

else

{

items = orderItems.Take(rows);

}

return items;

}

protected virtual IQueryable<TSource> Pagination<TSource, TKey1, TKey2>(int page, int rows, ref int total, IQueryable<TSource> items, Expression<Func<TSource, TKey1>> keySelector, Expression<Func<TSource, TKey2>> thenSelector = null, bool isDesc = true, bool isThenDesc = true)

{

var orderItems = isDesc ? items.OrderByDescending(keySelector) : items.OrderBy(keySelector);

if (thenSelector != null)

{

orderItems = isThenDesc ? orderItems.ThenByDescending(thenSelector) : orderItems.ThenBy(thenSelector);

}

total = items.Count();

if ((page - 1) > 0)

{

items = orderItems.Skip((page - 1) * rows);

items = items.Take(rows);

}

else

{

items = orderItems.Take(rows);

}

return items;

}

}

}

标签:predicate,服务,c#,db,query,基类,var,return,null
From: https://blog.csdn.net/weixin_62232239/article/details/143581341

相关文章

  • cookie&session
    CookieCookie是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器再发起请求时被携带并发送到服务器。作用:会话管理:记录用户的登录状态、购物车内容等。个性化:存储用户的偏好设置,如语言、主题等。追踪:用于分析用户行为,进行广告投放等。......
  • 鸿蒙Next密码自动填充服务:功能与应用场景全解析
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)在开发多语言电商平台方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。在当今数字化......
  • 深入探索鸿蒙Next密码自动填充服务:高级功能与适配场景
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)在开发多语言电商平台方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。在当今数字化......
  • C++ 在模板三个阶段检查错误
    第一个阶段是编译模板本身时。在这个阶段,编译器通常不会发现很多错误。编译器可以检查语法错误,例如忘记分号或者变量名拼错等,但也就这么多了。第二个阶段是编译器遇到模板使用时。在此阶段,编译器仍然没有很多可检查的。对于函数模板调用,编译器通常会检查实参数目是否正确。它还能......
  • 学习笔记(二十八):ArkUi-自定义弹窗 (CustomDialog)
    概述:CustomDialog是自定义弹窗,可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗一、创建自定义弹框1、使用@CustomDialog装饰器装饰自定义弹窗,可在此装饰器内自定义弹窗内容//自定义弹框内容@CustomDialogst......
  • 鸿蒙 Next 密码自动填充服务实战:构建安全登录模块
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)在开发多语言电商平台方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。在当今数字化......
  • 基于ACO蚁群优化的VRPSD问题求解matlab仿真,输出规划路径结果和满载率
    1.程序功能描述基于ACO蚁群优化的VRPSD问题求解matlab仿真,输出ACO优化的收敛曲线,规划路径结果和每一条路径的满载率。2.测试软件版本以及运行结果展示MATLAB2022a版本运行 3.核心程序%搜索fori=1:Iterationiis_best=0;forj=1:Npop......
  • C++智能指针
    C++智能指针以引用计数为基础的智能指针,引用计数的管理逻辑如下:除了初始化对象本身外,每个构造函数(拷贝构造函数除外)还要在堆上创建一个引用计数,用来记录有多少个对象共享状态。当我们创建一个对象时,只有一个对象共享状态,因此将引用计数初始化为1;拷贝构造函数不分配新的计数器......
  • CF 口胡笔记 2200Ct辑
    ¿如何搞笑高效做题?只需要口胡CF题就行啦!(从前天起口胡CF按照洛谷通过人数排序的题单这期我们来口胡CF2200Part1吧~CF617EXORandFavoriteNumber给定一个长度为\(n\)的序列\(a\),然后再给一个数字\(k\),再给出\(m\)组询问,每组询问给出一个区间,求这个区间......
  • sicp每日一题[2.71]
    Exercise2.71SupposewehaveaHuffmantreeforanalphabetofnsymbols,andthattherelativefrequenciesofthesymbolsare1,2,4,...,2n1.Sketchthetreeforn=5;forn=10.Insuchatree(forgeneraln)howmanybitsarerequiredtoencode......