Previous lesson: Mastering Prompt Engineering .NET 🧱 Lesson 2C - Implementing Repository Interfaces (Domain & Infrastructure Layers)
🚀 Introduction
In this lecture, we’ll focus on implementing the Application Layer —
this is the layer that connects our Domain logic (repositories) to the API layer.
It contains
DTOs (Data Transfer Objects) → used to safely send/receive data between layers.
Services → contain business workflows using domain repositories.
The goal is to keep
The Domain layer independent and pure (entities + repository interfaces).
The Infrastructure layer focused only on persistence (EF Core).
The Application layer responsible for business use cases.
Folder Structure Overview

ECommerce.Application/DTOs/ProductDto.cs
namespace ECommerce.Application.DTOs;
public class ProductDto
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public decimal Price { get; set; }
public int Stock { get; set; }
}ECommerce.Application/DTOs/CustomerDto.cs
namespace ECommerce.Application.DTOs;
public class CustomerDto
{
public Guid Id { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string PhoneNumber { get; set; } = string.Empty;
public string FullName => $"{FirstName} {LastName}";
}ECommerce.Application/DTOs/OrderDto.cs
using ECommerce.Domain.Enums;
namespace ECommerce.Application.DTOs;
public class OrderDto
{
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public DateTime OrderDate { get; set; }
public decimal TotalAmount { get; set; }
public OrderStatus Status { get; set; }
public List<OrderItemDto> Items { get; set; } = new();
}ECommerce.Application/DTOs/OrderItemDto.cs
namespace ECommerce.Application.DTOs;
public class OrderItemDto
{
public Guid Id { get; set; }
public Guid OrderId { get; set; }
public Guid ProductId { get; set; }
public string ProductName { get; set; } = string.Empty;
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice => UnitPrice * Quantity;
}Service Interfaces
We’ll now define service interfaces that describe the available operations for each entity.
ECommerce.Application/Services/Interfaces/IProductService.cs
using ECommerce.Application.DTOs;
namespace ECommerce.Application.Services.Interfaces;
public interface IProductService
{
Task<IEnumerable<ProductDto>> GetAllAsync();
Task<ProductDto?> GetByIdAsync(Guid id);
Task<ProductDto> AddAsync(ProductDto dto);
Task UpdateAsync(ProductDto dto);
Task DeleteAsync(Guid id);
}ECommerce.Application/Services/Interfaces/ICustomerService.cs
using ECommerce.Application.DTOs;
namespace ECommerce.Application.Services.Interfaces;
public interface ICustomerService
{
Task<IEnumerable<CustomerDto>> GetAllAsync();
Task<CustomerDto?> GetByIdAsync(Guid id);
Task<CustomerDto> AddAsync(CustomerDto dto);
Task UpdateAsync(CustomerDto dto);
Task DeleteAsync(Guid id);
}ECommerce.Application/Services/Interfaces/IOrderService.cs
using ECommerce.Application.DTOs;
namespace ECommerce.Application.Services.Interfaces;
public interface IOrderService
{
Task<IEnumerable<OrderDto>> GetAllAsync();
Task<OrderDto?> GetByIdAsync(Guid id);
Task<OrderDto> AddAsync(OrderDto dto);
Task UpdateAsync(OrderDto dto);
Task DeleteAsync(Guid id);
}ECommerce.Application/Services/Interfaces/IOrderItemService.cs
uusing ECommerce.Application.DTOs;
namespace ECommerce.Application.Services.Interfaces;
public interface IOrderItemService
{
Task<IEnumerable<OrderItemDto>> GetByOrderIdAsync(Guid orderId);
Task<IEnumerable<OrderItemDto>> GetAllAsync();
}Service Implementations
Next, we’ll implement concrete service classes that depend on repository interfaces from the Domain layer.
ECommerce.Application/Services/Implementations/ProductService.cs
using ECommerce.Application.DTOs;
using ECommerce.Application.Services.Interfaces;
using ECommerce.Domain.Entities;
using ECommerce.Domain.Repositories;
namespace ECommerce.Application.Services.Implementations;
public class ProductService : IProductService
{
private readonly IProductRepository _repository;
public ProductService(IProductRepository repository)
{
_repository = repository;
}
public async Task<IEnumerable<ProductDto>> GetAllAsync()
{
var products = await _repository.GetAllAsync();
return products.Select(p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.Description,
Price = p.Price,
Stock = p.Stock
});
}
public async Task<ProductDto?> GetByIdAsync(Guid id)
{
var p = await _repository.GetByIdAsync(id);
if (p == null) return null;
return new ProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.Description,
Price = p.Price,
Stock = p.Stock
};
}
public async Task<ProductDto> AddAsync(ProductDto dto)
{
var entity = new Product
{
Name = dto.Name,
Description = dto.Description,
Price = dto.Price,
Stock = dto.Stock
};
await _repository.AddAsync(entity);
dto.Id = entity.Id;
return dto;
}
public async Task UpdateAsync(ProductDto dto)
{
var entity = await _repository.GetByIdAsync(dto.Id);
if (entity == null) return;
entity.Name = dto.Name;
entity.Description = dto.Description;
entity.Price = dto.Price;
entity.Stock = dto.Stock;
await _repository.UpdateAsync(entity);
}
public async Task DeleteAsync(Guid id) => await _repository.DeleteAsync(id);
}ECommerce.Application/Services/Implementations/CustomerService.cs



Join the conversation! Your thoughts help the community grow.