I'm new to DDD, and been reading a lot about it, but I can't figure this out. I'm making a tipical CRUD operation (Create), and I have to validate some field against, the rest of the entities persisted in my repository.
I know that Application Services, shouldn't have any business logic, a domain Entity, should not access to repositories directly, a Domain Service, may be the best choice, but I don't know how to do it well. I'm very confused.
How can I correct the next code:
class CustomerApplicationService {
void AddNew ( CustomerDTO myNewCustomerDTO ) {
CustomerRepository myCustomerRepo = new CustomerRepository();
var allCustomers = myCustomerRepo.FindAll();
for each (Customer c in allCustomers) {
if (c.SomeField == myNewCustomerDTO.SomeField) {
// do something, check duplicate data, etc
}
}
var myNewCustomer = new Customer();
// map myNewCustomer ... fields with myNewCustomerDTO
myCustomerRepo.Save( myNewCustomer );
}
}
Thanks!