-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseProviderFactory.cs
More file actions
93 lines (81 loc) · 3.02 KB
/
DatabaseProviderFactory.cs
File metadata and controls
93 lines (81 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace OrderMonitor.Infrastructure.Data;
/// <summary>
/// Supported database providers for multi-database support.
/// </summary>
public enum DatabaseProvider
{
SqlServer,
MySql,
PostgreSql
}
/// <summary>
/// Factory for configuring the DbContext with the appropriate database provider.
/// </summary>
public static class DatabaseProviderFactory
{
private static readonly HashSet<string> ValidProviders = new(StringComparer.OrdinalIgnoreCase)
{
"SqlServer", "MySql", "PostgreSql"
};
/// <summary>
/// Parses a provider string into a DatabaseProvider enum value.
/// </summary>
public static DatabaseProvider ParseProvider(string provider)
{
if (string.IsNullOrWhiteSpace(provider))
throw new ArgumentException("Database provider cannot be null or empty.", nameof(provider));
return provider.Trim().ToLowerInvariant() switch
{
"sqlserver" => DatabaseProvider.SqlServer,
"mysql" => DatabaseProvider.MySql,
"postgresql" or "postgres" => DatabaseProvider.PostgreSql,
_ => throw new ArgumentException(
$"Invalid database provider '{provider}'. Allowed values: {string.Join(", ", ValidProviders)}.",
nameof(provider))
};
}
/// <summary>
/// Adds the OrderMonitorDbContext to the service collection with the specified provider.
/// </summary>
public static IServiceCollection AddOrderMonitorDbContext(
this IServiceCollection services,
DatabaseProvider provider,
string connectionString)
{
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentException("Connection string cannot be null or empty.", nameof(connectionString));
services.AddDbContext<OrderMonitorDbContext>(options =>
{
ConfigureProvider(options, provider, connectionString);
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
return services;
}
/// <summary>
/// Configures the DbContextOptionsBuilder for the specified provider.
/// </summary>
public static void ConfigureProvider(
DbContextOptionsBuilder options,
DatabaseProvider provider,
string connectionString)
{
switch (provider)
{
case DatabaseProvider.SqlServer:
options.UseSqlServer(connectionString);
break;
case DatabaseProvider.MySql:
var serverVersion = ServerVersion.AutoDetect(connectionString);
options.UseMySql(connectionString, serverVersion);
break;
case DatabaseProvider.PostgreSql:
options.UseNpgsql(connectionString);
break;
default:
throw new ArgumentOutOfRangeException(nameof(provider), provider,
$"Unsupported database provider: {provider}");
}
}
}