baseline: 小军 WMS 二开代码原始快照(含 SynOABusiness.cs Debugger.Break)
This commit is contained in:
commit
c2b0e7be0d
6522 files changed
+3530643
No files matched your search
+212
@@ -0,0 +1,212 @@
|
||||
using Dapper.Contrib.Extensions;
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
using Opsoft.NetCore.O3App.Shared;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
internal abstract class BaseRepository<TEntity, TRepository>
|
||||
where TEntity : class, new()
|
||||
{
|
||||
private const string EntitySuffix = "Entity";
|
||||
|
||||
private readonly IDapperContextFactory dapperContextFactory;
|
||||
|
||||
protected IMapper Mapper { get; }
|
||||
|
||||
protected ILogger Logger { get; }
|
||||
|
||||
protected string DatabaseName { get; }
|
||||
|
||||
protected virtual string TableName
|
||||
{
|
||||
get
|
||||
{
|
||||
return typeof(TEntity).Name.Replace(EntitySuffix, string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Func<Type, string> TableNameMapper => type => type.Name.Replace(EntitySuffix, string.Empty);
|
||||
|
||||
public BaseRepository(
|
||||
IDapperContextFactory dapperContextFactory,
|
||||
IMapper mapper,
|
||||
ILogger<TRepository> logger,
|
||||
string databaseName)
|
||||
{
|
||||
this.dapperContextFactory = Guard.ArgumentNotNull(dapperContextFactory, nameof(dapperContextFactory));
|
||||
this.Mapper = Guard.ArgumentNotNull(mapper, nameof(mapper));
|
||||
this.Logger = Guard.ArgumentNotNull(logger, nameof(logger));
|
||||
this.DatabaseName = Guard.ArgumentNotNullOrWhiteSpace(databaseName, nameof(databaseName));
|
||||
}
|
||||
|
||||
static BaseRepository()
|
||||
{
|
||||
SqlMapperExtensions.GetDatabaseType = DbConnectionExtensions.GetDatabaseType;
|
||||
SqlMapperExtensions.TableNameMapper = type => TableNameMapper(type);
|
||||
}
|
||||
|
||||
public async Task<List<TEntity>> QueryAsync(string sql, object parameters = null, bool isReadDb = false)
|
||||
{
|
||||
Guard.ArgumentNotNullOrWhiteSpace(sql, nameof(sql));
|
||||
|
||||
try
|
||||
{
|
||||
var entities = await GetDapperContext().QueryAsync<TEntity>(sql, parameters);
|
||||
return entities?.ToList() ?? new List<TEntity>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, new { sql, parameters });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<long> AddAsync(TEntity entity)
|
||||
{
|
||||
Guard.ArgumentNotNull(entity, nameof(entity));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().AddAsync(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, entity);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<long> AddListAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
Guard.ArgumentNotNull(entities, nameof(entities));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().AddAsync(entities);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, entities);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(TEntity entity)
|
||||
{
|
||||
Guard.ArgumentNotNull(entity, nameof(entity));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().UpdateAsync(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, entity);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateListAsync(IEnumerable<TEntity> entities)
|
||||
{
|
||||
Guard.ArgumentNotNull(entities, nameof(entities));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().UpdateAsync(entities);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, entities);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(TEntity entity)
|
||||
{
|
||||
Guard.ArgumentNotNull(entity, nameof(entity));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().DeleteAsync(entity);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, entity);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> ExecuteScalarAsync<T>(string sql, object parameters = null)
|
||||
{
|
||||
Guard.ArgumentNotNullOrWhiteSpace(sql, nameof(sql));
|
||||
|
||||
try
|
||||
{
|
||||
return await GetDapperContext().ExecuteScalarAsync<T>(sql, parameters);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, new { sql, parameters });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEntity> QueryFirstAsync(string sql, object parameters = null, bool isReadDb = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var entities = await GetDapperContext().QueryAsync<TEntity>(sql, parameters);
|
||||
return entities?.FirstOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, new { sql, parameters });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetByIdAsync(long id)
|
||||
{
|
||||
var sql = $"{GetSelectSql()} and Id = @Id";
|
||||
try
|
||||
{
|
||||
var entities = await GetDapperContext().QueryAsync<TEntity>(sql, new { @Id = id });
|
||||
return entities?.FirstOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, new { sql, id });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEntity> GetByGuidAsync(string guid)
|
||||
{
|
||||
var sql = $"{GetSelectSql()} and Guid = @Guid";
|
||||
try
|
||||
{
|
||||
var entities = await GetDapperContext().QueryAsync<TEntity>(sql, new { @Guid = guid });
|
||||
return entities?.FirstOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, new { sql, guid });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual string GetSelectSql()
|
||||
{
|
||||
return $"select * from {TableName} where 1 = 1";
|
||||
}
|
||||
|
||||
protected virtual DapperContext GetDapperContext(bool @readonly = false)
|
||||
{
|
||||
return dapperContextFactory.Create(this.DatabaseName, @readonly) as DapperContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user