### Build the Solution with .NET CLI Source: https://github.com/ardalis/cleanarchitecture/blob/main/README.template.md Use this command to compile the entire solution. Ensure you have the .NET SDK installed. ```sh dotnet build ``` -------------------------------- ### Start Hugo Local Development Server Source: https://github.com/ardalis/cleanarchitecture/blob/main/CONTRIBUTING.md Navigate to the 'docs/' directory and start the Hugo development server with the --minify flag. This allows for hot-reloading on file saves. ```bash cd docs hugo server --minify ``` -------------------------------- ### Run with Aspire for Database Setup Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Recommended command to run the application with Aspire, which automatically provisions the SQL Server container and applies migrations. ```powershell dotnet run --project src/MinimalClean.Architecture.AspireHost ``` -------------------------------- ### Run the Application with .NET CLI Source: https://github.com/ardalis/cleanarchitecture/blob/main/README.template.md Execute this command to start the web application. Specify the project path for the web project. ```sh dotnet run --project src/Your.ProjectName.Web ``` -------------------------------- ### Install Minimal Clean Architecture Template Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Installs the Ardalis.MinimalClean.Template package from NuGet. ```powershell dotnet new install Ardalis.MinimalClean.Template ``` -------------------------------- ### Install Full Clean Architecture Template Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Installs the Ardalis.CleanArchitecture.Template package from NuGet. ```powershell dotnet new install Ardalis.CleanArchitecture.Template ``` -------------------------------- ### Switching to SQL Server Connection String Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Example JSON configuration for setting the default connection string to SQL Server LocalDB. ```json { "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=YourDb;Trusted_Connection=true;" } } ``` -------------------------------- ### Clone Hugo Theme Source: https://github.com/ardalis/cleanarchitecture/blob/main/CONTRIBUTING.md Clone the Hugo Book theme alongside the project's docs directory. This mirrors the setup used in the CI environment. ```bash git clone https://github.com/alex-shpak/hugo-book docs/themes/hugo-book ``` -------------------------------- ### Encapsulated Domain Entity Example Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Demonstrates a well-encapsulated domain entity with private state and methods to manage its behavior. Avoid anemic domain models with public setters. ```csharp // Good: Encapsulated entity public class Cart { private readonly List _items = new(); public IReadOnlyCollection Items => _items.AsReadOnly(); public void AddItem(Product product, int quantity) { // Business logic here var existingItem = _items.FirstOrDefault(i => i.ProductId == product.Id); if (existingItem != null) { existingItem.IncreaseQuantity(quantity); } else { _items.Add(new CartItem(product, quantity)); } } } // Avoid: Anemic domain model public class Cart { public List Items { get; set; } = new(); } ``` -------------------------------- ### Build and Run Application Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Commands to build the solution and run the application directly or via Aspire. ```powershell dotnet build dotnet run --project src/MinimalClean.Architecture.Web # Or run with Aspire (if using) dotnet run --project src/MinimalClean.Architecture.AspireHost ``` -------------------------------- ### Create Core Project Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Use this command to create a new class library project for your core domain entities. This is the first step in migrating from a minimal to a full Clean Architecture. ```powershell # Create new Core project dotnet new classlib -n YourProject.Core # Move domain entities mv Domain/* ../YourProject.Core/ # Update namespaces # Update project references ``` -------------------------------- ### Project Structure Overview Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Illustrates the directory structure for a minimal clean architecture project, including Domain, Infrastructure, and Endpoints layers. ```text MinimalClean.Architecture.Web/ ├── Domain/ # Domain Layer │ ├── CartAggregate/ │ │ ├── Cart.cs # Aggregate root │ │ ├── CartItem.cs # Entity │ │ └── Events/ # Domain events (optional) │ ├── OrderAggregate/ │ └── ProductAggregate/ ├── Infrastructure/ # Infrastructure Layer │ ├── Data/ │ │ ├── AppDbContext.cs # EF Core DbContext │ │ ├── Config/ # EF configurations │ │ │ ├── CartConfiguration.cs │ │ │ └── OrderConfiguration.cs │ │ └── Migrations/ # EF migrations │ ├── Email/ # External services │ └── Services/ # Infrastructure services ├── Endpoints/ # Presentation Layer │ ├── Cart/ │ │ ├── Create.cs # Create cart endpoint │ │ ├── AddItem.cs # Add item to cart │ │ └── List.cs # List carts │ ├── Order/ │ └── Product/ └── Program.cs # Application startup ``` -------------------------------- ### Run Tests with .runsettings via Command Line Source: https://github.com/ardalis/cleanarchitecture/blob/main/PARALLEL_TEST_EXECUTION.md Use the 'dotnet test' command with the '--settings' option to specify the .runsettings file for test execution. ```bash dotnet test --settings .runsettings ``` -------------------------------- ### Create Full Clean Architecture Project Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Creates a new solution structure using the 'clean-arch' template in a specified output directory. ```powershell dotnet new clean-arch -o Your.ProjectName ``` -------------------------------- ### Create Minimal Clean Architecture Project Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Creates a new solution structure using the 'min-clean' template in a specified output directory. ```powershell dotnet new min-clean -o Your.ProjectName ``` -------------------------------- ### Project Structure Overview Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Illustrates the single-project, vertical slice organization of the Minimal Clean Architecture template. ```text src/MinimalClean.Architecture.Web/ ├── Domain/ # Domain entities and aggregates │ ├── CartAggregate/ │ ├── OrderAggregate/ │ └── ProductAggregate/ ├── Infrastructure/ # Data access and external services │ ├── Data/ │ │ ├── AppDbContext.cs │ │ ├── Config/ # EF Core configurations │ │ └── Migrations/ │ └── Email/ # Email services ├── Endpoints/ # API endpoints (FastEndpoints) │ ├── Cart/ │ ├── Order/ │ └── Product/ └── Program.cs # Application startup ``` -------------------------------- ### Update Database with Migrations Source: https://github.com/ardalis/cleanarchitecture/blob/main/src/Clean.Architecture.AspireHost/README.md Execute this command from the Web project directory to apply pending database migrations. Specify the context, infrastructure project path, and web project path. ```bash dotnet ef database update -c AppDbContext -p ../Clean.Architecture.Infrastructure/Clean.Architecture.Infrastructure.csproj -s Clean.Architecture.Web.csproj ``` -------------------------------- ### Create Infrastructure Project Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Create a new class library for infrastructure concerns and add a project reference to the Core project. This separates infrastructure code from the domain. ```powershell # Create Infrastructure project dotnet new classlib -n YourProject.Infrastructure # Move infrastructure code mv Infrastructure/* ../YourProject.Infrastructure/ # Add reference to Core dotnet add YourProject.Infrastructure reference YourProject.Core ``` -------------------------------- ### Running Tests Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Command to execute all unit and integration tests in the solution. ```powershell dotnet test ``` -------------------------------- ### Add Controller Support to Program.cs Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Configure your application to support controllers by adding services and mapping endpoints in Program.cs. ```csharp builder.Services.AddControllers(); // ControllersWithView if you need Views // and app.MapControllers(); ``` -------------------------------- ### Add Migration using .NET CLI Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Run this command from the Web project directory in a terminal to add a new migration using the .NET CLI. Adjust 'MIGRATIONNAME' and project paths as needed. ```powershell dotnet ef migrations add MIGRATIONNAME -c AppDbContext -p ../Your.ProjectName.Infrastructure/Your.ProjectName.Infrastructure.csproj -s Your.ProjectName.Web.csproj -o Data/Migrations ``` -------------------------------- ### Create UseCases Project Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Optionally, create a project for use cases to encapsulate business logic. This step involves organizing command and query handlers, potentially using a library like Mediator. ```powershell # Create UseCases project dotnet new classlib -n YourProject.UseCases # Move business logic from endpoints to use cases # Add Mediator (if not already using) # Create command/query handlers # Leverage Mediator Behaviors for cross-cutting concerns ``` -------------------------------- ### Focused Entity Framework Configuration Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Illustrates a focused Entity Framework configuration for the Cart entity. This approach keeps persistence logic separate and organized. ```csharp // Good: Focused EF configuration public class CartConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.HasKey(c => c.Id); builder.HasMany(c => c.Items) .WithOne() .HasForeignKey("CartId"); } } ``` -------------------------------- ### Add New Database Migration Source: https://github.com/ardalis/cleanarchitecture/blob/main/src/Clean.Architecture.AspireHost/README.md Use this command from the Web project directory to add a new database migration. Ensure the context, project paths, and output directory are correctly specified. ```bash dotnet ef migrations add MigrationName -c AppDbContext -p ../Clean.Architecture.Infrastructure/Clean.Architecture.Infrastructure.csproj -s Clean.Architecture.Web.csproj -o Data/Migrations ``` -------------------------------- ### Direct Web Project Run with LocalDB Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Commands to update the database schema using EF Core and then run the Web project directly, suitable for LocalDB. ```powershell dotnet ef database update -c AppDbContext -p src/MinimalClean.Architecture.Web -s src/MinimalClean.Architecture.Web dotnet run --project src/MinimalClean.Architecture.Web ``` -------------------------------- ### Add Testcontainers Package References Source: https://github.com/ardalis/cleanarchitecture/blob/main/TESTCONTAINERS_IMPLEMENTATION.md Add these package references to your Directory.Packages.props file to include Testcontainers and its MsSql module. ```xml ``` -------------------------------- ### Configure SQL Server Container with MsSqlBuilder Source: https://github.com/ardalis/cleanarchitecture/blob/main/TESTCONTAINERS_IMPLEMENTATION.md Instantiate a SQL Server container using MsSqlBuilder, specifying the image and password. This container will be managed by Testcontainers for testing purposes. ```csharp private readonly MsSqlContainer _dbContainer = new MsSqlBuilder() .WithImage("mcr.microsoft.com/mssql/server:2022-latest") .WithPassword("Your_password123!") .Build(); ``` -------------------------------- ### Update Database using .NET CLI Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Execute this command from the Web project folder to update the database to the latest migration. Replace 'Clean.Architecture' with your project's actual name. ```powershell dotnet ef database update -c AppDbContext -p ../Clean.Architecture.Infrastructure/Clean.Architecture.Infrastructure.csproj -s Clean.Architecture.Web.csproj ``` -------------------------------- ### Merge Projects for Simplification Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md When migrating from a full Clean Architecture to a minimal one, this step involves consolidating code into a single web project and organizing it by feature. ```powershell # Copy all code into Web project # Organize by vertical slices ``` -------------------------------- ### Run Functional Tests Source: https://github.com/ardalis/cleanarchitecture/blob/main/TESTCONTAINERS_IMPLEMENTATION.md Execute functional tests using the dotnet CLI. This command targets the specific project file for functional tests. ```bash dotnet test tests\Clean.Architecture.FunctionalTests\Clean.Architecture.FunctionalTests.csproj ``` -------------------------------- ### Key Design Decisions Source: https://github.com/ardalis/cleanarchitecture/blob/main/MinimalClean/README.template.md Highlights the core architectural choices made in this template. ```text Single project vertical slice architecture ``` -------------------------------- ### Add Migration in Package Manager Console Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Use this command in Visual Studio's Package Manager Console to add a new migration. Replace 'InitialMigrationName' and 'Your.ProjectName' with your specific values. ```powershell Add-Migration InitialMigrationName -StartupProject Your.ProjectName.Web -Context AppDbContext -Project Your.ProjectName.Infrastructure ``` -------------------------------- ### Add Razor Pages Support to Program.cs Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Configure your application to support Razor Pages by adding services and mapping endpoints in Program.cs. ```csharp builder.Services.AddRazorPages(); // and app.MapRazorPages(); ``` -------------------------------- ### Focused Endpoint Implementation Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Shows a clear and focused endpoint definition for creating a cart. This pattern promotes separation of concerns within the endpoint layer. ```csharp // Good: Clear, focused endpoint public class CreateCart : EndpointWithoutRequest { private readonly AppDbContext _db; public CreateCart(AppDbContext db) => _db = db; public override void Configure() { Post("/carts"); AllowAnonymous(); } public override async Task HandleAsync(CancellationToken ct) { var cart = new Cart(guestUserId: Guid.NewGuid()); _db.Carts.Add(cart); await _db.SaveChangesAsync(ct); await Send.Async(new CartResponse(cart.Id, cart.Items.Count), cancellation: ct); } } ``` -------------------------------- ### Dependency Flow Diagram Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Visualizes the dependency flow between the Endpoints, Infrastructure, and Domain layers in the architecture. ```text Endpoints ──→ Domain ↓ Infrastructure ──→ Domain ``` -------------------------------- ### Add Ardalis.ApiEndpoints Package Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/getting-started.md Use this command to add the Ardalis.ApiEndpoints NuGet package to your project. ```powershell dotnet add package Ardalis.ApiEndpoints ``` -------------------------------- ### Unit Test for Adding Item to Cart Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Tests the domain logic of adding a new product to a shopping cart. Ensure this test is focused solely on the Cart class's behavior. ```csharp public class CartTests { [Fact] public void AddItem_NewProduct_AddsToCart() { // Arrange var cart = new Cart(guestUserId: Guid.NewGuid()); var product = new Product("Test", 10m); // Act cart.AddItem(product, 2); // Assert Assert.Single(cart.Items); Assert.Equal(2, cart.Items.First().Quantity); } } ``` -------------------------------- ### Functional Test for Creating a Cart Source: https://github.com/ardalis/cleanarchitecture/blob/main/docs/content/minimal-clean-architecture.md Tests the end-to-end functionality of creating a new cart via an API endpoint. This test uses WebApplicationFactory to simulate HTTP requests. ```csharp public class CartEndpointsTests : IClassFixture> { [Fact] public async Task CreateCart_ReturnsNewCart() { // Arrange var client = _factory.CreateClient(); // Act var response = await client.PostAsync("/carts", null); // Assert response.EnsureSuccessStatusCode(); var cart = await response.Content.ReadFromJsonAsync(); Assert.NotNull(cart); } } ``` -------------------------------- ### Update EF Core Database Source: https://github.com/ardalis/cleanarchitecture/blob/main/README.template.md Apply pending database migrations using EF Core tools. Requires specifying the DbContext, Infrastructure project, and Web project. ```sh dotnet ef database update -c AppDbContext -p src/Your.ProjectName.Infrastructure/Your.ProjectName.Infrastructure.csproj -s src/Your.ProjectName.Web/Your.ProjectName.Web.csproj ``` -------------------------------- ### Markdown Front Matter for Hugo Pages Source: https://github.com/ardalis/cleanarchitecture/blob/main/CONTRIBUTING.md Use this YAML front matter block at the top of Markdown files to define page metadata for Hugo. ```markdown --- title: "My New Page" weight: 25 --- Page content here. ``` -------------------------------- ### Disable Parallel Execution in xUnit Source: https://github.com/ardalis/cleanarchitecture/blob/main/PARALLEL_TEST_EXECUTION.md To disable parallel execution for a specific test project, update its xunit.runner.json file by setting 'parallelizeAssembly' and 'parallelizeTestCollections' to false. ```json { "shadowCopy": false, "parallelizeAssembly": false, "parallelizeTestCollections": false } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.