text-matcher/src/Nocr.TextMatcher.Persistence/ServiceCollectionExtensions.cs
2024-03-30 11:31:23 +03:00

31 lines
1.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nocr.TextMatcher.AppServices.TextSubscriptions.Repositories;
using Nocr.TextMatcher.Persistence.TextSubscriptions;
namespace Nocr.TextMatcher.Persistence;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddEfPersistence(this IServiceCollection services, string connectionString)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException(nameof(connectionString));
services.AddScoped<ITextSubscriptionRepository, TextSubscriptionRepository>();
services.AddDbContext<TextMatcherContext>(
(ctx, context) =>
{
context.UseMySql(connectionString, new MariaDbServerVersion(MariaDbServerVersion.LatestSupportedServerVersion))
.UseLoggerFactory(ctx.GetRequiredService<ILoggerFactory>());
}
);
return services;
}
}