diff --git a/VerticalSliceArchitectureTemplate.sln b/VerticalSliceArchitectureTemplate.sln index 9616507..7854d9d 100644 --- a/VerticalSliceArchitectureTemplate.sln +++ b/VerticalSliceArchitectureTemplate.sln @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{21C89A08 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VerticalSliceArchitectureTemplate.Unit.Tests", "tests\VerticalSliceArchitectureTemplate.Unit.Tests\VerticalSliceArchitectureTemplate.Unit.Tests.csproj", "{07A76FBE-083C-49AA-856E-0C1B95DEB7B2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VerticalSliceArchitectureTemplate.AppHost", "src\VerticalSliceArchitectureTemplate.AppHost\VerticalSliceArchitectureTemplate.AppHost.csproj", "{4749B813-0C0B-4596-9008-9369B4CD027C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,9 +32,14 @@ Global {07A76FBE-083C-49AA-856E-0C1B95DEB7B2}.Debug|Any CPU.Build.0 = Debug|Any CPU {07A76FBE-083C-49AA-856E-0C1B95DEB7B2}.Release|Any CPU.ActiveCfg = Release|Any CPU {07A76FBE-083C-49AA-856E-0C1B95DEB7B2}.Release|Any CPU.Build.0 = Release|Any CPU + {4749B813-0C0B-4596-9008-9369B4CD027C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4749B813-0C0B-4596-9008-9369B4CD027C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4749B813-0C0B-4596-9008-9369B4CD027C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4749B813-0C0B-4596-9008-9369B4CD027C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {8EB413BD-C5DF-49A5-A372-4AEAF4999952} = {92C5999C-A447-479E-8630-064E6FDC78DA} {07A76FBE-083C-49AA-856E-0C1B95DEB7B2} = {21C89A08-D942-45BE-A302-4EEB543573C0} + {4749B813-0C0B-4596-9008-9369B4CD027C} = {92C5999C-A447-479E-8630-064E6FDC78DA} EndGlobalSection EndGlobal diff --git a/src/VerticalSliceArchitectureTemplate.AppHost/Program.cs b/src/VerticalSliceArchitectureTemplate.AppHost/Program.cs new file mode 100644 index 0000000..b11db3d --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate.AppHost/Program.cs @@ -0,0 +1,10 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var appDb = builder + .AddSqlServer("db") + .AddDatabase("appdb"); + +builder.AddProject("webapi") + .WithReference(appDb); + +builder.Build().Run(); diff --git a/src/VerticalSliceArchitectureTemplate.AppHost/Properties/launchSettings.json b/src/VerticalSliceArchitectureTemplate.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..928b2ee --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate.AppHost/Properties/launchSettings.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15230", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16214" + } + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate.AppHost/VerticalSliceArchitectureTemplate.AppHost.csproj b/src/VerticalSliceArchitectureTemplate.AppHost/VerticalSliceArchitectureTemplate.AppHost.csproj new file mode 100644 index 0000000..df3a962 --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate.AppHost/VerticalSliceArchitectureTemplate.AppHost.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + enable + enable + true + + + + + + + + + + + diff --git a/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.Development.json b/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.json b/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate/Common/Persistence/DependencyInjection.cs b/src/VerticalSliceArchitectureTemplate/Common/Persistence/DependencyInjection.cs index 4892b9a..9145776 100644 --- a/src/VerticalSliceArchitectureTemplate/Common/Persistence/DependencyInjection.cs +++ b/src/VerticalSliceArchitectureTemplate/Common/Persistence/DependencyInjection.cs @@ -7,12 +7,7 @@ public static class DependencyInjection public static void AddEfCore(this IServiceCollection services) { services.AddScoped(); - - services.AddDbContext((sp, options) => - { - options.UseInMemoryDatabase("InMemoryDbForTesting"); - - options.AddInterceptors(sp.GetServices()); - }); + + services.AddSqlServerDbContext("appdb"); } } \ No newline at end of file diff --git a/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.Designer.cs b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.Designer.cs new file mode 100644 index 0000000..827afed --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.Designer.cs @@ -0,0 +1,49 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VerticalSliceArchitectureTemplate.Common.Persistence; + +#nullable disable + +namespace VerticalSliceArchitectureTemplate.Common.Persistence.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20240321064028_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("VerticalSliceArchitectureTemplate.Features.Todos.Models.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Text") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("nvarchar(1024)"); + + b.HasKey("Id"); + + b.ToTable("Todos"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.cs b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.cs new file mode 100644 index 0000000..2759aa3 --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/20240321064028_Initial.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace VerticalSliceArchitectureTemplate.Common.Persistence.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Todos", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + Text = table.Column(type: "nvarchar(1024)", maxLength: 1024, nullable: false), + IsCompleted = table.Column(type: "bit", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Todos", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Todos"); + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/AppDbContextModelSnapshot.cs b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..dfc6a0d --- /dev/null +++ b/src/VerticalSliceArchitectureTemplate/Common/Persistence/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,46 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VerticalSliceArchitectureTemplate.Common.Persistence; + +#nullable disable + +namespace VerticalSliceArchitectureTemplate.Common.Persistence.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("VerticalSliceArchitectureTemplate.Features.Todos.Models.Todo", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("IsCompleted") + .HasColumnType("bit"); + + b.Property("Text") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("nvarchar(1024)"); + + b.HasKey("Id"); + + b.ToTable("Todos"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VerticalSliceArchitectureTemplate/Program.cs b/src/VerticalSliceArchitectureTemplate/Program.cs index a24791d..3cbc440 100644 --- a/src/VerticalSliceArchitectureTemplate/Program.cs +++ b/src/VerticalSliceArchitectureTemplate/Program.cs @@ -18,6 +18,15 @@ var app = builder.Build(); +if (app.Environment.IsDevelopment()) +{ + using var scope = app.Services.CreateScope(); + var dbContext = scope.ServiceProvider.GetRequiredService(); + + await dbContext.Database.EnsureCreatedAsync(); + await dbContext.Database.MigrateAsync(); +} + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/src/VerticalSliceArchitectureTemplate/VerticalSliceArchitectureTemplate.csproj b/src/VerticalSliceArchitectureTemplate/VerticalSliceArchitectureTemplate.csproj index d6966ae..64add53 100644 --- a/src/VerticalSliceArchitectureTemplate/VerticalSliceArchitectureTemplate.csproj +++ b/src/VerticalSliceArchitectureTemplate/VerticalSliceArchitectureTemplate.csproj @@ -7,10 +7,15 @@ + - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + +