- Simplified the .env.example file by removing outdated comments and consolidating environment variable descriptions. - Updated the architecture documentation to reflect the new structure and components of the .NET 10 microservice template. - Enhanced clarity in the README.md to provide a more comprehensive overview of the template's features and requirements. - Removed obsolete appsettings files to streamline the project structure and improve usability.
58 lines
2.2 KiB
C#
58 lines
2.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MyService.Domain.AggregatesModel.SampleAggregate;
|
|
using MyService.Infrastructure.Idempotency;
|
|
using MyService.Infrastructure.Repositories;
|
|
|
|
namespace MyService.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// EN: Dependency injection extensions for Infrastructure layer.
|
|
/// VI: Extensions dependency injection cho lớp Infrastructure.
|
|
/// </summary>
|
|
public static class DependencyInjection
|
|
{
|
|
/// <summary>
|
|
/// EN: Add infrastructure services to the DI container.
|
|
/// VI: Thêm các services infrastructure vào DI container.
|
|
/// </summary>
|
|
public static IServiceCollection AddInfrastructure(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
// EN: Add DbContext with PostgreSQL / VI: Thêm DbContext với PostgreSQL
|
|
services.AddDbContext<MyServiceContext>(options =>
|
|
{
|
|
var connectionString = configuration.GetConnectionString("DefaultConnection")
|
|
?? configuration["DATABASE_URL"]
|
|
?? throw new InvalidOperationException("Connection string not configured");
|
|
|
|
options.UseNpgsql(connectionString, npgsqlOptions =>
|
|
{
|
|
npgsqlOptions.MigrationsAssembly(typeof(MyServiceContext).Assembly.FullName);
|
|
npgsqlOptions.EnableRetryOnFailure(
|
|
maxRetryCount: 5,
|
|
maxRetryDelay: TimeSpan.FromSeconds(30),
|
|
errorCodesToAdd: null);
|
|
});
|
|
|
|
// EN: Enable sensitive data logging in development only
|
|
// VI: Chỉ bật sensitive data logging trong development
|
|
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
|
|
{
|
|
options.EnableSensitiveDataLogging();
|
|
options.EnableDetailedErrors();
|
|
}
|
|
});
|
|
|
|
// EN: Register repositories / VI: Đăng ký repositories
|
|
services.AddScoped<ISampleRepository, SampleRepository>();
|
|
|
|
// EN: Register idempotency services / VI: Đăng ký idempotency services
|
|
services.AddScoped<IRequestManager, RequestManager>();
|
|
|
|
return services;
|
|
}
|
|
}
|