2016-04-29 16:26:09 +08:00
|
|
|
|
// ***********************************************************************
|
|
|
|
|
// Assembly : OpenAuth.Domain
|
|
|
|
|
// Author : yubaolee
|
|
|
|
|
// Created : 04-29-2016
|
|
|
|
|
//
|
|
|
|
|
// Last Modified By : yubaolee
|
2020-12-17 23:04:04 +08:00
|
|
|
|
// Last Modified On : 12-15-2020
|
2016-04-29 16:26:09 +08:00
|
|
|
|
// Contact : Microsoft
|
2020-12-29 23:52:06 +08:00
|
|
|
|
// File: IUnitWork<OpenAuthDBContext>.cs
|
2016-04-29 16:26:09 +08:00
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-04-28 10:20:24 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Common;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2020-12-17 23:04:04 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-12-29 23:52:06 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using OpenAuth.Repository.Core;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
2017-11-29 20:49:14 +08:00
|
|
|
|
namespace OpenAuth.Repository.Interface
|
2016-04-29 16:26:09 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 工作单元接口
|
2021-07-22 19:33:55 +08:00
|
|
|
|
/// 使用详见:http://doc.openauth.net.cn/core/unitwork.html
|
2016-04-29 16:26:09 +08:00
|
|
|
|
/// <para> 适合在一下情况使用:</para>
|
|
|
|
|
/// <para>1 在同一事务中进行多表操作</para>
|
|
|
|
|
/// <para>2 需要多表联合查询</para>
|
|
|
|
|
/// <para>因为架构采用的是EF访问数据库,暂时可以不用考虑采用传统Unit Work的注册机制</para>
|
|
|
|
|
/// </summary>
|
2020-12-29 23:52:06 +08:00
|
|
|
|
public interface IUnitWork<TDbContext> where TDbContext:DbContext
|
2016-04-29 16:26:09 +08:00
|
|
|
|
{
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// EF默认情况下,每调用一次SaveChanges()都会执行一个单独的事务
|
|
|
|
|
/// 本接口实现在一个事务中可以多次执行SaveChanges()方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
void ExecuteWithTransaction(Action action);
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 返回DbContext,用于多线程等极端情况
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-12-29 23:52:06 +08:00
|
|
|
|
DbContext GetDbContext();
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 返回一个单独的实体,如果记录多于1个则取第一个
|
|
|
|
|
/// </summary>
|
|
|
|
|
T FirstOrDefault<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断指定条件的记录是否存在
|
|
|
|
|
/// </summary>
|
|
|
|
|
bool Any<T>(Expression<Func<T, bool>> exp) where T:class;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
|
|
|
|
|
|
|
|
|
IQueryable<T> Find<T>(int pageindex = 1, int pagesize = 10, string orderby = "",
|
|
|
|
|
Expression<Func<T, bool>> exp = null) where T:class;
|
|
|
|
|
|
2020-12-17 23:04:04 +08:00
|
|
|
|
int Count<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增对象,如果Id为空,则会自动创建默认Id
|
|
|
|
|
/// </summary>
|
2020-10-22 14:59:36 +08:00
|
|
|
|
void Add<T>(T entity) where T:BaseEntity;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量新增对象,如果对象Id为空,则会自动创建默认Id
|
|
|
|
|
/// </summary>
|
2020-10-22 14:59:36 +08:00
|
|
|
|
void BatchAdd<T>(T[] entities) where T:BaseEntity;
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新一个实体的所有属性
|
|
|
|
|
/// </summary>
|
|
|
|
|
void Update<T>(T entity) where T:class;
|
|
|
|
|
|
|
|
|
|
void Delete<T>(T entity) where T:class;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 实现按需要只更新部分更新
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <para>如:Update<User>(u =>u.Id==1,u =>new User{Name="ok"})</para>
|
|
|
|
|
/// <para>该方法内部自动调用了SaveChanges(),需要ExecuteWithTransaction配合才能实现事务控制</para>
|
2016-04-29 16:26:09 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="where">更新条件</param>
|
|
|
|
|
/// <param name="entity">更新后的实体</param>
|
|
|
|
|
void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量删除
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// <para>该方法内部自动调用了SaveChanges(),需要ExecuteWithTransaction配合才能实现事务控制</para>
|
2016-04-29 16:26:09 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
void Delete<T>(Expression<Func<T, bool>> exp) where T:class;
|
|
|
|
|
|
|
|
|
|
void Save();
|
2020-12-17 23:04:04 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-07-22 19:33:55 +08:00
|
|
|
|
/// 该方法不支持EF自带的事务,需要ExecuteWithTransaction配合才能实现事务控制,详见:http://doc.openauth.net.cn/core/unitwork.html
|
2020-12-17 23:04:04 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sql"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-10-22 14:59:36 +08:00
|
|
|
|
int ExecuteSql(string sql);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用SQL脚本查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"> T为数据库实体</typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
IQueryable<T> FromSql<T>(string sql, params object[] parameters) where T:class;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用SQL脚本查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"> T为非数据库实体,需要在DbContext中增加对应的DbQuery</typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
IQueryable<T> Query<T>(string sql, params object[] parameters) where T : class;
|
2020-12-17 23:04:04 +08:00
|
|
|
|
|
2021-04-28 10:20:24 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行存储过程
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="procName">存储过程名称</param>
|
|
|
|
|
/// <param name="sqlParams">存储过程参数</param>
|
|
|
|
|
List<T> ExecProcedure<T>(string procName,params DbParameter[] sqlParams) where T : class;
|
|
|
|
|
|
2020-12-17 23:04:04 +08:00
|
|
|
|
#region 异步接口
|
|
|
|
|
|
|
|
|
|
Task<int> ExecuteSqlRawAsync(string sql);
|
|
|
|
|
Task<int> SaveAsync();
|
|
|
|
|
Task<int> CountAsync<T>(Expression<Func<T, bool>> exp = null) where T : class;
|
|
|
|
|
Task<bool> AnyAsync<T>(Expression<Func<T, bool>> exp) where T : class;
|
|
|
|
|
Task<T> FirstOrDefaultAsync<T>(Expression<Func<T, bool>> exp) where T : class;
|
|
|
|
|
|
|
|
|
|
#endregion
|
2016-04-29 16:26:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|