Add EF Core In-Memory DB and repository pattern support

Added the `Microsoft.EntityFrameworkCore.InMemory` package to enable an in-memory database for testing and lightweight storage. Introduced `MessagingServiceDbContext` with `DbSet` properties for `EmailAccount`, `ReceivedEmail`, and `EmailAttachment`. Configured entity relationships in `OnModelCreating`.

Registered `MessagingServiceDbContext` and a generic repository (`IRepository<>`) in the dependency injection container. Updated namespaces and imports to support the new DbContext and repository.

Improved project structure to enhance modularity and testability by leveraging EF Core's in-memory database.
This commit is contained in:
2026-08-12 16:18:35 +02:00
parent be28a61d9c
commit 5c37b2ef92
3 changed files with 34 additions and 1 deletions

View File

@@ -1,10 +1,14 @@
using DigitalData.MessagingService.Application.Common.Interfaces;
using DigitalData.MessagingService.Application.Common.Interfaces.Repositories;
using DigitalData.MessagingService.Infrastructure.Persistence;
using DigitalData.MessagingService.Infrastructure.Queue;
using DigitalData.MessagingService.Infrastructure.Repositories;
using DigitalData.MessagingService.Infrastructure.Services;
using DigitalData.MessagingService.Infrastructure.Services.Background;
using DigitalData.MessagingService.Publisher;
using DigitalData.MessagingService.RabbitMQ;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -54,6 +58,13 @@ public static class DependencyInjection
services.AddMemoryCache();
// --- Database (InMemory) ---
services.AddDbContext<MessagingServiceDbContext>(options =>
options.UseInMemoryDatabase("MessagingServiceDb"));
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
return services;
}
}

View File

@@ -25,6 +25,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.10" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.10" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.65.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.11" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="10.0.10" />
</ItemGroup>

View File

@@ -1,11 +1,32 @@
using DigitalData.MessagingService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.MessagingService.Infrastructure.Persistence;
/// <summary>
/// Entity Framework Core DbContext for MessagingService.
/// IMPORTANT: This context maps to a LEGACY database - NO schema modifications allowed!
/// </summary>
public class MessagingServiceDbContext(DbContextOptions<MessagingServiceDbContext> options) : DbContext(options)
{
public DbSet<EmailAccount> EmailAccounts => Set<EmailAccount>();
public DbSet<ReceivedEmail> ReceivedEmails => Set<ReceivedEmail>();
public DbSet<EmailAttachment> EmailAttachments => Set<EmailAttachment>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ReceivedEmail>(entity =>
{
entity.HasMany(e => e.Attachments)
.WithOne(a => a.Email)
.HasForeignKey(a => a.EmailId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(e => e.Account)
.WithMany()
.HasForeignKey(e => e.AccountId)
.OnDelete(DeleteBehavior.Restrict);
});
}
}