rider recomendations

This commit is contained in:
Фролов 2021-12-08 15:29:41 +05:00
parent 5752ad40b7
commit 6565957c34
2 changed files with 34 additions and 26 deletions

View File

@ -11,26 +11,26 @@ namespace AsbCloudDb
{
public static class EFExtentions
{
static Dictionary<Type, IQueryStringFactory> QueryFactories { get; set; } = new Dictionary<Type, IQueryStringFactory>();
static Dictionary<Type, IQueryStringFactory> QueryFactories { get; set; } = new();
static QueryStringFactory<T> GetQueryStringFactory<T>(DbSet<T> dbset)
static QueryStringFactory<T> GetQueryStringFactory<T>(DbSet<T> dbSet)
where T : class
{
var t = typeof(T);
QueryStringFactory<T> factory = (QueryStringFactory<T>)QueryFactories.GetValueOrDefault(t);
var factory = (QueryStringFactory<T>)QueryFactories.GetValueOrDefault(t);
if (factory is null)
{
factory = new QueryStringFactory<T>(dbset);
factory = new QueryStringFactory<T>(dbSet);
QueryFactories.Add(t, factory);
}
return factory;
}
public static Task<int> ExecInsertOrUpdateAsync<T>(this Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade database, DbSet<T> dbset, IEnumerable<T> items, CancellationToken token)
public static Task<int> ExecInsertOrUpdateAsync<T>(this Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade database, DbSet<T> dbSet, IEnumerable<T> items, CancellationToken token)
where T : class
{
var factory = GetQueryStringFactory(dbset);
var factory = GetQueryStringFactory(dbSet);
var query = factory.MakeInsertOrUpdateSql(items);
return database.ExecuteSqlRawAsync(query, token);
@ -45,19 +45,18 @@ namespace AsbCloudDb
private readonly string insertHeader;
private readonly string pk;
private readonly string tableName;
private readonly string conflictBody;
private readonly IEnumerable<IClrPropertyGetter> getters;
public QueryStringFactory(DbSet<T> dbset)
{
var ps = dbset.EntityType.GetProperties();
var properties = dbset.EntityType.GetProperties();
var pkColsNames = dbset.EntityType.FindPrimaryKey()?.Properties.Select(p => p.GetColumnBaseName());
pk = pkColsNames is null ? string.Empty : $"({string.Join(", ", pkColsNames)})";
tableName = dbset.EntityType.GetTableName();
getters = ps.Select(p => p.GetGetter());
var colNames = ps.Select(p => $"\"{p.GetColumnBaseName()}\"");
var tableName = dbset.EntityType.GetTableName();
getters = properties.Select(p => p.GetGetter());
var colNames = properties.Select(p => $"\"{p.GetColumnBaseName()}\"");
var colunmsString = $"({string.Join(", ", colNames)})";
insertHeader = $"INSERT INTO {tableName} {colunmsString} VALUES ";
@ -87,7 +86,7 @@ namespace AsbCloudDb
private StringBuilder BuildRows(StringBuilder builder, IEnumerable<T> items)
{
var list = items.ToList();
for (int i = 0; i < list.Count; i++)
for (var i = 0; i < list.Count; i++)
{
if(i > 0)
builder.Append(',');

View File

@ -16,16 +16,15 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
private const string sheetNamePlan = "План";
private const string sheetNameFact = "Факт";
const int headerRowsCount = 1;
const int columnSection = 1;
const int columnCategory = 2;
const int columnCategoryInfo = 3;
const int columnDepthStart = 4;
const int columnDepthEnd = 5;
const int columnDate = 6;
const int columnDuration = 7;
const int columnComment = 8;
private const int headerRowsCount = 1;
private const int columnSection = 1;
private const int columnCategory = 2;
private const int columnCategoryInfo = 3;
private const int columnDepthStart = 4;
private const int columnDepthEnd = 5;
private const int columnDate = 6;
private const int columnDuration = 7;
private const int columnComment = 8;
private static readonly DateTime dateLimitMin = new DateTime(2001, 1, 1, 0, 0, 0);
private static readonly DateTime dateLimitMax = new DateTime(2099, 1, 1, 0, 0, 0);
@ -34,10 +33,20 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
private readonly IAsbCloudDbContext db;
private List<WellOperationCategory> categories = null;
public List<WellOperationCategory> Categories =>
categories ?? (categories = db.WellOperationCategories
.AsNoTracking()
.ToList());
public List<WellOperationCategory> Categories
{
get
{
if (categories is null)
{
categories = db.WellOperationCategories
.AsNoTracking()
.ToList();
}
return categories;
}
}
private List<WellSectionType> sections = null;
public List<WellSectionType> Sections