Prima implementazione del servizio di connessione e scoperta del database
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DataConnection.Interfaces;
|
||||
using DataConnection.EF.DatabaseDiscovery;
|
||||
using DataConnection.Enums;
|
||||
|
||||
namespace DataConnection.EF;
|
||||
|
||||
/// <summary>
|
||||
/// Opzioni per la configurazione del manager di database esistenti
|
||||
/// </summary>
|
||||
public class DbManagerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuratore del DbContext
|
||||
/// </summary>
|
||||
public Action<DbContextOptionsBuilder> DbContextConfigurator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Configuratore del modello del database
|
||||
/// </summary>
|
||||
public Action<ModelBuilder> ModelConfigurator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Flag che indica se la scoperta automatica delle entità è abilitata
|
||||
/// </summary>
|
||||
public bool EnableAutoDiscovery { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Assembly da cui caricare automaticamente le entità (se EnableAutoDiscovery = true)
|
||||
/// </summary>
|
||||
public System.Reflection.Assembly EntityAssembly { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Namespace in cui cercare le entità (se EnableAutoDiscovery = true)
|
||||
/// </summary>
|
||||
public string EntityNamespace { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Timeout per le operazioni del database (in secondi)
|
||||
/// </summary>
|
||||
public int CommandTimeout { get; set; } = 30;
|
||||
|
||||
/// <summary>
|
||||
/// Strategia di mappatura dei nomi (CamelCase, PascalCase, SnakeCase)
|
||||
/// </summary>
|
||||
public NamingStrategy NamingStrategy { get; set; } = NamingStrategy.Default;
|
||||
|
||||
/// <summary>
|
||||
/// Servizio per la scoperta dei database disponibili sul server
|
||||
/// </summary>
|
||||
public IDatabaseDiscovery DatabaseDiscoveryService { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stringa di connessione a livello di server (senza specificare il database)
|
||||
/// </summary>
|
||||
public string ServerConnectionString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Nome del database a cui connettersi
|
||||
/// </summary>
|
||||
public string DatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Configura automaticamente il servizio di scoperta database in base al tipo di database
|
||||
/// </summary>
|
||||
/// <param name="databaseType">Tipo di database</param>
|
||||
public void ConfigureDatabaseDiscovery(DatabaseType databaseType)
|
||||
{
|
||||
switch (databaseType)
|
||||
{
|
||||
case DatabaseType.SqlServer:
|
||||
DatabaseDiscoveryService = new SqlServerDatabaseDiscovery();
|
||||
break;
|
||||
// case DatabaseType.MySql:
|
||||
// DatabaseDiscoveryService = new MySqlDatabaseDiscovery();
|
||||
// break;
|
||||
// Altri tipi di database possono essere aggiunti qui
|
||||
default:
|
||||
throw new NotSupportedException($"Tipo di database non supportato: {databaseType}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Costruisce una stringa di connessione completa includendo il database selezionato
|
||||
/// </summary>
|
||||
public string BuildFullConnectionString()
|
||||
{
|
||||
if (string.IsNullOrEmpty(ServerConnectionString))
|
||||
{
|
||||
throw new InvalidOperationException("La stringa di connessione al server non è stata specificata");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(DatabaseName))
|
||||
{
|
||||
return ServerConnectionString;
|
||||
}
|
||||
|
||||
// Per SQL Server
|
||||
if (ServerConnectionString.Contains("Initial Catalog=") || ServerConnectionString.Contains("Database="))
|
||||
{
|
||||
// Sostituisci il database esistente
|
||||
var modifiedString = System.Text.RegularExpressions.Regex.Replace(
|
||||
ServerConnectionString,
|
||||
@"(Initial Catalog|Database)=([^;]*)",
|
||||
$"$1={DatabaseName}");
|
||||
|
||||
return modifiedString;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Aggiungi il database
|
||||
return ServerConnectionString + $";Database={DatabaseName}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user