ow
This commit is contained in:
commit
f05d2d215f
678 files changed
+73865
No files matched your search
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示数据库类型,如SqlServer,Sqlite等
|
||||
/// </summary>
|
||||
public enum DatabaseType
|
||||
{
|
||||
/// <summary>
|
||||
/// SqlServer数据库类型
|
||||
/// </summary>
|
||||
SqlServer,
|
||||
/// <summary>
|
||||
/// Sqlite数据库类型
|
||||
/// </summary>
|
||||
Sqlite,
|
||||
/// <summary>
|
||||
/// MySql数据库类型
|
||||
/// </summary>
|
||||
MySql,
|
||||
/// <summary>
|
||||
/// PostgreSql数据库类型
|
||||
/// </summary>
|
||||
PostgreSql,
|
||||
/// <summary>
|
||||
/// Oracle数据库类型
|
||||
/// </summary>
|
||||
Oracle
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 实体类基类
|
||||
/// </summary>
|
||||
public abstract class EntityBase<TKey> : IEntity<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置 编号
|
||||
/// </summary>
|
||||
[DisplayName("编号")]
|
||||
[NotMapped]
|
||||
public TKey? Id { get; set; }
|
||||
|
||||
public virtual string GetSelectFrom()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual string GetSelectCountFrom(string sqlWhere)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual string GetSelectTopFrom(int limit, int start, string orderby, string orderbytag, string sqlWhere)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断两个实体是否是同一数据记录的实体
|
||||
/// </summary>
|
||||
/// <param name="obj">要比较的实体信息</param>
|
||||
/// <returns></returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!(obj is EntityBase<TKey> entity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return IsKeyEqual(entity.Id, Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实体ID是否相等
|
||||
/// </summary>
|
||||
public static bool IsKeyEqual(TKey id1, TKey id2)
|
||||
{
|
||||
if (id1 == null && id2 == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (id1 == null || id2 == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Equals(id1, id2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用作特定类型的哈希函数。
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// 当前 <see cref="T:System.Object"/> 的哈希代码。<br/>
|
||||
/// 如果<c>Id</c>为<c>null</c>则返回0,
|
||||
/// 如果不为<c>null</c>则返回<c>Id</c>对应的哈希值
|
||||
/// </returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
if (Id == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return Id.ToString().GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Policy;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
public interface IApplicationService
|
||||
{
|
||||
CloudBuilderBuilder CloudBuilder { get; set; }
|
||||
IConfiguration Configuration { get; }
|
||||
/// <summary>
|
||||
/// 服务提供器
|
||||
/// </summary>
|
||||
IServiceProvider ServiceProvider { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 登录ID
|
||||
/// </summary>
|
||||
string UserId { get; set; }
|
||||
string Locale { get; set; }
|
||||
/// <summary>
|
||||
/// 事务
|
||||
/// </summary>
|
||||
IUnitOfWork UnitOfWork { get; set; }
|
||||
/// <summary>
|
||||
/// 数据仓库
|
||||
/// </summary>
|
||||
/// <typeparam name="K"></typeparam>
|
||||
/// <returns></returns>
|
||||
K GetRepository<K>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据模型接口
|
||||
/// </summary>
|
||||
public interface IEntity<TKey> : IGet
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取或设置 实体唯一标识,主键
|
||||
/// </summary>
|
||||
TKey Id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
public interface IEntityAudited
|
||||
{
|
||||
public const string CREATED_DATETIME = "CreatedDatetime";
|
||||
public const string CREATED_BY = "CreatedBy";
|
||||
public const string UPDATED_DATETIME = "UpdatedDatetime";
|
||||
public const string UPDATED_BY = "UpdatedBy";
|
||||
public const string ROW_VERSION = "RowVersion";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
public interface IGet
|
||||
{
|
||||
string GetSelectFrom();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册主库实体类
|
||||
/// </summary>
|
||||
public interface IMaster
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义实体仓储模型的数据标准操作
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">实体类型</typeparam>
|
||||
/// <typeparam name="TKey">主键类型</typeparam>
|
||||
public interface IRepository<TEntity>
|
||||
where TEntity : class
|
||||
{
|
||||
/// <summary>
|
||||
/// 实体集合
|
||||
/// </summary>
|
||||
DbSet<TEntity> Entities { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 不跟踪的(脱轨)实体
|
||||
/// </summary>
|
||||
IQueryable<TEntity> DetachedEntities { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务提供器
|
||||
/// </summary>
|
||||
IServiceProvider ServiceProvider { get; }
|
||||
/// <summary>
|
||||
/// 事务
|
||||
/// </summary>
|
||||
IUnitOfWork UnitOfWork { get; set; }
|
||||
|
||||
DbContext GetDbContext();
|
||||
|
||||
K GetRepository<K>();
|
||||
/// <summary>
|
||||
/// 动态改变主数据库
|
||||
/// </summary>
|
||||
void ChangeMasterDatabase();
|
||||
|
||||
/// <summary>
|
||||
/// 动态改变主数据库
|
||||
/// </summary>
|
||||
void ChangeSecondaryDatabase();
|
||||
|
||||
#region FromSqlRaw
|
||||
IQueryable<TEntity> FromSql(string sql, params object[] parameters);
|
||||
IQueryable<TEntity> FromSqlWhere(TEntity t, string where, params object[] parameters);
|
||||
|
||||
TEntity[] FindBySql(string sql, params object[] parameters);
|
||||
TEntity[] FindBySqlWhere(TEntity t, string where, params object[] parameters);
|
||||
|
||||
void PackSqlParameter(List<object> parameters, string parameterName, object value);
|
||||
|
||||
void ExecuteNonQuery(string sql, params object[] parameters);
|
||||
|
||||
object ExecuteScalar(string sql, params object[] parameters);
|
||||
|
||||
DataTable SelectDataTableBySql(string sql, params object[] parameters);
|
||||
|
||||
DataTable SelectDataTableByMySql(string sql, params object[] parameters);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Insert
|
||||
|
||||
EntityEntry<TEntity> Insert(TEntity entity);
|
||||
|
||||
void Insert(params TEntity[] entities);
|
||||
|
||||
void Insert(IEnumerable<TEntity> entities);
|
||||
|
||||
Task<EntityEntry<TEntity>> InsertAsync(TEntity entity, CancellationToken cancellationToken = default);
|
||||
|
||||
Task InsertAsync(params TEntity[] entities);
|
||||
|
||||
Task InsertAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
|
||||
|
||||
EntityEntry<TEntity> InsertNow(TEntity entity);
|
||||
|
||||
void InsertNow(params TEntity[] entities);
|
||||
|
||||
void InsertNow(IEnumerable<TEntity> entities);
|
||||
|
||||
Task<EntityEntry<TEntity>> InsertNowAsync(TEntity entity, CancellationToken cancellationToken = default);
|
||||
|
||||
Task InsertNowAsync(params TEntity[] entities);
|
||||
|
||||
Task InsertNowAsync(TEntity[] entities, CancellationToken cancellationToken = default);
|
||||
|
||||
Task InsertNowAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
|
||||
int SaveNow();
|
||||
|
||||
Task<int> SaveNowAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
EntityEntry<TEntity> Update(TEntity entity);
|
||||
void Update(params TEntity[] entities);
|
||||
void Update(IEnumerable<TEntity> entities);
|
||||
Task<EntityEntry<TEntity>> UpdateAsync(TEntity entity);
|
||||
Task UpdateAsync(params TEntity[] entities);
|
||||
Task UpdateAsync(IEnumerable<TEntity> entities);
|
||||
EntityEntry<TEntity> UpdateNow(TEntity entity);
|
||||
void UpdateNow(params TEntity[] entities);
|
||||
void UpdateNow(IEnumerable<TEntity> entities);
|
||||
Task<EntityEntry<TEntity>> UpdateNowAsync(TEntity entity, CancellationToken cancellationToken = default);
|
||||
Task UpdateNowAsync(params TEntity[] entities);
|
||||
Task UpdateNowAsync(TEntity[] entities, CancellationToken cancellationToken = default);
|
||||
Task UpdateNowAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
|
||||
EntityEntry<TEntity> UpdateInclude(TEntity entity, IEnumerable<string> propertyNames);
|
||||
EntityEntry<TEntity> UpdateInclude(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeAsync(TEntity entity, string[] propertyNames);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeAsync(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeAsync(TEntity entity, IEnumerable<string> propertyNames);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeAsync(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
EntityEntry<TEntity> UpdateIncludeNow(TEntity entity, string[] propertyNames);
|
||||
EntityEntry<TEntity> UpdateIncludeNow(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates);
|
||||
EntityEntry<TEntity> UpdateIncludeNow(TEntity entity, IEnumerable<string> propertyNames);
|
||||
EntityEntry<TEntity> UpdateIncludeNow(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeNowAsync(TEntity entity, string[] propertyNames, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeNowAsync(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeNowAsync(TEntity entity, IEnumerable<string> propertyNames, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateIncludeNowAsync(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates, CancellationToken cancellationToken = default);
|
||||
EntityEntry<TEntity> UpdateExclude(TEntity entity, IEnumerable<string> propertyNames);
|
||||
EntityEntry<TEntity> UpdateExclude(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeAsync(TEntity entity, string[] propertyNames);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeAsync(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeAsync(TEntity entity, IEnumerable<string> propertyNames);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeAsync(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
EntityEntry<TEntity> UpdateExcludeNow(TEntity entity, string[] propertyNames);
|
||||
EntityEntry<TEntity> UpdateExcludeNow(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates);
|
||||
EntityEntry<TEntity> UpdateExcludeNow(TEntity entity, IEnumerable<string> propertyNames);
|
||||
EntityEntry<TEntity> UpdateExcludeNow(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeNowAsync(TEntity entity, string[] propertyNames, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeNowAsync(TEntity entity, Expression<Func<TEntity, object>>[] propertyPredicates, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeNowAsync(TEntity entity, IEnumerable<string> propertyNames, CancellationToken cancellationToken = default);
|
||||
Task<EntityEntry<TEntity>> UpdateExcludeNowAsync(TEntity entity, IEnumerable<Expression<Func<TEntity, object>>> propertyPredicates, CancellationToken cancellationToken = default);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Delete
|
||||
EntityEntry<TEntity> Delete(TEntity entity);
|
||||
void Delete(params TEntity[] entities);
|
||||
void Delete(IEnumerable<TEntity> entities);
|
||||
Task<EntityEntry<TEntity>> DeleteAsync(TEntity entity);
|
||||
Task DeleteAsync(params TEntity[] entities);
|
||||
Task DeleteAsync(IEnumerable<TEntity> entities);
|
||||
EntityEntry<TEntity> DeleteNow(TEntity entity);
|
||||
void DeleteNow(params TEntity[] entities);
|
||||
void DeleteNow(IEnumerable<TEntity> entities);
|
||||
Task<EntityEntry<TEntity>> DeleteNowAsync(TEntity entity, CancellationToken cancellationToken = default);
|
||||
Task DeleteNowAsync(params TEntity[] entities);
|
||||
Task DeleteNowAsync(TEntity[] entities, CancellationToken cancellationToken = default);
|
||||
Task DeleteNowAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入从库实体类
|
||||
/// </summary>
|
||||
public interface ISecondary
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 实现接口将生成TypeScript文件的条件
|
||||
/// </summary>
|
||||
public interface IData
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务单元操作接口
|
||||
/// </summary>
|
||||
public interface IUnitOfWork : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 工作单元的事务是否已提交
|
||||
/// </summary>
|
||||
bool HasCommitted { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加DbContext共享事务
|
||||
/// </summary>
|
||||
void AddDbContext(DbContext dbContext);
|
||||
/// <summary>
|
||||
/// 获取指定数据实体的上下文类型
|
||||
/// </summary>
|
||||
/// <param name="entityType">实体类型</param>
|
||||
/// <returns>实体所属上下文实例</returns>
|
||||
DbContext GetDbContext();
|
||||
|
||||
/// <summary>
|
||||
/// 对数据库连接开启事务
|
||||
/// </summary>
|
||||
void BeginOrUseTransaction();
|
||||
|
||||
/// <summary>
|
||||
/// 对数据库连接开启事务
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">异步取消标记</param>
|
||||
/// <returns></returns>
|
||||
Task BeginOrUseTransactionAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 提交当前上下文的事务更改
|
||||
/// </summary>
|
||||
void Commit();
|
||||
|
||||
/// <summary>
|
||||
/// 回滚所有事务
|
||||
/// </summary>
|
||||
void Rollback();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DatabaseAccessor.Entity
|
||||
{
|
||||
public abstract class ViewEntityBase : IEntity<string>
|
||||
{
|
||||
/// <summary>
|
||||
/// View没有主键
|
||||
/// </summary>
|
||||
[NotMapped]
|
||||
public string? Id { get; set; }
|
||||
|
||||
public virtual string GetSelectFrom()
|
||||
{
|
||||
return null!;
|
||||
}
|
||||
|
||||
public virtual string GetSelectCountFrom(string sqlWhere)
|
||||
{
|
||||
return null!;
|
||||
}
|
||||
|
||||
public virtual string GetSelectTopFrom(int limit, int start, string orderby, string orderbytag, string sqlWhere)
|
||||
{
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user