Đây là bài 31 của loạt C# và .NET 10 qua Tới Nơi. Bài 31–38 cùng giải thích code cuối của service tại examples/toi-noi-dotnet/31-38-trip-service, nhắm tới net10.0.
API gọi repository ở runtime có nghĩa Domain phải tham chiếu project Infrastructure không?
Xem đáp án
Không. Lõi định nghĩa abstraction; Infrastructure triển khai nó và composition root ghép các implementation.
Từ các thí nghiệm sang một service
Ba mươi bài đầu đã kiểm tra từng cơ chế. Tám bài cuối cùng dùng chung service Trips để thấy chúng phối hợp: khách tạo chuyến, đọc chuyến của mình; tài xế nhận một chuyến còn trống; dữ liệu và Outbox được commit trên PostgreSQL.
Không thêm tám bản sao công thức khác nhau. Thư mục 31-38-trip-service là code cuối của cùng một lab, mỗi bài giải thích một lát cắt. Service này chưa phải là toàn bộ sản phẩm Tới Nơi: chưa có điều phối thực, thu tiền, fraud/OCR hoặc broker production.
Clean Architecture ở đây phục vụ ranh giới thay đổi và kiểm thử. Bốn project trông gọn không tự chứng minh kiến trúc đó là clean; điều cần xem là ai sở hữu rule, ai tham chiếu ai và chi tiết hạ tầng có rò vào mô hình hay không.
Bốn tầng làm những việc gì?
Domain giữ Trip, Money, DistanceKm, invariant và domain event; không biết EF, HTTP hay DI container. Application mô tả command/query, handler, validator và port cần dùng. Infrastructure triển khai repository, UoW, query projection và dispatcher. API bind HTTP, xác thực, phân quyền và đóng vai trò composition root.
Application tham chiếu Domain. Infrastructure tham chiếu Application và dùng các kiểu Domain. API tham chiếu các lớp cần ghép vào. Không có ProjectReference từ Domain/Application sang Infrastructure; architecture test còn kiểm tra các assembly được tham chiếu để phát hiện EF/ASP.NET lọt vào lõi.
Chiều gọi runtime có thể đi từ handler tới lớp triển khai repository. Chiều phụ thuộc trong mã nguồn vẫn hướng vào interface do Application sở hữu. Đừng vẽ hai chiều đó thành một mũi tên duy nhất rồi kết luận dependency inversion đã bị vi phạm.
Domain khác với persistence row
TripRow có setter để EF nạp dữ liệu vào đối tượng (materialize); Trip aggregate chỉ đổi qua hành vi. Adapter rehydrate Trip bằng Restore, kiểm tra trạng thái đã lưu có hợp lệ và không phát một sự kiện tạo chuyến mới mỗi lần đọc.
Không dùng reflection để tự sửa private state của aggregate. Một đường Restore tường minh giúp thấy rõ invariant và sự khác biệt giữa tạo mới với khôi phục. Restore không có nghĩa là tin mọi dữ liệu từ API: dữ liệu ngoài phải qua binding/validation, còn dữ liệu đã lưu bị hỏng cần hiện ra như một lỗi hệ thống.
Không nhất thiết mọi ứng dụng đều cần hai mô hình riêng. Trong lab, chi phí mapping được chấp nhận để giữ domain thuần và làm rõ bài học. Khi đánh giá một cách ánh xạ khác, hãy nói cả lợi ích và cái giá thay vì chỉ đếm số class.
Composition root là nơi biết implementation
Program đăng ký repository concrete với vòng đời scoped một lần, rồi ánh xạ ITripRepository về cùng instance đó; EfUnitOfWork dùng chính repository đó để gom các aggregate đang tham gia use case. Nếu đăng ký hai instance repository khác nhau cho hai interface, UoW có thể không thấy thay đổi domain cần flush.
DbContext là scoped theo request. TimeProvider là bản dùng chung và được thay bằng FixedClock trong test. Domain không gọi IServiceProvider; việc tìm handler nằm ở dispatcher thuộc hạ tầng, với phạm vi được giới hạn rõ ràng, không biến service locator thành thói quen trong mọi lớp.
Cấu hình storage và auth được bind và validate khi host khởi động. API không tự migrate; readiness kiểm tra kết nối và migration đang chờ. Liveness không query DB để lỗi database không dẫn đến vòng restart của chính process.
Bài tập và bằng chứng
Bài tập: chuyển một lời gọi UseNpgsql vào Domain có làm test giá tiền sai ngay không? Nếu không, tại sao architecture test vẫn đáng giữ?
Xem đáp án
Test số tiền có thể vẫn đạt, nhưng dependency boundary đã đổi. Architecture test bảo vệ một thuộc tính khác: lõi không phụ thuộc ORM/web/hạ tầng, giúp ta test và thay đổi từng chi tiết một cách độc lập.
Phạm vi được kiểm tra ở đây là bốn tầng của service Trips. Không suy ra một repository hay một database có thể dùng chung cho mọi service Python/Rust/Go trong Tới Nơi.
Code và kiểm chứng
Thiết lập TOI_NOI_PG cho database local riêng toi_noi_dotnet_lab theo README bài 21, rồi từ gốc repository:
dotnet test examples/toi-noi-dotnet/31-38-trip-service/Tests
Fixture dùng schema GUID riêng, không xoá database; JWT dùng khoá ngẫu nhiên trong test và vẫn đi qua handler xác thực thật. Test không tự bỏ qua khi thiếu PostgreSQL. Về đặc tả Docker/CI/cloud, xem README của service và cloud/README.md để biết phạm vi, điều kiện tiên quyết và phần chưa được triển khai thật.
Contracts.cs — 31-38-trip-service/Application/Contracts.cs
using ToiNoi.Trips.Domain;
namespace ToiNoi.Trips.Application;
public interface IRequest<TResponse> { }
public delegate Task<TResponse> RequestNext<TResponse>();
public interface IRequestHandler<TRequest, TResponse> where TRequest : IRequest<TResponse>
{ Task<TResponse> HandleAsync(TRequest request, CancellationToken ct); }
public interface IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{ Task<TResponse> HandleAsync(TRequest request, RequestNext<TResponse> next, CancellationToken ct); }
public interface IRequestValidator<TRequest> { void Validate(TRequest request); }
public interface IRequestDispatcher
{ Task<TResponse> SendAsync<TRequest, TResponse>(TRequest request, CancellationToken ct) where TRequest : IRequest<TResponse>; }
public interface ITripRepository
{
Task<Trip?> GetAsync(Guid id, CancellationToken ct);
void Add(Trip trip);
}
public interface IUnitOfWork { Task CommitAsync(CancellationToken ct); }
public sealed record BookingReceipt(Guid CustomerId, string Key, decimal DistanceKm, Guid TripId, decimal Fare);
public interface IBookingRequests
{
Task<BookingReceipt?> FindAsync(Guid customerId, string key, CancellationToken ct);
void Add(BookingReceipt receipt);
}
public sealed record TripView(Guid Id, Guid CustomerId, decimal Fare, string Currency, string Status, Guid? DriverId, int Version);
public interface ITripQueries { Task<TripView?> FindOwnedAsync(Guid tripId, Guid customerId, CancellationToken ct); }
public sealed class ValidationFailureException(string field, string error) : Exception(error)
{ public string Field { get; } = field; }
public sealed class NotFoundException() : Exception("Trip not found.");
public sealed class ConflictException(string message) : Exception(message);
public sealed class DuplicateRequestException(Exception inner) : Exception("Request key already committed.", inner);
public sealed class ConcurrencyConflictException(Exception inner) : Exception("Trip changed concurrently.", inner);
ServiceTests.cs — 31-38-trip-service/Tests/ServiceTests.cs
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ToiNoi.Trips.Application;
using ToiNoi.Trips.Domain;
using ToiNoi.Trips.Infrastructure;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace ToiNoi.Trips.Tests;
public sealed class ServiceTests : IAsyncLifetime
{
private ServiceLab lab = null!;
private ApiFactory factory = null!;
private HttpClient customer = null!;
private readonly Guid owner = Guid.NewGuid();
public async Task InitializeAsync()
{
lab = await ServiceLab.CreateAsync(); factory = new ApiFactory(lab.Connection);
customer = factory.Client(owner, "customer");
}
public async Task DisposeAsync() { customer?.Dispose(); factory?.Dispose(); if (lab is not null) await lab.DisposeAsync(); }
private static async Task<HttpResponseMessage> CreateAsync(HttpClient client, string key, decimal? distance)
{
using var request = new HttpRequestMessage(HttpMethod.Post, "/trips") { Content = JsonContent.Create(new { distanceKm = distance }) };
request.Headers.Add("Idempotency-Key", key); return await client.SendAsync(request);
}
[Fact]
public async Task Create_commits_trip_request_and_outbox_with_one_result()
{
using var response = await CreateAsync(customer, "create-1", 5m);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
var result = (await response.Content.ReadFromJsonAsync<CreateTripResult>())!;
Assert.Equal(25500m, result.Fare); Assert.Equal("VND", result.Currency);
Assert.Equal($"/trips/{result.TripId}", response.Headers.Location?.ToString());
await using var db = lab.Open();
var row = await db.Trips.SingleAsync(); Assert.Equal(owner, row.CustomerId); Assert.Equal(DateTimeOffset.UnixEpoch, row.CreatedAt);
Assert.Equal(1, await db.Requests.CountAsync()); var message = await db.Outbox.SingleAsync();
Assert.Equal("trip.requested.v1", message.Type); Assert.Equal(row.Id, message.TripId);
}
[Theory]
[InlineData(null)]
[InlineData("-0.001")]
[InlineData("100.001")]
[InlineData("2.0000001")]
public async Task Invalid_distance_is_400_and_creates_no_rows(string? input)
{
decimal? distance = input is null ? null : decimal.Parse(input, System.Globalization.CultureInfo.InvariantCulture);
using var response = await CreateAsync(customer, "invalid-1", distance);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
var problem = await response.Content.ReadFromJsonAsync<JsonElement>(); Assert.True(problem.GetProperty("errors").TryGetProperty("distanceKm", out _));
await using var db = lab.Open(); Assert.Equal(0, await db.Trips.CountAsync()); Assert.Equal(0, await db.Outbox.CountAsync());
}
[Theory]
[InlineData("")]
[InlineData("with space")]
public async Task Invalid_key_is_400(string key)
{
using var response = await CreateAsync(customer, key, 5m); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Fact]
public async Task Concurrent_duplicate_and_replay_return_same_committed_trip()
{
var responses = await Task.WhenAll(CreateAsync(customer, "repeat-1", 5m), CreateAsync(customer, "repeat-1", 5m));
try
{
Assert.All(responses, r => Assert.Equal(HttpStatusCode.Created, r.StatusCode));
var first = (await responses[0].Content.ReadFromJsonAsync<CreateTripResult>())!;
var second = (await responses[1].Content.ReadFromJsonAsync<CreateTripResult>())!; Assert.Equal(first, second);
using var replay = await CreateAsync(customer, "repeat-1", 5m);
Assert.Equal(first, await replay.Content.ReadFromJsonAsync<CreateTripResult>());
await using var db = lab.Open(); Assert.Equal(1, await db.Trips.CountAsync()); Assert.Equal(1, await db.Requests.CountAsync()); Assert.Equal(1, await db.Outbox.CountAsync());
}
finally { foreach (var response in responses) response.Dispose(); }
}
[Fact]
public async Task Same_key_different_input_is_conflict()
{
using var first = await CreateAsync(customer, "conflict-1", 5m);
using var second = await CreateAsync(customer, "conflict-1", 6m); Assert.Equal(HttpStatusCode.Conflict, second.StatusCode);
await using var db = lab.Open(); Assert.Equal(1, await db.Trips.CountAsync());
}
[Fact]
public async Task Ownership_is_from_verified_subject_and_wrong_owner_sees_404()
{
using var created = await CreateAsync(customer, "owned-1", 5m); var trip = (await created.Content.ReadFromJsonAsync<CreateTripResult>())!;
using var own = await customer.GetAsync($"/trips/{trip.TripId}"); Assert.Equal(HttpStatusCode.OK, own.StatusCode);
using var other = factory.Client(Guid.NewGuid(), "customer"); using var forbidden = await other.GetAsync($"/trips/{trip.TripId}");
Assert.Equal(HttpStatusCode.NotFound, forbidden.StatusCode);
}
[Fact]
public async Task Unauthenticated_and_wrong_role_are_rejected()
{
using var anonymous = factory.CreateClient(); using var a = await anonymous.GetAsync($"/trips/{Guid.NewGuid()}"); Assert.Equal(HttpStatusCode.Unauthorized, a.StatusCode);
using var driver = factory.Client(Guid.NewGuid(), "driver"); using var b = await CreateAsync(driver, "role-1", 5m); Assert.Equal(HttpStatusCode.Forbidden, b.StatusCode);
}
[Fact]
public async Task Two_drivers_have_one_winner_and_one_accept_event()
{
using var created = await CreateAsync(customer, "accept-1", 5m); var trip = (await created.Content.ReadFromJsonAsync<CreateTripResult>())!;
using var a = factory.Client(Guid.NewGuid(), "driver"); using var b = factory.Client(Guid.NewGuid(), "driver");
var responses = await Task.WhenAll(a.PostAsJsonAsync($"/trips/{trip.TripId}/accept", new { expectedVersion = 0 }), b.PostAsJsonAsync($"/trips/{trip.TripId}/accept", new { expectedVersion = 0 }));
try
{
Assert.Single(responses, r => r.StatusCode == HttpStatusCode.OK); Assert.Single(responses, r => r.StatusCode == HttpStatusCode.Conflict);
await using var db = lab.Open(); var row = await db.Trips.SingleAsync(); Assert.Equal("Accepted", row.Status); Assert.Equal(1, row.Version); Assert.NotNull(row.DriverId);
Assert.Equal(2, await db.Outbox.CountAsync()); Assert.Equal(1, await db.Outbox.CountAsync(x => x.Type == "trip.accepted.v1"));
}
finally { foreach (var response in responses) response.Dispose(); }
}
[Fact]
public async Task Rehydration_has_no_new_creation_events()
{
using var created = await CreateAsync(customer, "restore-1", 5m); var trip = (await created.Content.ReadFromJsonAsync<CreateTripResult>())!;
await using var scope = factory.Services.CreateAsyncScope(); var repository = scope.ServiceProvider.GetRequiredService<ITripRepository>();
var a = (await repository.GetAsync(trip.TripId, CancellationToken.None))!; var b = await repository.GetAsync(trip.TripId, CancellationToken.None);
Assert.Same(a, b); Assert.Empty(a.PendingEvents);
}
[Fact]
public async Task Precancelled_command_never_writes()
{
await using var scope = factory.Services.CreateAsyncScope(); var dispatcher = scope.ServiceProvider.GetRequiredService<IRequestDispatcher>();
using var cts = new CancellationTokenSource(); cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => dispatcher.SendAsync<CreateTrip, CreateTripResult>(new(owner, "cancel-1", 5m), cts.Token));
await using var db = lab.Open(); Assert.Equal(0, await db.Trips.CountAsync());
}
[Fact]
public async Task Readiness_is_healthy_after_migration()
{
using var response = await customer.GetAsync("/health/ready"); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
public sealed class HealthTests
{
[Fact]
public async Task Live_can_be_healthy_while_schema_is_not_ready_and_startup_does_not_migrate()
{
await using var lab = await ServiceLab.CreateAsync(migrate: false); using var factory = new ApiFactory(lab.Connection); using var client = factory.CreateClient();
using var live = await client.GetAsync("/health/live"); using var ready = await client.GetAsync("/health/ready");
Assert.Equal(HttpStatusCode.OK, live.StatusCode); Assert.Equal(HttpStatusCode.ServiceUnavailable, ready.StatusCode);
}
}
public sealed class ArchitectureTests
{
[Fact]
public void Domain_and_application_do_not_reference_orm_web_or_infrastructure()
{
foreach (var assembly in new[] { typeof(Trip).Assembly, typeof(CreateTrip).Assembly })
Assert.DoesNotContain(assembly.GetReferencedAssemblies(), reference => reference.Name!.Contains("EntityFrameworkCore") || reference.Name.Contains("AspNetCore") || reference.Name == "Infrastructure");
}
[Fact]
public void Restore_rejects_invalid_state_and_does_not_raise_events()
{
Assert.Throws<ArgumentException>(() => Trip.Restore(Guid.NewGuid(), Guid.NewGuid(), new DistanceKm(5m), new Money(25500m), DateTimeOffset.UnixEpoch, TripStatus.Accepted, null, 0));
var restored = Trip.Restore(Guid.NewGuid(), Guid.NewGuid(), new DistanceKm(5m), new Money(25500m), DateTimeOffset.UnixEpoch, TripStatus.Requested, null, 0);
Assert.Empty(restored.PendingEvents);
}
}
Bài trước: bài 30. Bài tiếp: bài 32.



Bình luận (…)