netcore-blueprint 0.0.64 → 0.0.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BlueprintTemplate/Host.API/Extensions/DatabaseExtensions.cs +33 -7
- package/BlueprintTemplate/Host.API/Extensions/ModuleExtension.cs +5 -0
- package/BlueprintTemplate/Host.API/appsettings.json +1 -0
- package/BlueprintTemplate/Modules/__MODULE__Module/__MODULE__Module.API/__MODULE__Module.cs +1 -0
- package/BlueprintTemplate/Shared.Core/Interfaces/Modules/IModule.cs +1 -0
- package/BlueprintTemplate/Shared.Infrastructure/Services/RepositoryFactory.cs +16 -4
- package/BlueprintTemplate/User.API/UserModule.cs +1 -0
- package/package.json +1 -1
|
@@ -9,28 +9,54 @@ namespace Host.API.Extensions
|
|
|
9
9
|
this IServiceCollection services,
|
|
10
10
|
IConfiguration configuration)
|
|
11
11
|
{
|
|
12
|
-
|
|
13
|
-
configuration.GetConnectionString("MySQL");
|
|
12
|
+
var dbEnabled = configuration.GetValue<bool>("Database:Enabled");
|
|
14
13
|
|
|
15
|
-
if (!
|
|
14
|
+
if (!dbEnabled)
|
|
16
15
|
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
mySQLConnectionString,
|
|
20
|
-
ServerVersion.AutoDetect(mySQLConnectionString)));
|
|
16
|
+
Console.WriteLine("⏭️ Database is disabled.");
|
|
17
|
+
return services;
|
|
21
18
|
}
|
|
22
19
|
|
|
20
|
+
var dbType = configuration["Database:Type"]?.ToLower();
|
|
21
|
+
|
|
22
|
+
if (dbType != "mysql")
|
|
23
|
+
return services;
|
|
24
|
+
|
|
25
|
+
var mySQLConnectionString =
|
|
26
|
+
configuration.GetConnectionString("MySql");
|
|
27
|
+
|
|
28
|
+
if (string.IsNullOrEmpty(mySQLConnectionString))
|
|
29
|
+
throw new InvalidOperationException("MySql connection string is missing.");
|
|
30
|
+
|
|
31
|
+
services.AddDbContext<MyDbContext>(options =>
|
|
32
|
+
options.UseMySql(
|
|
33
|
+
mySQLConnectionString,
|
|
34
|
+
ServerVersion.AutoDetect(mySQLConnectionString)));
|
|
35
|
+
|
|
36
|
+
Console.WriteLine("✅ MySQL database registered.");
|
|
37
|
+
|
|
23
38
|
return services;
|
|
24
39
|
}
|
|
25
40
|
|
|
26
41
|
public static void ApplyDatabaseMigrations(this WebApplication app)
|
|
27
42
|
{
|
|
43
|
+
var config = app.Services.GetRequiredService<IConfiguration>();
|
|
44
|
+
var dbEnabled = config.GetValue<bool>("Database:Enabled");
|
|
45
|
+
|
|
46
|
+
if (!dbEnabled)
|
|
47
|
+
{
|
|
48
|
+
Console.WriteLine("⏭️ Skip database migrations (DB disabled).");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
28
52
|
if (!app.Environment.IsDevelopment())
|
|
29
53
|
return;
|
|
30
54
|
|
|
31
55
|
using var scope = app.Services.CreateScope();
|
|
32
56
|
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
|
|
33
57
|
dbContext.Database.Migrate();
|
|
58
|
+
|
|
59
|
+
Console.WriteLine("✅ Database migrations applied.");
|
|
34
60
|
}
|
|
35
61
|
}
|
|
36
62
|
|
|
@@ -9,6 +9,7 @@ namespace Host.API.Extensions
|
|
|
9
9
|
IConfiguration config,
|
|
10
10
|
IMvcBuilder mvcBuilder)
|
|
11
11
|
{
|
|
12
|
+
bool dbEnabled = config.GetValue<bool>("Database:Enabled");
|
|
12
13
|
var modules = AppDomain.CurrentDomain
|
|
13
14
|
.GetAssemblies()
|
|
14
15
|
.SelectMany(a => a.GetTypes())
|
|
@@ -27,6 +28,10 @@ namespace Host.API.Extensions
|
|
|
27
28
|
continue;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
if (module.RequiresDatabase && !dbEnabled)
|
|
32
|
+
throw new InvalidOperationException(
|
|
33
|
+
$"{module.Name} requires database but Database:Enabled=false");
|
|
34
|
+
|
|
30
35
|
Console.WriteLine($"✅ Register module: {module.Name}");
|
|
31
36
|
module.Register(services, config);
|
|
32
37
|
|
|
@@ -12,6 +12,7 @@ namespace __MODULE__Module.API
|
|
|
12
12
|
public class __MODULE__Module : IModule
|
|
13
13
|
{
|
|
14
14
|
public string Name => "__MODULE__";
|
|
15
|
+
public bool RequiresDatabase => true;
|
|
15
16
|
public void Register(IServiceCollection services, IConfiguration config)
|
|
16
17
|
{
|
|
17
18
|
__MODULE__CoreDependencyInjection.AddModule(services);
|
|
@@ -7,6 +7,7 @@ namespace Shared.Core.Interfaces.Modules
|
|
|
7
7
|
public interface IModule
|
|
8
8
|
{
|
|
9
9
|
string Name { get; }
|
|
10
|
+
bool RequiresDatabase { get; }
|
|
10
11
|
void Register(IServiceCollection services, IConfiguration config);
|
|
11
12
|
Assembly ApiAssembly { get; }
|
|
12
13
|
Assembly MappingAssembly { get; }
|
|
@@ -9,17 +9,28 @@ namespace Shared.Infrastructure.Services
|
|
|
9
9
|
{
|
|
10
10
|
public class RepositoryFactory<T> : IRepositoryFactory<T> where T : BaseEntity
|
|
11
11
|
{
|
|
12
|
-
private readonly
|
|
12
|
+
private readonly bool _dbEnabled;
|
|
13
|
+
private readonly string? _databaseType;
|
|
13
14
|
private readonly IServiceProvider _serviceProvider;
|
|
14
15
|
|
|
15
16
|
public RepositoryFactory(IConfiguration configuration, IServiceProvider serviceProvider)
|
|
16
17
|
{
|
|
17
|
-
|
|
18
|
+
_dbEnabled = bool.TryParse(configuration["Database:Enabled"], out var result) && result;
|
|
19
|
+
_databaseType = configuration["Database:Type"];
|
|
18
20
|
_serviceProvider = serviceProvider;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
public IItemInfra<T> CreateItemRepository()
|
|
22
24
|
{
|
|
25
|
+
if (!_dbEnabled)
|
|
26
|
+
{
|
|
27
|
+
throw new InvalidOperationException(
|
|
28
|
+
$"Database is disabled. Repository for {typeof(T).Name} is not available.");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (string.IsNullOrEmpty(_databaseType))
|
|
32
|
+
throw new InvalidOperationException("Database:Type is not configured.");
|
|
33
|
+
|
|
23
34
|
var dbType = _databaseType.ToLower();
|
|
24
35
|
var entityType = typeof(T);
|
|
25
36
|
|
|
@@ -27,10 +38,11 @@ namespace Shared.Infrastructure.Services
|
|
|
27
38
|
{
|
|
28
39
|
"mysql" => typeof(MySQLItemInfra<>).MakeGenericType(entityType),
|
|
29
40
|
"mongodb" => typeof(MongoItemInfra<>).MakeGenericType(entityType),
|
|
30
|
-
_ => throw new ArgumentException("Invalid database type")
|
|
41
|
+
_ => throw new ArgumentException($"Invalid database type: {_databaseType}")
|
|
31
42
|
};
|
|
32
43
|
|
|
33
|
-
return (IItemInfra<T>)ActivatorUtilities.CreateInstance(
|
|
44
|
+
return (IItemInfra<T>)ActivatorUtilities.CreateInstance(
|
|
45
|
+
_serviceProvider, implementationType);
|
|
34
46
|
}
|
|
35
47
|
}
|
|
36
48
|
}
|
|
@@ -12,6 +12,7 @@ namespace UserModule.API
|
|
|
12
12
|
public class UserModule: IModule
|
|
13
13
|
{
|
|
14
14
|
public string Name => "User";
|
|
15
|
+
public bool RequiresDatabase => true;
|
|
15
16
|
public void Register(IServiceCollection services, IConfiguration config)
|
|
16
17
|
{
|
|
17
18
|
UserCoreDependencyInjection.AddModule(services);
|