Skip to content

Commit b726538

Browse files
author
Vadim Belov
committed
Refactor EF migration logic and minor code cleanups
- Moved EF Core migration logic to new DbContextExtensions.ApplyMigrations<TContext> with optional logging; HostExtensions now delegates to it. - Made DeletedAtUtc property in IDeletableEntity interface-level only. - Removed unnecessary casts in AesGcmStreamCipher and AesGcmStreamFormat. - Fixed extern/static order in ShellLink ExtractIconEx declaration. - Made fields private and explicit in StopwatchDebuggerTests.
1 parent f44d79e commit b726538

7 files changed

Lines changed: 43 additions & 18 deletions

File tree

Sources/EasyExtensions.Crypto/AesGcmStreamCipher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public AesGcmStreamCipher(ReadOnlyMemory<byte> masterKey, int keyId = 1, int? th
107107
{
108108
throw new ArgumentOutOfRangeException(nameof(windowCap), "Window cap must be >= 4.");
109109
}
110-
if (memoryLimitBytes.HasValue && memoryLimitBytes.Value < (long)(MinChunkSize * 4))
110+
if (memoryLimitBytes.HasValue && memoryLimitBytes.Value < MinChunkSize * 4)
111111
{
112112
throw new ArgumentOutOfRangeException(nameof(memoryLimitBytes), "Memory limit too small for safe operation.");
113113
}

Sources/EasyExtensions.Crypto/Internals/AesGcmStreamFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static void BuildChunkHeader(Span<byte> header, int keyId, Tag128 tag, in
104104
offset += MagicBytes.Length;
105105
BinaryPrimitives.WriteInt32LittleEndian(header[offset..], required);
106106
offset += sizeof(int);
107-
BinaryPrimitives.WriteInt64LittleEndian(header[offset..], (long)textLength);
107+
BinaryPrimitives.WriteInt64LittleEndian(header[offset..], textLength);
108108
offset += sizeof(long);
109109
BinaryPrimitives.WriteInt32LittleEndian(header[offset..], keyId);
110110
offset += sizeof(int);

Sources/EasyExtensions.EntityFrameworkCore/Abstractions/IDeletableEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IDeletableEntity
1111
/// When entity was deleted.
1212
/// </summary>
1313
[Column("deleted_at_utc")]
14-
public DateTime? DeletedAtUtc { get; set; }
14+
DateTime? DeletedAtUtc { get; set; }
1515

1616
/// <summary>
1717
/// Calling this method must sets <see cref="DeletedAtUtc"/> to <see cref="DateTime.UtcNow"/>.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace EasyExtensions.EntityFrameworkCore.Extensions
5+
{
6+
/// <summary>
7+
/// Provides extension methods for applying database migrations to a DbContext instance.
8+
/// </summary>
9+
public static class DbContextExtensions
10+
{
11+
/// <summary>
12+
/// Applies all pending database migrations for the specified DbContext instance.
13+
/// </summary>
14+
/// <remarks>This method checks for any pending migrations and applies them in order. If there are
15+
/// no pending migrations, no action is taken. Logging is performed for each migration if a logger is
16+
/// provided.</remarks>
17+
/// <typeparam name="TContext">The type of the DbContext to apply migrations to.</typeparam>
18+
/// <param name="context">The DbContext instance whose pending migrations will be applied. Cannot be null.</param>
19+
/// <param name="logger">An optional logger used to record information about the migration process. If null, no logging is performed.</param>
20+
public static void ApplyMigrations<TContext>(this TContext context, ILogger? logger) where TContext : DbContext
21+
{
22+
var migrations = context.Database.GetPendingMigrations();
23+
if (migrations.Any())
24+
{
25+
foreach (var migration in migrations)
26+
{
27+
logger?.LogInformation("Applying migration {Migration}.", migration);
28+
}
29+
context.Database.Migrate();
30+
logger?.LogInformation("Migrations applied.");
31+
}
32+
}
33+
}
34+
}

Sources/EasyExtensions.EntityFrameworkCore/Extensions/HostExtensions.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,8 @@ public static IHost ApplyMigrations<TContext>(this IHost host) where TContext :
1919
using (var serviceScope = host.Services.GetService<IServiceScopeFactory>()!.CreateScope())
2020
{
2121
var context = serviceScope.ServiceProvider.GetRequiredService<TContext>();
22-
var migrations = context.Database.GetPendingMigrations();
23-
if (migrations.Any())
24-
{
25-
var logger = serviceScope.ServiceProvider.GetRequiredService<ILogger<DbContext>>();
26-
foreach (var migration in migrations)
27-
{
28-
logger.LogInformation("Applying migration {Migration}.", migration);
29-
}
30-
context.Database.Migrate();
31-
logger.LogInformation("Migrations applied.");
32-
}
22+
var logger = serviceScope.ServiceProvider.GetRequiredService<ILogger<DbContext>>();
23+
context.ApplyMigrations(logger);
3324
}
3425
return host;
3526
}

Sources/EasyExtensions.Tests/StopwatchDebuggerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace EasyExtensions.Tests
44
{
55
internal class StopwatchDebuggerTests
66
{
7-
readonly StopwatchDebugger debugger;
8-
string action = string.Empty;
9-
TimeSpan elapsed = TimeSpan.Zero;
7+
private readonly StopwatchDebugger debugger;
8+
private string action = string.Empty;
9+
private TimeSpan elapsed = TimeSpan.Zero;
1010

1111
public StopwatchDebuggerTests()
1212
{

Sources/EasyExtensions.Windows/ShellLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ private struct FILETIME
397397
private class UnmanagedMethods
398398
{
399399
[DllImport("Shell32", CharSet = CharSet.Auto)]
400-
internal extern static int ExtractIconEx([MarshalAs(UnmanagedType.LPTStr)]
400+
internal static extern int ExtractIconEx([MarshalAs(UnmanagedType.LPTStr)]
401401
string lpszFile, int nIconIndex, IntPtr[]? phIconLarge, IntPtr[]? phIconSmall, int nIcons);
402402

403403
[DllImport("user32")]

0 commit comments

Comments
 (0)