.Net onion/clean architecture sample
This layer does not depend on any other layer. This layer contains entities, enums, specifications etc.
Add repository and unit of work contracts in this layer.
This layer contains business logic, services, service interfaces, request and response models.
Third party service interfaces are also defined in this layer.
This layer depends on domain layer.
This layer contains database related logic (Repositories and DbContext), and third party library implementation (like logger and email service).
This implementation is based on domain and application layer.
This layer contains Webapi or UI.
Domain model are of 2 types
Domain entity (data only)
Domain model (data + behaviour)
There are 2 types of validations in DDD:
Model Field validations
Model validations can be validated in Application layer or Domain layer.
Business validations
Business validations can be validated in Applciation layer or Domain layer.
Business validations have two types:
For Aggregate pattern add both types of validations inside domain layer.
Problem occurs when validating domain model against other domain models.
public WithdrawMoney(double amount, Func<string, bool> isBankAccountActive){
if(!isBankAccountActive(BankAccountNumber)){
throw new DomainValidationException("Bank account is not in active state");
}
}
public WithdrawMoney(double amount, BankAccount bankAccount){
if(bankAccount.Status != Active){
throw new DomainValidationException("Bank account is not in active state");
}
}
For more details read