A data transfer object (DTO) defines the data communicated across a boundary. It separates external communication from the domain or persistence model, allowing data to be flattened, circular references removed, and internal properties hidden. section 7.4.1
Use DTOs to make permitted operations explicit:
- Represent creation and update as separate types, exposing only the fields each operation accepts. section 7.4.1
- Pass DTOs through repository methods instead of exposing database entities directly. section 7.4.1
- Prefer explicit types when direct entity use would depend on undocumented change tracking, ignored fields, or side effects. section 7.4.1
Listing 7.19 ICustomerRepository interface
namespace CustomerBatchImporter;
public interface ICustomerRepository
{
Task CreateAsync( #1
NewCustomerDto customer); #2
Task UpdateAsync(
UpdateCustomerDto customer);
Task<Customer?> GetByEmailAsync(string email); #3
}
public record NewCustomerDto( #4
string Email,
string Name,
string License
) { } #5
public record UpdateCustomerDto(
int Id,
string? NewName,
string? NewLicense
) { }
The repository interface demonstrates separate record DTOs for creating and updating customers. section 7.4.1





