Introduction
In the previous lesson, we saw that microservices are not simply a matter of splitting one application into smaller folders or projects. This lesson focuses on one of the most important design decisions: where should service boundaries be drawn? A poorly chosen boundary produces several small applications that still depend on one another, share database tables, and must be deployed together.
Our sample application will contain three business areas: Catalog, Order, and Inventory. Catalog manages products and pricing information. Order manages the order lifecycle. Inventory manages stock quantities and reservable products. This separation is not merely a technical folder structure; it is a business-driven modeling decision.
By the end of this lesson, you will be able to explain what each service knows, what it may change, which data it owns, and how it should be placed independently within the solution.
1. How to define a service boundary
When defining a service boundary, the first question should not be “Which service owns this table?” but rather “Who is responsible for this business rule?” The same concept may have different meanings in different business contexts. For example, a Product in Catalog may represent a name, description, and selling price, while the corresponding concept in Inventory may focus on a stock code, warehouse location, and available quantity.
A useful service boundary usually has the following characteristics:
- It has a clear and consistent business responsibility.
- Most of its business rules are enforced inside the service.
- Most changes to its data happen within the service.
- It does not require direct access to another service's database tables.
- It can be tested and deployed independently.
These are not absolute laws. However, if a service must make a synchronous call to another service for every operation, or if several services update the same table, the boundaries should be reconsidered.
The Catalog service
Catalog owns the product catalog. Product name, description, category relationships, selling price, and publication status belong to this service's model. Catalog does not change stock quantities as if they were its own data. If a product detail page needs stock information, it can call the Inventory API or later use a read model populated through messaging.
The Order service
Order manages cart-to-order conversion, order states, order lines, and total calculation rules. When an order is created, the service may read a product name or price from Catalog. However, the historical price of the created order should be stored in the order line owned by Order. This prevents a later Catalog price change from altering the total of an existing order.
The Inventory service
Inventory is responsible for stock quantities, reservations, and releasing reserved stock. Order should not update inventory tables directly. Instead, it should request a reservation through an Inventory API or a messaging contract. If the reservation succeeds, the order workflow can continue. If it fails, the result should be translated into an appropriate business outcome for the user.
2. Data ownership and independence
Data ownership in microservices does not necessarily mean that every service must have a separate database server. The essential principle is that one service owns the data and the business rules applied to that data. During local development, Catalog, Order, and Inventory might use the same SQL Server instance, but their tables and access boundaries should remain explicit. In production, the data should be movable to separate databases or different storage technologies.
The following structure is problematic:
OrderService -> direct SELECT/UPDATE on CatalogDb.Products
InventoryService -> direct SELECT/UPDATE on CatalogDb.Products
CatalogService -> CatalogDb.ProductsAlthough Catalog appears to own products, the other services now depend on its table structure. Renaming a column or splitting a table requires all services to be updated together.
A healthier approach is:
OrderService -> Catalog API or ProductSnapshot message
OrderService -> Inventory API or StockReserved message
CatalogService -> only its product data
InventoryService -> only its stock dataEach service may use its own database model. A shared ProductId value can be used, but that does not mean every service should share one Product class.
3. Limiting shared code
Shared code appears attractive because it reduces duplication. In microservices, however, excessive sharing reduces independence. Moving domain entities, EF Core DbContext classes, and service-specific DTOs into a common project is especially risky.
The following types of content may reasonably be shared:
- Common compiler settings and code analyzers.
- Aspire Service Defaults for observability and health-check infrastructure.
- Technical utilities that are genuinely common.
- Explicitly designed and versioned integration contracts.
The Aspire Service Defaults project provides shared service infrastructure such as OpenTelemetry, health checks, service discovery, and HTTP client configuration. Catalog or Order domain models should not be added to that project. Putting every entity into a generic “Shared” project forces different bounded contexts to depend on the same model.
4. Recommended solution structure
As a starting point, the solution can be organized as follows:
DistributedShop.sln
src/
DistributedShop.AppHost/
AppHost.cs
DistributedShop.ServiceDefaults/
Extensions.cs
CatalogService/
CatalogService.csproj
Program.cs
Domain/
Application/
Infrastructure/
Endpoints/
OrderService/
OrderService.csproj
Program.cs
Domain/
Application/
Infrastructure/
Endpoints/
InventoryService/
InventoryService.csproj
Program.cs
Domain/
Application/
Infrastructure/
Endpoints/
Contracts/
Catalog.Contracts/
Inventory.Contracts/Each service is a separate ASP.NET Core Web API project. Each one has its own Program.cs, dependency registrations, domain model, data access layer, and endpoints. The Contracts directory contains only deliberately designed contracts for service-to-service communication. Domain entities and database models do not belong there.
The role of AppHost
AppHost does not own business rules. It does not calculate Catalog prices or manage Order state. Its role is to define the application model, start services, connect resources, and manage the local development environment.
var builder = DistributedApplication.CreateBuilder(args);
var catalog = builder.AddProject<Projects.CatalogService>("catalog");
var order = builder.AddProject<Projects.OrderService>("order")
.WithReference(catalog);
var inventory = builder.AddProject<Projects.InventoryService>("inventory");
order.WithReference(inventory);
builder.Build().Run();In this example, the order service references Catalog and Inventory. These references allow the service to receive the relevant connection and service-discovery configuration. The business rules remain inside the service projects.
In a real project, add WithReference relationships only where they are needed. Connecting every service to every other service creates an invisible dependency graph and expands service-discovery configuration unnecessarily.
5. API boundaries and contracts
Services should expose explicit contracts rather than their internal classes. A possible read contract for Catalog is:
public sealed record ProductSummary(
Guid Id,
string Name,
decimal Price,
bool IsPublished);This contract represents what Catalog chooses to expose. Internal properties such as cost price, supplier notes, or administrative fields do not need to be included.
On the Order side, the product name and price can be stored as a snapshot when an order line is created:
public sealed class OrderLine
{
public Guid ProductId { get; private set; }
public string ProductName { get; private set; } = "";
public decimal UnitPrice { get; private set; }
public int Quantity { get; private set; }
}ProductName and UnitPrice do not belong to Catalog's database in this context. They are data owned by Order to preserve the historical correctness of the order.
6. Common mistakes
- One database, multiple services: Separating projects does not create real independence if all services update the same tables.
- Shared entity models: A common entity library can force different bounded contexts to use one model.
- Boundaries based only on technical layers: “All repositories in one service and all controllers in another” does not represent business boundaries.
- A call chain for every request: Long chains such as Catalog → Inventory → Pricing → Order increase latency and failure risk.
- Business logic in AppHost: AppHost should connect resources; it should not make order or stock decisions.
- Sharing contracts without a real need: Shared contracts introduce versioning and backward-compatibility responsibilities.
7. Practice: Design the first solution skeleton
In this short exercise, create the solution skeleton without adding databases yet.
- Create
DistributedShop.AppHost,DistributedShop.ServiceDefaults,CatalogService,OrderService, andInventoryServiceprojects. - Reference the Service Defaults project from the three API projects and call
AddServiceDefaultsin eachProgram.csfile. - Register the three API projects in AppHost with
AddProject. - Express Order's dependencies on Catalog and Inventory as AppHost references.
- Add one health endpoint and one simple information endpoint to each service.
- Inspect the resource status in the dashboard and verify service names, endpoints, and dependencies.
As an additional task, answer these questions: Should Order read only the product price from Catalog, or should it receive the entire product model? How should an existing order be protected when Catalog changes a price? Should Inventory report the reservation result through a synchronous response or a message?
Conclusion
In this lesson, we defined service boundaries through business responsibilities. We saw that Catalog, Order, and Inventory should have separate models and data ownership. We examined why limiting data access through APIs or messages is safer than sharing entities. We also clarified that Aspire AppHost is not a business-logic layer; it is the application model that composes services and resources.
In the next stage, we will turn this skeleton into working ASP.NET Core APIs with databases and Aspire resources. The main test of a strong solution structure is not whether services appear in separate folders, but whether each service can manage its own decisions, data, and lifecycle.
Sources
- https://learn.microsoft.com/en-us/dotnet/architecture/microservices/architect-microservice-container-applications/data-sovereignty-per-microservice
- https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/app-host-overview
- https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/service-defaults
- https://learn.microsoft.com/en-us/dotnet/aspire/service-discovery/overview