Onion Architecture is a clean, modular approach to building applications, where the core business logic is at the center, and all other layers (like data access, UI, etc.) are built around it. This architecture helps in achieving separation of concerns, testability, and maintainability by organizing the application into distinct layers.
In this blog, we'll explore Onion Architecture by building a simple Product and Order service in .NET Core. We'll break down each layer, explain its purpose, and provide code examples to demonstrate how they interact with one another.
What is Onion Architecture?
Onion Architecture, introduced by Jeffrey Palermo, is structured in concentric layers, where.
- The core layer contains the business logic and domain entities.
- The infrastructure layer handles database access, external services, and other technical concerns.
- The application layer is responsible for service implementations, use cases, and coordinating data flow between the core and infrastructure.
- The presentation layer deals with the user interface or API endpoints.
Here's a visual representation.

Setting Up the Project
Let's build a Product and Order service using Onion Architecture. We'll structure the solution into four projects.
- Domain: Contains entities, repository interfaces, and domain services.
- Application: Implements use cases, commands, queries, and application services.
- Infrastructure: Manages data access, external service integrations, and repository implementations.
- Presentation: Exposes API endpoints for external clients.
1. Domain Layer
The Domain Layer is the core of the Onion Architecture. It contains the business logic, entities, and interfaces that define the domain of your application.
Entities: Product and Order.
namespace ProductOrder.Domain.Entities
{
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class Order
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public int Quantity { get; set; }
public DateTime OrderDate { get; set; }
public decimal Total => Quantity * Product.Price; // Business logic example
public Product Product { get; set; }
}
}
Repositories: Interfaces for Data Access.
namespace ProductOrder.Domain.Interfaces
{
public interface IProductRepository
{
Task<Product> GetByIdAsync(Guid id);
Task<IEnumerable<Product>> GetAllAsync();
Task AddAsync(Product product);
Task UpdateAsync(Product product);
Task DeleteAsync(Guid id);
}
public interface IOrderRepository
{
Task<Order> GetByIdAsync(Guid id);
Task<IEnumerable<Order>> GetAllAsync();
Task AddAsync(Order order);
Task UpdateAsync(Order order);
Task DeleteAsync(Guid id);
}
}
2. Application Layer
The Application Layer coordinates the data flow between the domain entities and the infrastructure. It implements the application's use cases and is where the business logic meets the infrastructure.
Services: Product and Order Management.
namespace ProductOrder.Application.Services
{
public class ProductService
{
private readonly IProductRepository _productRepository;
public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public async Task<Product> GetProductByIdAsync(Guid id)
{
return await _productRepository.GetByIdAsync(id);
}
public async Task AddProductAsync(Product product)
{
await _productRepository.AddAsync(product);
}
// Additional methods...
}
public class OrderService
{
private readonly IOrderRepository _orderRepository;
private readonly IProductRepository _productRepository;
public OrderService(IOrderRepository orderRepository, IProductRepository productRepository)
{
_orderRepository = orderRepository;
_productRepository = productRepository;
}
public async Task<Order> CreateOrderAsync(Guid productId, int quantity)
{
var product = await _productRepository.GetByIdAsync(productId);
if (product == null) throw new Exception("Product not found");
var order = new Order
{
Id = Guid.NewGuid(),
ProductId = productId,
Quantity = quantity,
OrderDate = DateTime.UtcNow,
Product = product
};
await _orderRepository.AddAsync(order);
return order;
}
// Additional methods...
}
}
3. Infrastructure Layer
The Infrastructure Layer is responsible for communicating with external systems like databases, file systems, and web services. It implements the repository interfaces defined in the domain layer.
Implementing Repositories

Mariusz PostolPosted Aug 21, 2024, 9:29 AM
You said: "Onion Architecture is a clean, modular approach to building applications, where the core business logic is at the center, and all other layers (like data access, UI, etc.) are built around it. This architecture helps in achieving separation of concerns, testability, and maintainability by organizing the application into distinct layers." What is the difference compared with the layered program architecture? Can you comment on it? What benefit we can expect? How to prove that is cleaner and more modular?