proyecto preparado para DDD + CQRS

This commit is contained in:
2025-09-01 23:06:17 +02:00
parent 895e40edc0
commit 09c211a0fe
7 changed files with 92 additions and 12 deletions

View File

@@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace back.Domain;
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
Database.EnsureCreated();
Database.Migrate();
}
}

14
back/Domain/IEntity.cs Normal file
View File

@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace back.Domain;
public interface IEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
string Id
{
get;
set;
}
}

View File

@@ -1,4 +1,8 @@
using Autofac.Extensions.DependencyInjection;
using back.Domain;
using MCVIngenieros.Healthchecks;
using MediatR.Extensions.FluentValidation.AspNetCore;
using Microsoft.EntityFrameworkCore;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
@@ -43,7 +47,15 @@ public class Program
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfiguration(configuration);
builder.Host.UseSerilog();
// Add services to the container.
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
});
builder.Services.AddFluentValidation([typeof(Program).Assembly]);
builder.Services.AddHealthChecksSupport().DiscoverHealthChecks();
builder.Services.AddLogging();
builder.Logging.AddOpenTelemetry(options =>
@@ -63,7 +75,32 @@ public class Program
.AddAspNetCoreInstrumentation()
.AddConsoleExporter());
builder.Services.AddDataProtection();
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy
.WithOrigins(builder.Configuration.GetSection("AllowedHosts").Get<string[]>() ?? [])
.SetIsOriginAllowedToAllowWildcardSubdomains()
.SetPreflightMaxAge(TimeSpan.FromMinutes(10))
.AllowCredentials()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddHttpContextAccessor();
builder.Services.AddAntiforgery(options =>
{
options.HeaderName = "X-XSRF-TOKEN";
});
builder.Services.AddDbContext<ApplicationDbContext>(opts =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
opts.UseSqlite(connectionString);
});
builder.Services.AddControllers(options =>
{
@@ -88,7 +125,6 @@ public class Program
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -4,5 +4,6 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"AllowedHosts": "*"
}

View File

@@ -4,5 +4,6 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"AllowedHosts": "mmorales.photo"
}

View File

@@ -1,9 +1,3 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -7,12 +7,35 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="8.4.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageReference Include="Autofac.WebApi2" Version="6.1.1" />
<PackageReference Include="FluentValidation" Version="12.0.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
<PackageReference Include="MCVIngenieros.Healthchecks" Version="0.0.1" />
<PackageReference Include="MediatR" Version="13.0.0" />
<PackageReference Include="MediatR.Contracts" Version="2.0.1" />
<PackageReference Include="MediatR.Extensions.Autofac.DependencyInjection" Version="13.1.0" />
<PackageReference Include="MediatR.Extensions.FluentValidation.AspNetCore" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Antiforgery" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Api.ProviderBuilderExtensions" Version="1.12.0" />
@@ -41,7 +64,6 @@
<ItemGroup>
<Folder Include="Application\" />
<Folder Include="Infrastructure\" />
<Folder Include="Domain\" />
<Folder Include="Presentation\" />
</ItemGroup>