db configuration provider
This commit is contained in:
@@ -18,14 +18,17 @@ namespace PARR.API.Controllers.V1
|
|||||||
{
|
{
|
||||||
private readonly IMapper mapper;
|
private readonly IMapper mapper;
|
||||||
private readonly ITemplateService templateService;
|
private readonly ITemplateService templateService;
|
||||||
|
private readonly SettingsFromDb settingsFromDb;
|
||||||
|
|
||||||
public TemplateController(
|
public TemplateController(
|
||||||
IMapper mapper,
|
IMapper mapper,
|
||||||
ITemplateService templateService
|
ITemplateService templateService,
|
||||||
|
SettingsFromDb settingsFromDb
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
this.templateService = templateService;
|
this.templateService = templateService;
|
||||||
|
this.settingsFromDb = settingsFromDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ builder.Services.InstallApiServices(builder.Configuration);
|
|||||||
builder.Services.InstallSettings(builder.Configuration);
|
builder.Services.InstallSettings(builder.Configuration);
|
||||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
|
|
||||||
|
// Dal configuration
|
||||||
|
builder.Configuration.AddDalConfigurations(builder.Services);
|
||||||
|
builder.Services.AddDallSettings(builder.Configuration);
|
||||||
|
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace PARR.DAL.Configurations.DbSettings
|
||||||
|
{
|
||||||
|
internal static class DBConfigurationExtantions
|
||||||
|
{
|
||||||
|
public static IConfigurationBuilder AddDbSettings(this IConfigurationBuilder builder, IServiceCollection services)
|
||||||
|
{
|
||||||
|
return builder.Add(new DBConfigurationSource(services));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PARR.DAL.Context;
|
||||||
|
using PARR.DAL.Contracts;
|
||||||
|
|
||||||
|
namespace PARR.DAL.Configurations.DbSettings
|
||||||
|
{
|
||||||
|
internal class DBConfigurationProvider : ConfigurationProvider
|
||||||
|
{
|
||||||
|
private readonly IServiceCollection services;
|
||||||
|
|
||||||
|
public DBConfigurationProvider(IServiceCollection services)
|
||||||
|
{
|
||||||
|
this.services = services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Load()
|
||||||
|
{
|
||||||
|
using (var dataContext = services.BuildServiceProvider().GetService<DataContext>())
|
||||||
|
{
|
||||||
|
//если миграциями БД еще не создана, то SettingsInDbs будет недоступна
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var settings = dataContext?.Setting.ToList();
|
||||||
|
|
||||||
|
if (settings == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (settings != null)
|
||||||
|
{
|
||||||
|
//формируем список с корневым SettingsFromDb, и в него вложены дочерние настройки из БД, чтобы потом забиндить в класс
|
||||||
|
Data = settings.ToDictionary(settings => $"{nameof(SettingsFromDb)}:{settings.Name}", settings => (string?)settings.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//TODO: логи, я не тестил, возможно надо через BuildServiceProvider прокинуть ILogger
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
PARR.DAL/Configurations/DbSettings/DBConfigurationSource.cs
Normal file
20
PARR.DAL/Configurations/DbSettings/DBConfigurationSource.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace PARR.DAL.Configurations.DbSettings
|
||||||
|
{
|
||||||
|
internal class DBConfigurationSource : IConfigurationSource
|
||||||
|
{
|
||||||
|
private readonly IServiceCollection services;
|
||||||
|
|
||||||
|
public DBConfigurationSource(IServiceCollection services)
|
||||||
|
{
|
||||||
|
this.services = services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfigurationProvider Build(IConfigurationBuilder builder)
|
||||||
|
{
|
||||||
|
return new DBConfigurationProvider(services);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -99,10 +99,11 @@ namespace PARR.DAL.Context
|
|||||||
|
|
||||||
modelBuilder.Entity<Models.Setting>(f =>
|
modelBuilder.Entity<Models.Setting>(f =>
|
||||||
{
|
{
|
||||||
|
// При добавлении записей, добавлять тоже в PARR.DAL.Contracts.SettingsFromDb
|
||||||
f.HasData(
|
f.HasData(
|
||||||
new { Name = SettingsEnum.Initiator.ToString(), Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.", Type = SettingsTypesEnum.String.ToString(), Value = "" },
|
new { Name = nameof(SettingsFromDb.Initiator), Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.", Value = "КУЗНЕЦОВ МИХАИЛ ВАЛЕРЬЕВИЧ (IVC_KUZNETSOVMV@DVGD.OAO.RZD)" },
|
||||||
new { Name = SettingsEnum.ClosingCode.ToString(), Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.", Type = SettingsTypesEnum.String.ToString(), Value = "выполнен" },
|
new { Name = nameof(SettingsFromDb.ClosingCode), Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.", Value = "выполнен" },
|
||||||
new { Name = SettingsEnum.Category.ToString(), Description = "Категория создаваемого объекта в ЕСПП", Type = SettingsTypesEnum.String.ToString(), Value = "регламентная работа" }
|
new { Name = nameof(SettingsFromDb.Category), Description = "Категория создаваемого объекта в ЕСПП", Value = "регламентная работа" }
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace PARR.DAL.Contracts
|
|
||||||
{
|
|
||||||
public enum SettingsEnum
|
|
||||||
{
|
|
||||||
Initiator,
|
|
||||||
ClosingCode,
|
|
||||||
Category
|
|
||||||
}
|
|
||||||
}
|
|
||||||
26
PARR.DAL/Contracts/SettingsFromDb.cs
Normal file
26
PARR.DAL/Contracts/SettingsFromDb.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
namespace PARR.DAL.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Настройки из БД
|
||||||
|
/// </summary>
|
||||||
|
public class SettingsFromDb
|
||||||
|
{
|
||||||
|
// При добавлении свойств, добавлять в миграцию: PARR.DAL.Context.DataContext
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.
|
||||||
|
/// </summary>
|
||||||
|
public string Initiator { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.
|
||||||
|
/// </summary>
|
||||||
|
public string ClosingCode { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Категория создаваемого объекта в ЕСПП
|
||||||
|
/// </summary>
|
||||||
|
public string Category { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace PARR.DAL.Contracts
|
|
||||||
{
|
|
||||||
public enum SettingsTypesEnum
|
|
||||||
{
|
|
||||||
Int,
|
|
||||||
Decimal,
|
|
||||||
String,
|
|
||||||
Bool
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,7 @@ using PARR.DAL.Context;
|
|||||||
namespace PARR.DAL.Migrations
|
namespace PARR.DAL.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(DataContext))]
|
[DbContext(typeof(DataContext))]
|
||||||
[Migration("20230919020903_TblTemplWorks")]
|
[Migration("20230919043205_TblTemplWorks")]
|
||||||
partial class TblTemplWorks
|
partial class TblTemplWorks
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -445,10 +445,6 @@ namespace PARR.DAL.Migrations
|
|||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text");
|
.HasColumnType("text");
|
||||||
|
|
||||||
b.Property<string>("Type")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("Value")
|
b.Property<string>("Value")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text");
|
.HasColumnType("text");
|
||||||
@@ -462,21 +458,18 @@ namespace PARR.DAL.Migrations
|
|||||||
{
|
{
|
||||||
Name = "Initiator",
|
Name = "Initiator",
|
||||||
Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.",
|
Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.",
|
||||||
Type = "String",
|
Value = "КУЗНЕЦОВ МИХАИЛ ВАЛЕРЬЕВИЧ (IVC_KUZNETSOVMV@DVGD.OAO.RZD)"
|
||||||
Value = ""
|
|
||||||
},
|
},
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
Name = "ClosingCode",
|
Name = "ClosingCode",
|
||||||
Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.",
|
Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.",
|
||||||
Type = "String",
|
|
||||||
Value = "выполнен"
|
Value = "выполнен"
|
||||||
},
|
},
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
Name = "Category",
|
Name = "Category",
|
||||||
Description = "Категория создаваемого объекта в ЕСПП",
|
Description = "Категория создаваемого объекта в ЕСПП",
|
||||||
Type = "String",
|
|
||||||
Value = "регламентная работа"
|
Value = "регламентная работа"
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -117,9 +117,8 @@ namespace PARR.DAL.Migrations
|
|||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
Name = table.Column<string>(type: "text", nullable: false),
|
Name = table.Column<string>(type: "text", nullable: false),
|
||||||
Description = table.Column<string>(type: "text", nullable: false),
|
Value = table.Column<string>(type: "text", nullable: false),
|
||||||
Type = table.Column<string>(type: "text", nullable: false),
|
Description = table.Column<string>(type: "text", nullable: false)
|
||||||
Value = table.Column<string>(type: "text", nullable: false)
|
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
@@ -128,12 +127,12 @@ namespace PARR.DAL.Migrations
|
|||||||
|
|
||||||
migrationBuilder.InsertData(
|
migrationBuilder.InsertData(
|
||||||
table: "Setting",
|
table: "Setting",
|
||||||
columns: new[] { "Name", "Description", "Type", "Value" },
|
columns: new[] { "Name", "Description", "Value" },
|
||||||
values: new object[,]
|
values: new object[,]
|
||||||
{
|
{
|
||||||
{ "Category", "Категория создаваемого объекта в ЕСПП", "String", "регламентная работа" },
|
{ "Category", "Категория создаваемого объекта в ЕСПП", "регламентная работа" },
|
||||||
{ "ClosingCode", "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.", "String", "выполнен" },
|
{ "ClosingCode", "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.", "выполнен" },
|
||||||
{ "Initiator", "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.", "String", "" }
|
{ "Initiator", "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.", "КУЗНЕЦОВ МИХАИЛ ВАЛЕРЬЕВИЧ (IVC_KUZNETSOVMV@DVGD.OAO.RZD)" }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,10 +442,6 @@ namespace PARR.DAL.Migrations
|
|||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text");
|
.HasColumnType("text");
|
||||||
|
|
||||||
b.Property<string>("Type")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("text");
|
|
||||||
|
|
||||||
b.Property<string>("Value")
|
b.Property<string>("Value")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("text");
|
.HasColumnType("text");
|
||||||
@@ -459,21 +455,18 @@ namespace PARR.DAL.Migrations
|
|||||||
{
|
{
|
||||||
Name = "Initiator",
|
Name = "Initiator",
|
||||||
Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.",
|
Description = "Инициатор регламентной работы, указывается при создании шаблона в ЕСПП.",
|
||||||
Type = "String",
|
Value = "КУЗНЕЦОВ МИХАИЛ ВАЛЕРЬЕВИЧ (IVC_KUZNETSOVMV@DVGD.OAO.RZD)"
|
||||||
Value = ""
|
|
||||||
},
|
},
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
Name = "ClosingCode",
|
Name = "ClosingCode",
|
||||||
Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.",
|
Description = "Код закрытия регламентной работы, указывается при создании шаблона в ЕСПП.",
|
||||||
Type = "String",
|
|
||||||
Value = "выполнен"
|
Value = "выполнен"
|
||||||
},
|
},
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
Name = "Category",
|
Name = "Category",
|
||||||
Description = "Категория создаваемого объекта в ЕСПП",
|
Description = "Категория создаваемого объекта в ЕСПП",
|
||||||
Type = "String",
|
|
||||||
Value = "регламентная работа"
|
Value = "регламентная работа"
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,11 +9,8 @@ namespace PARR.DAL.Models
|
|||||||
[Key]
|
[Key]
|
||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
|
|
||||||
public required string Description { get; set; }
|
|
||||||
|
|
||||||
//SettingsTypesEnum
|
|
||||||
public required string Type { get; set; }
|
|
||||||
|
|
||||||
public required string Value { get; set; }
|
public required string Value { get; set; }
|
||||||
|
|
||||||
|
public required string Description { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PARR.DAL.Configurations.DbSettings;
|
||||||
using PARR.DAL.Context;
|
using PARR.DAL.Context;
|
||||||
|
using PARR.DAL.Contracts;
|
||||||
using PARR.DAL.Services;
|
using PARR.DAL.Services;
|
||||||
using PARR.DAL.Services.Implementation;
|
using PARR.DAL.Services.Implementation;
|
||||||
using PARR.DAL.Services.Implementations;
|
using PARR.DAL.Services.Implementations;
|
||||||
@@ -15,11 +17,16 @@ namespace PARR.DAL
|
|||||||
{
|
{
|
||||||
public static class ParrDalInstaller
|
public static class ParrDalInstaller
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Устанавливает Dal сервисы (1)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
public static void InstallDalServices(this IServiceCollection services, IConfiguration configuration)
|
public static void InstallDalServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.AddDbContext<DataContext>(opt =>
|
services.AddDbContext<DataContext>(opt =>
|
||||||
opt.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
opt.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||||
);
|
);
|
||||||
|
|
||||||
services.AddTransient<V1_IHostService, V1_HostService>();
|
services.AddTransient<V1_IHostService, V1_HostService>();
|
||||||
services.AddTransient<IHostService, HostService>();
|
services.AddTransient<IHostService, HostService>();
|
||||||
@@ -36,5 +43,34 @@ namespace PARR.DAL
|
|||||||
services.AddTransient<ITemplateService, TemplateService>();
|
services.AddTransient<ITemplateService, TemplateService>();
|
||||||
services.AddTransient<IStatusTemplateService, StatusTemplateService>();
|
services.AddTransient<IStatusTemplateService, StatusTemplateService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Добавляет конфигурацию Dal (2)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IConfigurationBuilder AddDalConfigurations(this IConfigurationBuilder builder, IServiceCollection services)
|
||||||
|
{
|
||||||
|
builder.Add(new DBConfigurationSource(services));
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Добавляет конфигурацию в сервисы (3)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddDallSettings(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
//SettingsFromDb configuration
|
||||||
|
var settingsFromDb = new SettingsFromDb();
|
||||||
|
configuration.GetSection(nameof(SettingsFromDb)).Bind(settingsFromDb);
|
||||||
|
services.AddSingleton(settingsFromDb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user