Files
PIbd-32_BuslaevRoman_KOP/WinFormsComponentOrientedHost/Program.cs
2025-09-23 14:47:56 +04:00

75 lines
2.9 KiB
C#

using System;
using System.Windows.Forms;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ComponentOrientedPlatform.Abstractions; // ILicenseProvider
using WinFormsComponentOrientedHost.Composition; // ReflectionLoader
using WinFormsComponentOrientedHost.Infrastructure; // SimpleLogger, HostServicesImpl
using WinFormsComponentOrientedHost.Licensing; // (åñëè õî÷åøü îñòàâèòü ôàéëîâóþ ëèöåíçèþ)
using WinFormsComponentOrientedHost.UI; // FormMain
using WinFormsComponentOrientedHost.Data; // AppDbContextFactory
using WinFormsComponentOrientedHost.Auth; // AuthService, LoginForm, RuntimeLicenseProvider
using WinFormsComponentOrientedHost.Data.Entities; // User (ñóùíîñòü!)
namespace WinFormsComponentOrientedHost
{
internal static class Program
{
public static IConfiguration Configuration { get; private set; } = default!;
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
// Êîíôèã
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
Configuration = builder.Build();
var logger = new SimpleLogger();
// Ñòðîêà ïîäêëþ÷åíèÿ è ôàáðèêà DbContext
var cs = Configuration["Database:ConnectionString"] ?? throw new Exception("No DB connection string");
var dbFactory = new WinFormsComponentOrientedHost.Data.AppDbContextFactory(cs);
// Ìèãðàöèè (èëè EnsureCreated)
try
{
using var db = dbFactory.CreateDbContext();
db.Database.Migrate();
}
catch (Exception ex)
{
logger.Error("DB migrate failed", ex);
}
// === Àóòåíòèôèêàöèÿ ===
var authService = new AuthService(dbFactory);
using var loginForm = new LoginForm(authService);
if (loginForm.ShowDialog() != DialogResult.OK)
return;
User user = loginForm.AuthenticatedUser!;
var runtimeLicense = new RuntimeLicenseProvider(user.accessLevel, owner: user.login);
// HostServices c ëèöåíçèåé ïîëüçîâàòåëÿ
var host = new HostServicesImpl(runtimeLicense, logger);
host.Register<WinFormsComponentOrientedHost.Data.IAppDbContextFactory>(dbFactory);
// Çàãðóç÷èê ïëàãèíîâ
var pluginsPath = Configuration["Plugins:Path"] ?? ".\\Plugins";
var loader = new ReflectionLoader(pluginsPath, logger); // åñëè ó òåáÿ êîíñòðóêòîð áåç host — èñïîëüçóé (pluginsPath, logger)
// Ñòàðò îñíîâíîé ôîðìû
Application.Run(new FormMain(Configuration, host, loader));
}
}
}